I can't get the username datas right [MULTIPLAYER]

Godot Version

4.3

Question

Hey guys I am tying to make a multiplayer game but I just can’t get the datas of a player get stored in a global dictionary when they join.

A screenshot of me hosting a game with a username


output:
image

A screenshot of me joining the game hosted by John Doe.


output:
image

The Problem:
As you can see I was trying to join the game with the username “Jane Doe” But the username was later stored as the host’s name “John Doe” I will attatch the script below for you guys to have a look:

Script for Node_3D:


@onready var main_menu = $"CanvasLayer/Main Menu"
@onready var address_entry = $"CanvasLayer/Main Menu/MarginContainer/VBoxContainer/address"
const Player = preload("res://player.tscn")
@onready var username = $"CanvasLayer/Main Menu/MarginContainer/VBoxContainer/usrname"
const PORT = 29737

var enet_peer = ENetMultiplayerPeer.new()

func _on_host_pressed():
	main_menu.hide()
	
	enet_peer.create_server(PORT)
	multiplayer.multiplayer_peer = enet_peer
	multiplayer.peer_connected.connect(add_player)
	multiplayer.peer_disconnected.connect(remove_player)
	add_player(multiplayer.get_unique_id())

func remove_player(peer_id):
	var player = get_node_or_null(str(peer_id))
	if player:
		player.queue_free()

func _on_join_pressed():
	var data = Datas.datas
	main_menu.hide()
	enet_peer.create_client(address_entry.text, PORT)
	multiplayer.multiplayer_peer = enet_peer
	#data[multiplayer.get_unique_id()] = {"Name" : username.text}

func add_player(peer_id):
	var data = Datas.datas
	var player = Player.instantiate()
	player.name = str(peer_id)
	#if str(peer_id) == '1':
	data[peer_id] = {"Name" : username.text}
	add_child(player)

Script for the global data:

var datas = {}
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	pass
#	print(datas)
#	print()# Replace with function body.


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
	print(datas)

Let me know if you guys need any additional information! Thank you all!

Its commented out for the joining player click

Also global data is not shared by processes. If you desire the name to be visible to all peers you will need to devise another mechanism to do so. It will have to leverage a multiplayer synchronizer or rpc.

2 Likes

Ahha! Anything that could be used for synchronizing the data to all processes?

Yes, i like multiplayer synchronizers.

I devised a synchronizer to setup something along the these lines.

You spawn a player data synchronizer that can be authed to the owning player. Then whenever the player changes skin color or name it will be synched to all peers.