#Managing Multiplayer Synchronizer Authority

4 messages · Page 1 of 1 (latest)

hybrid maple
#

Hi all,

I've recently been learning the Godot 4(.2) multiplayer api and I got stuck for a while on making sure that the multiplayer authority for MultiplayerSynchronizer was set correctly on both the client and server.
I am spawning a Node upon client connection and I am trying to give authority over that node's MultiplayerSynchronizer to that same client.
I tried using an exported variable that represented the authority, but it would always default on the client side rather than using what was set on the server.
I ended up simply setting the node's name to the desired authority and then parsed that in _enter_tree and set the authority.
While this did work, it feels a little bit hacky, and I was wondering if there was some sort of built in way to synchronize the MultiplayerSynchronizer's authority via the MultiplayerSpawner.
It seems a little odd to me that something like the node's name would be required to be sent over by the server, but not other attributes like the authority.

Thanks.

woeful bone
#

My multiplayer system works similar to this, and here's how I did it:

When the node is added and the player is connected, it sets the node's name to the player's unique ID, which you can get through the function multiplayer.get_unique_id()
When this node is added to the scene tree, it calls a function called set_multiplayer_authority(), and it gets the node's name and converts it to an int to set the authority properly.

The code for adding a player is pretty simple, but make sure that whenever someone hosts the server, it connects "peer_connected" to this function. You can do that by just typing in multiplayer.peer_connected.connect(add_player)

The actual function for adding a player looks like this. The id is passed in automatically by the signal connected to the function, which gets the ID of the player connecting.

func add_player(id):
  var player = playerScene.instantiate()
  player.name = str(id)
  add_child(player)

Finally, when the player is connected, the node added should have a script that lets it know when it enters the game. This is done with the "_enter_tree()" function.

func _enter_tree():
  set_multiplayer_authority(str(name).to_int())
hybrid maple
#

yeah that's more or less what I did.
It definitely works, it just feels a little strange to transmit the id via the node's name.

#

but I guess that because we don't want nodes with the same name, it's actually probably best to do it that way anyway so it's easier to get the nodes from the tree