Blitz3D+ Command Reference

NetPlayerLocal ( player )

Parameters

player - a player id, typically from CreateNetPlayer or NetMsgFrom

Description

Returns whether a player lives on this machine.

1 means the player was created here with CreateNetPlayer; 0 means it is a remote player on another machine (or an id that is not in the game at all).

Use it when iterating everyone in the session to decide who you control: local players read the keyboard and send updates, remote players are driven by incoming messages. On a join notice (type 100), it also distinguishes "someone arrived" from an echo of your own player registering.

See also: CreateNetPlayer, NetPlayerName, NetMsgFrom, RecvNetMsg.

Example

; NetPlayerLocal Example
; ----------------------

; Run TWO copies of this program on one machine: the first copy
; hosts "demo game", the second finds it over loopback and joins.
; NetPlayerLocal tells whether a player id lives on THIS machine.

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))

    ; Our own player is local, so NetPlayerLocal returns 1
    Print "NetPlayerLocal('"+NetPlayerName$(me)+"') = "+NetPlayerLocal(me)+" (created here)"

    ; Broadcast a greeting for the other copy to receive
    SendNetMsg 1,"Hello!",me,0,1

    ; Players heard from the other copy are NOT local (returns 0)
    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
                sender=NetMsgFrom()
                Print "'"+NetMsgData$()+"' from '"+NetPlayerName$(sender)+"'"
                Print "    NetPlayerLocal = "+NetPlayerLocal(sender)+" (a remote player)"
                If mtype=1 Then SendNetMsg 2,"Hello back!",me,sender,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 compare the flags."
    EndIf

    StopNetGame
    Print "Left the game"
EndIf

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

End

Index