Multiplayer: How to synchronize queue_free() without MultiplayerSpawner node?
Hello. Let's just say for simplicity sake that I have a system that looks something like this:
I have a Raycast3D attached to the Camera and dynamically spawned Rigidbody3D nodes in game. These Rigidbody3D nodes have a script attached called Item.gd that contains only one method remove_item() that simply calls queue_free(). Also player nodes have client authority.
Camera script - this script is queued_free() if you have no authority over the node*
func _input(_event):
if not Input.is_action_just_pressed("interact"):
return
var item = interaction_raycast.get_collider() as Item
if item:
item.remove_item.rpc()
Item.gd:
extends RigidBody3D
class_name Item
@rpc("any_peer", "call_local")
func remove_item():
queue_free()
Now for like 80% of the time this script works fine, but at some point I run into these 5 errors on peers side:
I suppose somewhere along the way the reference to the same Item is for some reason lost, cause the result is that the peer doing the interaction has the Item despawned, but not the other peer. As the debugger says, it cannot find the node. I know this could be a result of a queue_free somewhere along the way but there's no other queue_free, I triple checked before posting this.
The only workaround I can really think of right now is to have some kind of a globally accessible container node containing all items and have my own unique id system and call a global function using find_node by self-made id and queue_free it after finding it, but it really sounds like an overkill for such a simple problem. Not to mention it would be annoying to manually place items via inspector on map that way too.
*I want each peer to only contain the one script that is on their own camera, if I don't do that all scripts will listen for all players inputs and I'd be able to remove nodes from other peers perspective and vice versa. I'd be really surprised if this is the reason for this issue but I figured I should mention it regardless.