• Godot Help
  • Godot multiplayer set authority not working

Hi,
Im relatively new to Godot and I wanted to test how easy/hard it is to implement very simple local multiplayer connection. However no matter what I do, nothing works properly. Ive been trying different code for hours but nothing works there is always some problem.

This is my current code for the Game scene:

extends Node2D

var ip = "127.0.0.1"
var port = 8910

var player_scene = preload("res://Player.tscn")

var peer 

func _ready():
	multiplayer.connected_to_server.connect(on_server_connection)
	multiplayer.peer_connected.connect(on_peer_connection)


func _on_host_pressed():
	peer = ENetMultiplayerPeer.new()
	peer.create_server(port)
	multiplayer.multiplayer_peer = peer


func _on_join_pressed():
	peer = ENetMultiplayerPeer.new()
	peer.create_client(ip, port)
	multiplayer.multiplayer_peer = peer


func _on_start_pressed():
	start_game.rpc()

@rpc("any_peer", "call_local")
func start_game():
	var instance
	instance = player_scene.instantiate()
	instance.set_multiplayer_authority(1)
	$Players.add_child(instance)
		
	for peer_id in multiplayer.get_peers():
		instance = player_scene.instantiate()
		instance.set_multiplayer_authority(peer_id)
		$Players.add_child(instance)
	$UI.visible = false

func on_server_connection():
	print("peer succesfully connected to a server!")

func on_peer_connection(id):
	print(str(id) + " connected to other peers!")

And this is my code for the Player scene:

extends Sprite2D

func _ready():
	global_position.x = randi_range(50, 400)
	global_position.y = randi_range(50, 400)
	print("my id is: " + str(multiplayer.get_unique_id()))

func _physics_process(delta):
	if is_multiplayer_authority():
		var direction = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
		global_position += direction * 100 * delta

Player scene have MultiplayerSyncronizer and Game scene have multiplayer spawner.

I also have Host/join buttons and start button. I open two instances of the game, on 1 I press host on the other I press join and then on the host I press start. 2 sprites appear on both instances so this part works, I can also move the sprite on the host instance, however on the client instance I just cant move the node. Seems like some kind of problem with the authority. What am I doing wrong? How could I fix this?

22 days later
9 days later

instance.set_multiplayer_authority(1)

This is your problem. Both the host and client call this, but the client's peers is also just the server.

Change this line to...
instance.set_multiplayer_authority(multiplayer.get_unique_id())

4 months later

func _on_area_2d_body_entered(body):

if body.has_method("Playerr"):# && CanPick == true :

var random_index = randi_range(0, Guns.size() -1)
#var instance = Guns[random_index].instantiate()
var instance = Famas.instantiate()
#body.get_node($GunRotation).
var Authority = body.get_node(".").get_multiplayer_authority()
instance.set_multiplayer_authority(Authority)
#instance.set_multiplayer_authority(multiplayer.get_unique_id())
print("   Guns   Authority  :",body.multiplayer.get_unique_id())
body.get_node(".").add_child(instance)



var Authority = body.get_node(".").get_multiplayer_authority()
instance.set_multiplayer_authority(Authority)

this will fix the issue to set a instance to Authority !............