SendNetMsg ( type,msg$,from_player[,to_player][,reliable] )
Parameters
|
type - message type, 1 to 99 (higher values are reserved for the system) msg$ - the message text to send from_player - id of the sending player (one of your local players) to_player (optional) - id of the player to send to; 0 broadcasts to every player (default) reliable (optional) - 1 guarantees delivery (default); 0 is faster but the message may be lost |
Description
|
Sends a message to one player, or to everyone, in the current network game. Returns 1 if the message was handed to the network, 0 on failure. The receiving machines pick it up with RecvNetMsg and read it back through NetMsgType, NetMsgFrom, NetMsgTo and NetMsgData$. The type is your own protocol: pick numbers 1-99 and give them meanings (1 = chat, 2 = position update, 3 = fire...). Values from 100 up belong to the system messages RecvNetMsg reports. To keep traffic down, pack related values into one string - "x,y,frame" - rather than sending three messages. Reliable messages always arrive but cost more; for things sent every frame where a lost one does not matter (positions), send unreliable and let the next update cover any gap. One big gotcha: messages between two players on the same machine are never delivered - RecvNetMsg only sees messages from other machines. So a player does not hear their own broadcasts, and testing needs two copies of the program running (they can share one PC via a loopback join). See also: RecvNetMsg, NetMsgType, NetMsgData, CreateNetPlayer. |
Example
; SendNetMsg Example ; ------------------ ; Run TWO copies of this program on one machine: the first copy ; hosts "demo game", the second finds it over loopback and joins. ; The copies then greet each other with SendNetMsg. 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)+"'" ; SendNetMsg type,data$,from,to,reliable ; type 1-99 = your own message kinds (here 1 = greeting) ; to 0 = broadcast to every player in the game ; reliable = 1 guarantees delivery SendNetMsg 1,"Hello from "+NetPlayerName$(me),me,0,1 Print "Broadcast a greeting with SendNetMsg" ; Listen for the other copy for 6 seconds Print "Listening for messages for 6 seconds..." start=MilliSecs() heard=0 While MilliSecs()-start<6000 If RecvNetMsg() Then mtype=NetMsgType() If mtype>=1 And mtype<=99 Then heard=heard+1 Print "Received '"+NetMsgData$()+"' from "+NetPlayerName$(NetMsgFrom()) ; Answer greetings directly to their sender If mtype=1 Then SendNetMsg 2,"Nice to meet you!",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
Index