GNet questions

BlitzMax Forums/BlitzMax Programming/GNet questions

Gillissie(Posted 2009) [#1]
I am working with GNet for the first time. It's quite different from other networking libraries I've used before, so I have questions.

When a player joins a game, it apparently just silently slips in without announcing itself. Is there a way to detect when a new player joins? I tried sending a one-off "I joined" message to all remote objects after connecting, but the remote objects don't exist immediately after the connect command so nothing goes out. I'm not sure when the remote objects get created, but they are there eventually because I wrote a function to check for them, which I manually fire off after being connected.


Volker(Posted 2009) [#2]
Shouldn't GNetObjectState() return GNET_CREATED?


Gillissie(Posted 2009) [#3]
The problem is that there is no object returned, regardless of the state. Here's the chunk of code I'm talking about:

In the GNet example program, I added this right after the "connect" command line code:

If Not GNetConnect(host, arg, GAMEPORT) Notify "Connect failed"
Local MSG:TGNetObject = CreateGNetMessage(host)
SetGNetString(MSG, SLOT_TYPE, "join")
SetGNetString(MSG, SLOT_NAME, "Dude")
Broadcast(MSG)


I wrote the Broadcast() function to find all remote objects and send the message to each one:

Function Broadcast(message:TGNetObject)
	For Local obj:TGNetObject = EachIn GNetObjects(host)
		If GNetObjectRemote(obj) Then
			SendGNetMessage(message, obj)
		End If
	Next
End Function


However, there are no remote objects at this point, only 1 local one. However, if I call this other function by pressing a certain key after connecting, it shows the remote object.

Function ListObjects()
	Local i%, j%
	
	For Local obj:TGNetObject = EachIn GNetObjects(host)
		i:+1
		If GNetObjectRemote(obj) Then
			j:+1
		End If
	Next
	
	Print i + " objects, " + j + " remote"
End Function


When exactly does the remote object get created?

If there's a different way to detect a newly connected player, I'll use it. But the GNet docs are very sparse.


Gillissie(Posted 2009) [#4]
Ok, I think I have this figured out. In each loop right after calling GNetSync(), I check for objects in the GNET_CREATED list. If any exist, then those are newly connected peers. I just hope the list only includes peers and doesn't include message type objects.

While Not KeyHit(KEY_ESCAPE)
  ...etc...

  GNetSync()
  CheckNewPeers()

  ...etc...

Wend

Function CheckNewPeers()
	If GNetObjects(host, GNET_CREATED).Count() > 0 Then
		Local obj:TGNetObject
		
		For obj = EachIn GNetObjects(host, GNET_CREATED)
			Print "New player joined: " + GetGNetString(obj, SLOT_NAME)
			' Do player initialization.
		Next
	End If
End Function



Gillissie(Posted 2009) [#5]
One more thing. The CheckNewPeers() function needs to check for the object type of "player", otherwise it picks up bullets and any other type of object that was created.

Function CheckNewPeers()
	If GNetObjects(host, GNET_CREATED).Count() > 0 Then
		Local obj:TGNetObject
		
		For obj = EachIn GNetObjects(host, GNET_CREATED)
			If GetGNetString(obj, SLOT_TYPE) = "player" Then
				Print "New player joined: " + GetGNetString(obj, SLOT_NAME)
			End If
		Next
	End If
End Function



Will(Posted 2009) [#6]
I recommend using an int in every GNetObject's first spot as an identifier of type - just make a peer object that is created and look to see if any of those are detected.


Gillissie(Posted 2009) [#7]
Will,

I am using an INT for TYPE in my own program. In this example I was referring to the example program, which uses a string TYPE slot.