NetMsgType ( )
Parameters
| None. |
Description
|
Returns the type of the message last collected with RecvNetMsg. This is the first thing to check after RecvNetMsg returns 1, because it says how to interpret the rest: 1-99 : a user message - one of your own types sent with SendNetMsg; the text is in NetMsgData$ 100 : a player joined the game (NetMsgFrom gives the new player's id) 101 : a player left the game (NetMsgFrom gives who) 102 : the host quit and THIS machine is now the host 200 : the session was lost - a fatal error, leave the game A tidy way to handle them is a Select NetMsgType() block in your message loop. Note the messages you receive are always from other machines - local players' messages to each other are never delivered. See also: RecvNetMsg, NetMsgFrom, NetMsgTo, NetMsgData, SendNetMsg. |
Example
; NetMsgType Example ; ------------------ ; Run TWO copies of this program on one machine: the first copy ; hosts "demo game", the second finds it over loopback and joins. ; Every received message is labelled with its NetMsgType. 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 user message of type 7 for the other copy SendNetMsg 7,"Hello from "+NetPlayerName$(me),me,0,1 ; NetMsgType values: 1-99 = your own message kinds, ; 100 = player joined, 101 = player left, ; 102 = this machine became the host, 200 = fatal error Print "Listening for messages for 6 seconds..." start=MilliSecs() heard=0 While MilliSecs()-start<6000 If RecvNetMsg() Then heard=heard+1 mtype=NetMsgType() If mtype>=1 And mtype<=99 Then Print "NetMsgType "+mtype+" (user message): '"+NetMsgData$()+"'" If mtype=100 Then Print "NetMsgType 100 (player joined): "+NetPlayerName$(NetMsgFrom()) If mtype=101 Then Print "NetMsgType 101 (player left)" If mtype=102 Then Print "NetMsgType 102 (we are the host now)" 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 types." EndIf StopNetGame Print "Left the game" EndIf Print "" Print "Press any key to close the example" WaitKey End
Index