Problem with MultiplayerSynchronizer and spawn object

Godot Version

4.2.1

Question

I have a problem with MultiplayerSynchronizer and when i spawn a Platform.
I use PackedScene to spawn my Platform that i can stand on it (which is a CharacterBody2D) but client can’t spawn or see what host spawned and if he try to spawn on host scene and it take the postion of host mouse to spawn it
Here’s the code:

extends Node2D

@export var platform: PackedScene

@rpc("any_peer", "call_local", "reliable")
func add_node(nodePath: String):
	if not multiplayer.is_server():
		return

	var newNode = load(nodePath).instantiate()
	newNode.global_position = get_global_mouse_position()

	add_child(newNode, false)
	print(newNode)
		
func _input(event):
	if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT and event.pressed:
		add_node.rpc(platform.get_path())

and this the error that shows:

get_node: Node not found: “Mulitplayer_test/platform_Manger/@CharacterBody2D@12/MultiplayerSynchronizer” (relative to “/root”).
<C++ Error> Method/function failed. Returning: nullptr
<C++ Source> scene/main/node.cpp:1638 @ get_node()

E 0:00:11:0108 process_simplify_path: Parameter “node” is null.
<C++ Source> modules/multiplayer/scene_cache_interface.cpp:76 @ process_simplify_path()

change it to:
add_node.rpc(platform.resource_path))

it didn’t wotk it shows error

Invalid get index ‘resource_path’ (on base: ‘Nil’).

did you put the scene on editor’s inspector?

like this:
image

yes think you for helping me out but that’s not the problem First i have to fix MultiplayerSynchronizer but i don’t know how

everytime i spawn it create new one

show the platform_Manager.gd’s attaching node’s editor inspector

replace with:
rpc("add_node",platform.resource_path)

how is your players being managed?, do you have some kind of autoload just for the players? it’s not spawning because the platform is only spawned to the host

i didn’t get, autoload ?

autoload is Singletons (Autoload) — Godot Engine (stable) documentation in English

what i mean is how do you spawn the player and do you keep the player in an array or something and can be accessed when needed?

something like this:

no, i don’t use Autoload
i use this code for joing and hosting:

extends Node2D

var peer = ENetMultiplayerPeer.new()
@export var player_scene: PackedScene


func _on_host_pressed():
	peer.create_server(135)
	multiplayer.multiplayer_peer = peer
	multiplayer.peer_connected.connect(_add_player)
	_add_player()
	

func _add_player(id = 1):
	var player = player_scene.instantiate()
	player.name = str(id)
	add_child(player)
	print(id)

func exit_game(id):
	multiplayer.peer_disconnected.connect(del_player)
	del_player(id)
func del_player(id):
	rpc("_del_player", id)

@rpc("any_peer", "call_local") func _del_player(id):
	get_node(str(id)).queue_free()

func _on_join_pressed():
	peer.create_client("localhost", 135)
	multiplayer.multiplayer_peer = peer

so basically to spawn the platform for both player, it needs to be similar to how you spawn the client and or host too. In the video i have sent, he needed to get the player by autoload (after setting all of connecting and have the player data) , then spawn the player in specified position

i found why it doesnt show now after testing it myself, global mouse position is true, it always spawn where the mouse is, so even for the client, it spawn where the mouse is, that is outside the window of client

1 Like

one time i tryed to fix it and i didn’'t know but it got synced and both can see and spawn but only on host mouse postion but i forgot :sweat_smile:

will this work for you?
newNode.global_position = get_viewport().get_mouse_position()

when i click to spawn it spawn on (x = 606.225) and not taking the mouse postion

i did use rpc and now i can spawn on host and it shows on client with no error but client can’t spawn on hist mouse postion he can only spawn on host mouse postion as you said and i tryed to use

but when i click to spawn on like (11, 11) it spawn on like (490, 249)

1 Like

One of my friends told me what is the problem with spawning object, so it was everyone is sending the global_mouse_position and that’s problem

and he told me how to fix that, by updating this:

to :

@rpc("any_peer","call_local")
func place(pos):
	
	var b = Platform.instantiate()
	b.global_position = pos
	var a = get_parent()
	a.add_child(b)

and when I want to spawn it, I use:

spawn_pos = get_global_mouse_position()
place.rpc(spawn_pos)

and that’s it, big thanks to my brother OmarTop :grin:

1 Like