Multiple Camera2D in the world

jomamojomamo Posts: 3Member
in 2D

I have a multiplayer game setup with the server creating players when my game starts and issuing a command to all connected clients to create players in their worlds.

Based on this tutorial:
https://gitlab.com/menip/godot-multiplayer-tutorials/-/tree/master/LobbyTutorial

Server spawning players:

func spawn_player(spawn_pos, id):
    var player = Player.instance()

    player.position = spawn_pos
    player.name = String(id) # Important
    player.set_network_master(id) # Important

    $YSort/Players.add_child(player)

Client spawning players:

puppet func spawn_player(spawn_pos, id):
    var player = Player.instance()

    player.position = spawn_pos
    player.name = String(id) # Important
    player.set_network_master(id) # Important

    print("Spawn player with id " + str(id))

    $YSort/Players.add_child(player)

Player ready

func _ready():
    if is_network_master():
        $Camera2D.make_current()
        ...
    else:       
        var player_id = get_network_master()
        ...

The first client to spawn, the camera follows the player around as they move. The second player to spawn the camera does not follow their respective player, it follows the first player.

I played around with the ordering in which the server calls the client to spawn the list of players:

    for player in world_players:
        print("RPC caller id " + str(caller_id) + "call to spawn player " + str(player.get_network_master()))
        world.rpc_id(caller_id, "spawn_player", player.position, player.get_network_master())

If the player owned agent is created last in the list, the camera will follow the player. If the player is created first, and then all of the other players in the game for the client, the camera does not follow the player.

Even if I ensure that the last player in world_players is the player owned agent, there is no guarantee that the packets will arrive in the desired order on the RPC calls, and every so often the camera still appears broken for a player.

Any idea on why the camera is not setting current correctly for each player? I am marking the network_master and I am marking the camera as current on the client if the client owns the player.

Comments

  • TwistedTwiglegTwistedTwigleg Posts: 2,655Admin

    Is there any code attached to the Camera2D nodes? Maybe the code for following the player is not setup correctly, assuming there is code for doing such.

    Another thing to check would be to see if the camera's are actually following the first player, or if the first player's camera is set as current and/or is overriding the view for all of the other players. If the camera nodes are actually following the player, then it should be a script issue, while the first player's camera being the current suggests the is_network_master() condition is not working as intended. I would suggest somehow marking each Camera (maybe just spawn a Label or something) that will allow you to tell which Camera each player is seeing.

  • jomamojomamo Posts: 3Member

    Thanks for the ideas, ill give them a try.

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Emoji
Image
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file