Search code examples
godotenet

How can I spawn two nodes at different coordinates using High Level Multiplayer (ENET)?


Currently, I am using High Level Multiplayer (ENET) with Godot to spawn two player nodes (with KinematicBody2D and Collisions). For reasons of collisions causing problems, I cannot have the players spawn in the same coordinates, so I am trying to get them to spawn next to each other.

I put my Player.gd on this pastebin link which has the code I am trying to debug.

The problem I am having is trying to get the client and server to spawn the players at the coordinates I have set in the initialization of the player scene. Currently, the client and server can position themselves correctly, but the other player hangs out at (0,0) until I move the player.

The player movement works perfectly, so even though that code is in the paste, it isn't relevant to this question.

The client's stdout is

Connecting to Server!!!
Player Connected!!!
Network Master: 151057035
Player ID: 151057035
Client Position - Master: 151057035
Player: 151057035 Set Position: (500, 250)
Own ID: 151057035 Player ID: 151057035
Caller ID: 0
Network Master: 1
Player ID: 151057035
Server Position - Master: 1
Player: 1 Set Position: (300, 250)
Player: 1 Set Position: (300, 250)
Player: 151057035 Set Position: (500, 250)
Own ID: 151057035 Player ID: 151057035
Caller ID: 1

The server's stdout is

Hosting Server!!!
Player Connected!!!
Network Master: 1
Player ID: 1
Server Position - Master: 1
Player: 1 Set Position: (300, 250)
Own ID: 1 Player ID: 1
Caller ID: 0
Network Master: 959488417
Player ID: 1
Client Position - Master: 959488417
Player: 959488417 Set Position: (500, 250)
Player: 959488417 Set Position: (500, 250)
Player: 1 Set Position: (300, 250)
Own ID: 1 Player ID: 1
Caller ID: 959488417

Solution

  • Basically, to tell the client what coordinates to spawn at, you need to have the server tell the client what coordinates to use.

    My own personal code involves player registration and tracking, so it is too complicated to just paste the full code. Instead, I am posting a greatly simplified spawn code (that does not even include multi-world support)


    Sample Code

    # Server Code
    master func spawn_server(net_id: int, coordinates: Vector2):
        rpc_unreliable_id(net_id, "spawn", coordinates) # Godot's RPC handles ensuring packet delivery just like TCP, but with less overhead.
    
    # Client Code in Same File
    puppet func spawn(coordinates: Vector2):
        player_object.position = coordinates