Multiple Camera2D in the world
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:
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)
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.