HostNetGame ( game_name$ )

Parameters

game_name$ - name for the new game session

Description

Hosts a new network game under the name you give.

A TCP/IP session with the given name is created on this machine. Returns 2 if hosting started successfully (matching StartNetGame's "hosting" result) and 0 if it failed - it never returns 1.

Other machines join with JoinNetGame using the same game name and this machine's IP address. Once hosting, create your players with CreateNetPlayer and exchange messages with SendNetMsg and RecvNetMsg; StopNetGame ends the session. Only one session can be active at a time - starting another first raises a runtime error.

The current Windows runtime implements this classic player-message API with Winsock. For shared scenes across desktop, mobile and web, see the Extended Shared World multiplayer guide. Classic NetGame and Shared World sessions cannot run together.

See also: JoinNetGame, StartNetGame, StopNetGame, CreateNetPlayer.

Example

; HostNetGame Example
; -------------------

; HostNetGame starts hosting a network game under the name you
; give. It returns 2 on success.
status=HostNetGame("demo game")

If status=2 Then
    Print "HostNetGame returned 2 - now hosting 'demo game'"

    ; A player must exist before messages can be received
    host_player=CreateNetPlayer("Host")
    Print "Created player '"+NetPlayerName$(host_player)+"'"

    ; Listen for guests - message type 100 means a player joined
    Print ""
    Print "Listening 8 seconds for joiners..."
    Print "(run the JoinNetGame example in a second copy of Blitz3D+ now)"
    start=MilliSecs()
    While MilliSecs()-start<8000
        If RecvNetMsg() Then
            If NetMsgType()=100 Then Print NetPlayerName$(NetMsgFrom())+" joined the game!"
            If NetMsgType()=101 Then Print "A player left the game"
        EndIf
        Delay 10
    Wend

    ; Always end the session when done
    StopNetGame
    Print "Game stopped"
Else
    Print "HostNetGame returned "+status+" - hosting failed"
    Print "(network play may be unavailable on this machine)"
EndIf

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

End

Index