MultiplayerSpawner and Authority question(s)

Godot Version

4.3

Question

I have a 3D multiplayer game utilizing a lot of the code from the Godot High Level Multiplayer docs.
My server, that can also be a client or Player, spawns the Players here on the Root Game Node with this script:

#ROOT GAME NODE SCRIPT#
#Server is running all of this ONLY I know this because in the script before where it calls start_game() I am checking multiplayer.is_server()#

func start_game():
		for player_id in Lobby.players:
			spawn_player(player_id)

func spawn_player(authority_id: int):
	var player = player_scene.instantiate()
	
	player.name = "Player" + str(authority_id)
	
	add_child(player)
	player.global_transform.origin = Vector3(0,0,0)
	player.set_multiplayer_authority(authority_id)

It is also setting the authority as you can see. Then the MultiplayerSpawner Node is supposed to replicate the Player across all Clients right?
Well, the authority is all messed up and I am unable to play/control each individual client when testing with multiple Debug Instances.

Here is where I am trying to remedy this in my Player code:

#PLAYER SCRIPT#

func _ready():
	
	if multiplayer.is_server():
		self.set_multiplayer_authority(1)
	else:
		self.set_multiplayer_authority(multiplayer.get_unique_id())
		
	if not is_multiplayer_authority():
		return
    anim_player.playback_default_blend_time = 0.3
	
	camera.current = true
	Input.mouse_mode = Input.MOUSE_MODE_CAPTURED

#rest of player code below here

I just know I am missing something small with the authority and can’t put my finger on it… I feel like the camera should be assigned correctly, but it just isn’t working. I can control 1 instance and not the other.

Here’s to hoping this makes sense and I am wanting to say my main problem is that maybe the MultiplayerSpawner just doesn’t save and replicate the authority as I think it should across clients?

Here are my MultiplayerSpawner settings for reference:

  1. Root node of the level is Sandbox
  2. I only want 4 Players to ever be able to spawn (testing with only 2 instances right now)
  3. My Player.tscn is referenced correctly
    image

This sets every client to think they are the multiplayer authority for every player. get_unique_id() is the multiplayer id for the client, so each player is spawned for each client and they declare authority with the client’s id, rather than the player’s.

I’d recommend setting the player.name to just the authority_id as a string. That way you can easily convert it back to a number

#PLAYER SCRIPT#

func _ready():
	set_multiplayer_authority(name.to_int())
3 Likes

Worked like a charm! Thank you a ton!
Also thanks for the explanation of how I was assigning the authority wrong so I could see and learn from my mistake :slight_smile:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.