Spawning Items in Multiplayer wont appear for other clients

Godot Version

4.2.1

Question

I’m working on a multiplayer game with an inventory system similar to Minecraft. When the player drops an item it appears in front of them for other players to pick up.
But, for some reason it only appears on the players end and not the rest of the clients. Can anyone tell me what I’m doing wrong?

The new_core looks to be a node or scene of some sort. RPC’s cannot serialize an object like that to send over the network. To fix your code you should pass
base type like enum/int/string/dictionary/array that represents the new_core class and build everything within that function. Or you could use the multiplayerSpawner class and have it watch the parent where you drop the object.

Update:
Since the class already exists you may need to do some sort of search on the client side to do the same duplication… That is a tough call.

The last time I checked, in regards to sending node via RPC, is that they get replaced in the packet with a unique reference number. You can use that number on the client to search for the node, but as your intention stands that node will not exist to be found. I wouldn’t recommend this since there is no guarantee that the reference numbers can be synced reliably between server and client.

Unfortunately it seems you will need to re-think how the node is spawned, and I highly recommend using the MultiplayerSpawner nodes.

1 Like

Ah, I think i found something you will need to set the server to allow_object_decoding

This may allow you to send the node like you do, but be warned this may lead to security risks.

1 Like

Could you do this?

func add_slot_one(slot: Node):
  if slot == null:
    if core.stack > 0:
      rpc("drop_item")

@RPC(blah, blah, blah)
func drop_item():
  var new_core = core.duplicate()
  new_core.stack = 1
  core.stack -= 1
  var drop = preload(...)
  ... etc

what i learned from this (Problem with MultiplayerSynchronizer and spawn object - #21 by zdrmlpzdrmlp) is that, you will need to send the global position of dropped item before calling rpc and put the global position as a parameter in rpc to be used for the destined item’s global position in order to spawn the item for both player

another thing is to make the
@rpc("any_peer","call_local","reliable",1)
to just
@rpc("any_peer","call_local","reliable")

1 Like

Removing the one changes the RPC channel to 0, there is no real difference.

What I typically do to sync a new object is to use the MultiplayerSpawner to take care of instancing the node on the client, then a MultiplayerSynchronizer to sync properties “on spawn” in the replication config. Like position and whatever else I need.

1 Like

Take a look at this Multiplayer in Godot 4.0: Scene Replication

Thank you for your time and Information and sorry for such a delayed response. I’ve pretty much got a idea of what I should be doing. I’m just confused on how MultiplayerSpawner works. My understanding is that you just simply Instance the scene and use the spawn function instead of addChild to load the child in. Am I missing a step?

It’s a little different, you can use the custom spawn function but all that is required for the spawner is a parent to watch for new children and a list of spawnable scenes that you wish to replicate on clients when they are added or removed.

Then you just add/remove child on the parent as usual. The name will be synchronized but the scene will be in an initial state so a MultiplayerSynchronizer is needed to match properties as needed between the host and client. Like position and rotation and velocity

You can also chain spawners to make a deep tree. Main hs Level spwner, level has character spwner, character has item spawner. Etc.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.