This is what I tried.

puppet var food = 10

func addfood():
	food += 20

func calc_food():
	rpc("sync_food")

remotesync func sync_food():
	rset("food", food)
	$Foodcounter.text = str(food)

func _on_Button_pressed():
	addfood()
	calc_food()

What happens is it works fine. Except the first time I press it the client has 10 Food, and the server has 30 food. After that every time I press the button there is a 20 food discrepancy.

I'm assuming the information is sent before the addfood() func has run and that's why the server is always ahead by 20 food, but I can't figure out how to solve this.

Also, if there is an easy way to just code. client food = server food .

Then let me know.

I managed to fix it. I'm leaving a small explanation for anyone else with a similar problem.

I removed the food counter and instead made it a new function. Then added a rpc call to it. So the whole code looks like this.


puppet var food = 10
                
func addfood():
	food += 20
        
func calc_food():
	rpc("sync_food")
                
remotesync func sync_food():
	rset("food", food)
	rpc("sync_counter")
        
remotesync sync_counter():
	$Foodcounter.text = str(food)
              
func _on_Button_pressed():
	addfood()
	calc_food()

I still don't understand why this one works and the previous one doesn't.

So what's happening here. When I press the button, the addfood puts +20 on the var food in server side. Then calc_food(this func is probably redundant) gets called, it calls a remote func sync_food that executes both client and server side which changes the var food, to the current food. Since puppet var food can only be changed by server, the client now has the same food as the server. Then the visual $Foodcounter node shows the current food in text.

The only difference in my code is that the $Foodcounter.text = str(food) gets called in it's own function instead of in the same function right after syncing food. Why this makes a difference I still don't know.

I consider this problem solved although I don't 100% get what was causing the problem.

2 years later