NetMsgTo ( )
Parameters
| None. |
Description
|
Returns the id of the player the last received message was addressed to. After RecvNetMsg returns 1, this tells you which player the sender aimed the message at: one of your local players' ids for a direct message, or 0 when it was broadcast to everyone. That matters mainly when one machine runs more than one player - it is how you route a private message to the right one. With a single local player and broadcast-only traffic you will rarely need it; NetMsgType, NetMsgFrom and NetMsgData$ do the day-to-day work. See also: RecvNetMsg, NetMsgType, NetMsgFrom, NetMsgData, SendNetMsg. |
Example
; NetMsgTo Example ; ---------------- ; Run TWO copies of this program on one machine: the first copy ; hosts "demo game", the second finds it over loopback and joins. ; NetMsgTo shows who each received message was addressed to: ; broadcasts arrive with NetMsgTo = 0, replies with our own id. 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)+"' (id "+me+")" ; Broadcast a greeting: to=0 means every player gets it SendNetMsg 1,"Hello from "+NetPlayerName$(me),me,0,1 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 "Message '"+NetMsgData$()+"'" ; NetMsgTo returns the recipient's player id; ; 0 means it was broadcast to everyone If NetMsgTo()=0 Then Print " NetMsgTo = 0 (a broadcast)" Else Print " NetMsgTo = "+NetMsgTo()+" (sent straight to us)" EndIf ; Reply directly, so the other copy sees a non-zero NetMsgTo If mtype=1 Then SendNetMsg 2,"A private reply",me,NetMsgFrom(),1 EndIf 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 recipients." EndIf StopNetGame Print "Left the game" EndIf Print "" Print "Press any key to close the example" WaitKey End
Index