Godot Version
4.6.2.stable - Arch Linux x64
Question
So I’ve made an inventory system a few months ago, you can put items in your LeftHand or RightHand, you can switch the items, put it in inventory, however it seems like the nodes dont sync across clients when created or moving from one parent to another parent. How do I fix this? I’ve tried making everything work on rpc functions, but that doesn’t work, and I’ve also tried MultiplayerSynchronisers, but that doesn’t work too.
Everything works fine on the client actually doing the thing, however on other clients the gun just doesn’t exist anymore to them, from what I’ve seen at least
I really need to clean up the node tree, a lot of the nodes in the whole player node tree (not just this section) are completely redundant as any code referencing them has been removed months ago.
Here’s the ‘hands’ node code:
extends Node3D
"""
<popcorn> Hey so, to anyone editing this, this is probably the worst code file I have ever made in my entire life.
"""
func _enter_tree() -> void:
set_multiplayer_authority(str(player.name).to_int())
for i in $MultiplayerSpawner.get_children():
i.spawn_function = g.rpc_instantiate
func spawn_function(DATA):
var foo = g.rpc_instantiate(DATA)
foo.player = player
@export var player : Player
@export var Inventory : Node3D
@onready var UI : GUI = player.get_node("CanvasLayer")
func switch_hand(hand, other_hand):
if hand.get_child_count() != 0:
hand.get_child(0).reparent(other_hand)
func Inventoryitem_name(num : int):
num = num - 1
if Inventory.get_child(num).get_child_count() > 0:
player.UI.Inventory.get_child(num).get_child(0).text = Inventory.get_child(num).get_child(0).item_name
else:
player.UI.Inventory.get_child(num).get_child(0).text = "-"
func fetchspawner(invorhand: bool, index : int):
if invorhand == false:
if index == 1:
return $MultiplayerSpawner/RightHand
elif index == 0:
return $MultiplayerSpawner/LeftHand
if invorhand == true:
if index == 0:
return $MultiplayerSpawner/InvSlot1
if index == 1:
return $MultiplayerSpawner/InvSlot2
if index == 2:
return $MultiplayerSpawner/InvSlot3
if index == 3:
return $MultiplayerSpawner/InvSlot4
if index == 4:
return $MultiplayerSpawner/InvSlot5
if index == 5:
return $MultiplayerSpawner/InvSlot6
@rpc("call_local")
func insert_item(item: PackedScene) -> Node:
var InventoryArray = Inventory.get_children()
if player.RightHand.get_child_count() == 0:
var new = fetchspawner(false, 1).spawn(item)
new.player = player
return new
elif player.LeftHand.get_child_count() == 0:
var new = fetchspawner(false, 0).spawn(item)
new.player = player
return new
elif InventoryArray[0].get_child_count() == 0:
var new = fetchspawner(true, 0).spawn(item)
new.player = player
return new
elif InventoryArray[1].get_child_count() == 0:
var new = fetchspawner(true, 1).spawn(item)
new.player = player
return new
elif InventoryArray[2].get_child_count() == 0:
var new = fetchspawner(true, 2).spawn(item)
new.player = player
return new
elif InventoryArray[3].get_child_count() == 0:
var new = fetchspawner(true, 3).spawn(item)
new.player = player
return new
elif InventoryArray[4].get_child_count() == 0:
var new = fetchspawner(true, 4).spawn(item)
new.player = player
return new
elif InventoryArray[5].get_child_count() == 0:
var new = fetchspawner(true, 5).spawn(item)
new.player = player
return new
else:
return null
@rpc("call_local")
func loadslot(num : int):
num = num - 1
if Input.is_action_pressed("both_hands_current"): return
if Inventory.get_child(num).get_child_count() > 0:
if player.RightHand.get_child_count() > 0:
if player.LeftHand.get_child_count() > 0:
var HandItem = player.LeftHand.get_child(0)
var InventoryItem = Inventory.get_child(num).get_child(0)
HandItem.make_current(false)
InventoryItem.make_current(true)
HandItem.reparent(Inventory.get_child(num))
InventoryItem.reparent(player.LeftHand)
else:
var InventoryItem = Inventory.get_child(num).get_child(0)
InventoryItem.make_current(true)
InventoryItem.reparent(player.LeftHand)
else:
var InventoryItem = Inventory.get_child(num).get_child(0)
InventoryItem.make_current(true)
InventoryItem.reparent(player.RightHand)
elif Inventory.get_child(num).get_child_count() <= 0:
if player.RightHand.get_child_count() > 0:
if player.LeftHand.get_child_count() > 0:
var HandItem = player.LeftHand.get_child(0)
HandItem.make_current(false)
HandItem.reparent(Inventory.get_child(num))
else:
var HandItem = player.RightHand.get_child(0)
HandItem.make_current(false)
HandItem.reparent(Inventory.get_child(num))
elif player.LeftHand.get_child_count() > 0:
var HandItem = player.LeftHand.get_child(0)
HandItem.make_current(false)
HandItem.reparent(Inventory.get_child(num))
func _process(delta : float):
if not is_multiplayer_authority():
return
#1
Inventoryitem_name(1)
Inventoryitem_name(2)
Inventoryitem_name(3)
Inventoryitem_name(4)
Inventoryitem_name(5)
Inventoryitem_name(6)
if Input.is_action_just_pressed("Inventory Slot 1"):
loadslot.rpc(1)
if Input.is_action_just_pressed("Inventory Slot 2"):
loadslot.rpc(2)
if Input.is_action_just_pressed("Inventory Slot 3"):
loadslot.rpc(3)
if Input.is_action_just_pressed("Inventory Slot 4"):
loadslot.rpc(4)
if Input.is_action_just_pressed("Inventory Slot 5"):
loadslot.rpc(5)
if Input.is_action_just_pressed("Inventory Slot 6"):
loadslot.rpc(6)
if player.LeftHand.get_child_count() > 0:
player.UI.LeftHand.get_child(0).text = str(player.LeftHand.get_child(0).item_name)
else:
player.UI.LeftHand.get_child(0).text = "-"
if player.RightHand.get_child_count() > 0:
player.UI.RightHand.get_child(0).text = str(player.RightHand.get_child(0).item_name)
else:
player.UI.RightHand.get_child(0).text = "-"
if Input.is_action_just_pressed("switch hands"):
switch_hands_function.rpc()
if Input.is_action_pressed("other_hand_current"):
if player.LeftHand.get_child_count() > 0:
if player.LeftHand.get_child_count() > 0:
player.UI.LeftHand.get_child(0).text = str(player.LeftHand.get_child(0).item_name)
if player.RightHand.get_child_count() > 0:
if player.RightHand.get_child_count() > 0:
player.UI.RightHand.get_child(0).text = str(player.RightHand.get_child(0).item_name)
player.UI.RightHandCurrent.hide()
player.UI.LeftHandCurrent.show()
if player.LeftHand.get_child_count() > 0:
if player.LeftHand.get_child_count() != 0:
player.LeftHand.get_child(0).use_delay = 0
player.LeftHand.get_child(0).current = true
if player.RightHand.get_child_count() > 0:
if player.RightHand.get_child_count() != 0:
player.RightHand.get_child(0).current = false
elif Input.is_action_pressed("both_hands_current"):
if player.LeftHand.get_child_count() > 0:
if player.LeftHand.get_child_count() > 0:
player.UI.LeftHand.get_child(0).text = str(player.LeftHand.get_child(0).item_name)
if player.RightHand.get_child_count() > 0:
if player.RightHand.get_child_count() > 0:
player.UI.RightHand.get_child(0).text = str(player.RightHand.get_child(0).item_name)
player.UI.RightHandCurrent.show()
player.UI.LeftHandCurrent.show()
if player.LeftHand.get_child_count() > 0:
player.LeftHand.get_child(0).current = true
if player.RightHand.get_child_count() > 0:
player.RightHand.get_child(0).current = true
else:
if player.LeftHand.get_child_count() > 0:
if player.LeftHand.get_child_count() > 0:
player.UI.LeftHand.get_child(0).text = str(player.LeftHand.get_child(0).item_name)
if player.RightHand.get_child_count() > 0:
if player.RightHand.get_child_count() > 0:
player.UI.RightHand.get_child(0).text = str(player.RightHand.get_child(0).item_name)
player.UI.RightHandCurrent.show()
player.UI.LeftHandCurrent.hide()
if player.LeftHand.get_child_count() > 0:
if player.LeftHand.get_child_count() > 0:
player.LeftHand.get_child(0).use_delay = 0
player.LeftHand.get_child(0).current = false
if player.RightHand.get_child_count() > 0:
if player.RightHand.get_child_count() > 0:
player.RightHand.get_child(0).current = true
@rpc("call_local")
func switch_hands_function():
if player.LeftHand.get_child_count() > 0 == true:
if player.RightHand.get_child_count() > 0:
switch_hand(player.LeftHand,player.RightHand)
switch_hand(player.RightHand,player.LeftHand)
else:
switch_hand(player.LeftHand,player.RightHand)
return
elif player.RightHand.get_child_count() > 0 == true:
if player.LeftHand.get_child_count() > 0 == false:
switch_hand(player.RightHand,player.LeftHand)
elif player.LeftHand.get_child_count() > 0:
switch_hand(player.LeftHand,player.RightHand)
switch_hand(player.RightHand,player.LeftHand)
return
