Sync a variable between multiplayer peers doesnt work

Godot Version

4.2.2

Question

hello i am trying to sync a variable named “test” and here is my code that i tried

it works like this, gets updates for every peer in multiplayer:

@rpc("any_peer","call_local","reliable")
func testcheck():
	test +=1

func _physics_process(_delta):
	rpc("testcheck")
	print(test)

when i press the input “yes” on two peers the “test” variable goes from 2 because i pressed “yes” input one time on both peers

BUT when i try to call “testcheck” in if condition it only updates locally like this:

var cond : bool = true
@rpc("any_peer","call_local","reliable")
func testcheck():
	test +=1

func _physics_process(_delta):
	if Input.is_action_just_pressed("yes") && cond:
		rpc("testcheck")
		print(test)

when i press the input “yes” on two peers the “test” variable goes from 0 to 1 each of the peers but it must show 2 instead of two 1

so on conclusion the first code sets “test” variable for every peer on multiplayer if test is increased by 5 it is 5 on every peer

but on second code it updates the “test” variable locally even if i call from rpc()

what am i doing wrong? the value needs to be updated when conditions met. thank you

I don’t think this is an RPC issue but more of a tree and instance issue.

I suspect you have multiple node instances calling their remote counterparts but not the other instances

Player1 --> RPC --> player2
Root --------------> Root
- p1 RPC +1 ----------> - p1 +1
- p2 0     -----------> - p2 0

Where your first example seems to work because it is called blindly in the physics process by all nodes.

The test variable needs to either exist in one place like root,

Player1 
Root.rpc() (+1 local)
- p1
- p2


Player2
Root  ( +1 remote)
- p1
- p2

Or if the variable exists on the player node you need to use a static variable that is shared between multiple instances of the nodes.

Player1 
Root
- p1.rpc() ( static +1 local)
- p2 static +1


Player2
Root  
- p1( remote static +1 )
- p2 static +1

thank you. calling it in root node worked

the variable was inside of same gdscript i moved it inside of game controller which is in main scene

when i called that variable from game controller it synced like charm

1 Like