Blitz3D+ Command Reference

NetMsgData$ ( )

Parameters

None.

Description

Returns the text of the message last collected with RecvNetMsg.

This is the msg$ string exactly as the sender passed it to SendNetMsg - your game's payload. Most games pack several values into it ("x,y,frame") and parse them back out here, keyed by NetMsgType.

It is only meaningful for user messages (types 1-99); the system messages (100 and up) carry no text, so check NetMsgType first.

Each RecvNetMsg replaces the stored message, so read the data before collecting the next one.

See also: RecvNetMsg, NetMsgType, NetMsgFrom, SendNetMsg.

Example

; NetMsgData Example
; ------------------

; Run TWO copies of this program on one machine: the first copy
; hosts "demo game", the second finds it over loopback and joins.
; NetMsgData$ returns the text payload of a received message.

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)+"'"

    ; Pack several values into ONE string - fewer, fuller messages
    ; keep network traffic low. Here: x, y and frame of our hero.
    SendNetMsg 1,"120,45,3",me,0,1
    Print "Broadcast the packet '120,45,3' (x,y,frame)"

    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
                ; NetMsgData$ is the string given to SendNetMsg -
                ; the receiver parses the packed values back out
                Print "NetMsgData$ = '"+NetMsgData$()+"' from "+NetPlayerName$(NetMsgFrom())
                If mtype=1 Then SendNetMsg 2,"130,50,4",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 payloads."
    EndIf

    StopNetGame
    Print "Left the game"
EndIf

Print ""
Print "Press any key to close the example"
WaitKey

End

Index