cant save data in rpc()

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By phucotaku

look at the code below, I don’t know how to describe my problem because I don’t know where it comes from

extends Node2D
var i=10
func _ready():
	if get_tree().is_network_server():
		print(i)
		rpc("test")
		$Timer.start(5)
remote func test():
	i=73
	print(i)
func _on_Timer_timeout():
	if get_tree().is_network_server():
		print(i)

Output:
10
73
10

I want to change the value of variable i through the function rpc() but it dont save viriable, ,i think because rpc function is executed late i added signal timeout but it’s not how i can save it?, I have created server and client normally, no error and the above code also does not return error

:bust_in_silhouette: Reply From: godot_dev_

If your running both the client and server on the same Godot Editor, (why both the server and client print to the same output console), the ouput is exactly what your code is telling it to do. Your logic can be summarized as follows:

  1. Server prints i so ‘10’ is output to console (server : i= 10 , client : i = 10)
  2. Server requests client to change i to 73 and then server sleeps for 5 seconds (server : i= 10 , client : i = 10)
  3. Client executes the test (triggered by the server’s call to rpc) function and prints i, so 73 is output to console (server : i= 10 , client : i = 73)
  4. Server wakes up from it’s sleep and prints i, outputting 10 ( server : i= 10 , client :i = 73) )

I am not quite sure what you intend to do, but I don’t see an error with your code. I suggest you carefully think about what you want to do to solve your problem. The variable i is changed by the rpc call, otherwise 73 would not have been printed. I recommend you add “client” or “server” to the print out to indicate who is printing what.
So your output console would appear like follows:
server: 10
client: 73
server: 10

Ohhhhhh, I understand!!!, thank you man!

phucotaku | 2022-08-02 17:28