Scene_multiplayer.send_auth problem

Godot Version

4.3

Question

I want to use scene multiplayer but when using the send_auth method I get an error:
E 0:00:00:0281 main.gd:20 @ set_peer(): Condition “multiplayer_peer.is_null() || multiplayer_peer->get_connection_status() != MultiplayerPeer::CONNECTION_CONNECTED” is true. Returning: ERR_UNCONFIGURED
<C++ Source> modules/multiplayer/scene_multiplayer.cpp:460 @ send_auth()
main.gd:20 @ set_peer()
main.gd:11 @ _ready()

my code:

extends Node

const PORT: int = 3724
const ADDRESS: String = "ws://127.0.0.1:{0}"

@onready var peer: WebSocketMultiplayerPeer = WebSocketMultiplayerPeer.new()
@onready var scene_multiplayer: SceneMultiplayer = SceneMultiplayer.new()

func _ready() -> void:
	set_scene_multiplayer()
	set_peer()

func set_scene_multiplayer() -> void:
	get_tree().set_multiplayer(scene_multiplayer)
	multiplayer.connected_to_server.connect(_on_connected_to_server)

func set_peer() -> void:
	peer.create_client(ADDRESS.format([PORT]))
	scene_multiplayer.multiplayer_peer = peer
	scene_multiplayer.send_auth(1, "test".to_utf8_buffer())

func _on_connected_to_server() -> void:
	print("connected")

You need to setup an auth callback and listen for authenticating signal.

I set it up but on the server side,

They both need their own auth callback.
And the client send_auth can be called during that callback.
Right now you are sending auth before the connection is established. create_client is asynchronous.

This is the generel sequence

func auth_callback(id:int, data:PackedByteArray):
	print("authing %i %s" % [multiplayer.get_unique_id(), data.get_string_from_ascii()])
	if not multiplayer.is_server():
		multiplayer.send_auth(id, "secret".to_ascii_buffer())
		multiplayer.complete_auth(id)
	else:
		if data.get_string_from_ascii() == "secret":
			print("auth complete")
			multiplayer.complete_auth(id)
			

func _on_peer_authenticating(id):
	print("peer_authenticating")
	multiplayer.send_auth(id, "password?".to_ascii_buffer())

You can continue to talk back and forth during the auth callback, but once it is complete both client and server need to call complete_auth.

1 Like