| None. |
|
Fetches the next message in the current network game. Returns 1 if a message was collected, 0 if there was nothing waiting. Each collected message is then read through NetMsgType (what kind), NetMsgFrom (who sent it), NetMsgTo (who it was for) and NetMsgData$ (the text). Call it in a loop every frame until it returns 0, so messages never pile up. Besides your own messages (types 1-99), it also delivers system events: 100 a player joined, 101 a player left, 102 this machine just became the host, 200 the session was lost - handle at least 100, 101 and 200 in any real game. Note that messages sent between two players on the same machine are never delivered - RecvNetMsg only reports traffic from other machines. Run two copies of your program (even on one PC) to see messages flow. A player must exist locally (CreateNetPlayer) before messages arrive at all. See also: SendNetMsg, NetMsgType, NetMsgFrom, NetMsgData. |
; RecvNetMsg Example
; ------------------
; Run TWO copies of this program on one machine: the first copy
; hosts "demo game", the second finds it over loopback and joins.
; Each copy polls RecvNetMsg to hear the other one.
SeedRnd MilliSecs()
; Join an existing demo game if one is running, else host one
status=JoinNetGame("demo game","127.0.0.1")
If status=1 Then
Print "Joined an existing demo game as a client"
Else
If HostNetGame("demo game")=2 Then status=2
If status=2 Then Print "No game found - hosting a new demo game"
EndIf
If status=0 Then
Print "Could not host or join a network game."
Else
; A player must exist before messages can be sent or received
me=CreateNetPlayer("Player"+Rand(100,999))
Print "Playing as '"+NetPlayerName$(me)+"'"
; Broadcast a greeting for the other copy to receive
SendNetMsg 1,"Hello from "+NetPlayerName$(me),me,0,1
; RecvNetMsg returns True whenever a message has arrived;
; game loops poll it every frame, this loop polls for 6 seconds
Print "Polling RecvNetMsg for 6 seconds..."
start=MilliSecs()
heard=0
While MilliSecs()-start<6000
If RecvNetMsg() Then
; A message is in - the NetMsg commands describe it
mtype=NetMsgType()
If mtype>=1 And mtype<=99 Then
heard=heard+1
Print "Received '"+NetMsgData$()+"' from "+NetPlayerName$(NetMsgFrom())
If mtype=1 Then SendNetMsg 2,"Heard you loud and clear!",me,NetMsgFrom(),1
EndIf
If mtype=100 Then Print NetPlayerName$(NetMsgFrom())+" joined the game"
EndIf
Delay 10
Wend
If heard=0 Then
Print "No messages arrived - a program only hears OTHER copies."
Print "Start a second copy of this example to see the exchange."
EndIf
StopNetGame
Print "Left the game"
EndIf
Print ""
Print "Press any key to close the example"
WaitKey
End