• Godot Help
  • I need help syncing a bool variable across clients

Hey, all!
I've been having some trouble with my multiplayer game. I've been using this tutorial -
My code is posted below. Any help would be appreciated. Thanks!

I have an autoload script (GameManager) that holds one variable: Players - an empty dictionary.
In my Network script, attached to the main menu, I have a function (Send_Player_Information) that syncs player information across all peers. This syncs 3 parameters (1 empty string, 1 multiplayer unique id, and 1 boolean variable (is_vampire)). 2 parameters are synced perfectly, but I can't get the third to work right. It's probably staring me in the face but I just can't find what's wrong.

I have a button that switches a bool variable (is_vampire) between true and false.
I have added this variable to the 'Send_Player_Information' function (see below). Everything works perfectly on the host, but the client side just resets to the default variable value, even if I have changed the value in-game through a button press. I have tested everything before the 'Send_Player_Information' function and the variable's value is changed on command, but when the function is called, the value received is different than the value I thought I sent.

Please let me know if you need any more info! Thanks in advance!


`


extends Control

@export var network = "127.0.0.1"
@export var PORT = 9999
var max_players = 10
var peer
var is_vampire = true

# Called when the node enters the scene tree for the first time.
func _ready():
    multiplayer.peer_connected.connect(peer_connected)
    multiplayer.peer_disconnected.connect(peer_disconnected)
    multiplayer.connected_to_server.connect(connected_to_server)
    multiplayer.connection_failed.connect(connection_failed)
    multiplayer.server_disconnected.connect(server_disconnected)

func server_disconnected():
    print("Server Disconnected")
    get_tree().quit()

func peer_connected(id):
    print("Player Connected " + str(id))

func peer_disconnected(id):
    print("Player Disconnected " + str(id))
    GameManager.Players.erase(id)
    var players = get_tree().get_nodes_in_group("Player")
    for i in players:
    	if i.name == str(id):
	    	i.queue_free()

func connected_to_server(): 
	print("Connected to server!")
# rpc the host with the name and unique id of the player who joined
    Send_Player_Information.rpc_id(1 , "" , multiplayer.get_unique_id() , is_vampire)

func connection_failed(): print("Couldn't connect!")


# update the player information (called from rpc when a new player joins the server)
@rpc("any_peer", "reliable")
func Send_Player_Information(name, id, vampire): 
    # if the game manager does not have this id
    if not GameManager.Players.has(id):
	    GameManager.Players[id]={
		    "name" : name,
		    "id" : id,
		    "vampire" : vampire
	    }

    if multiplayer.is_server():
	    for i in GameManager.Players:
		    Send_Player_Information.rpc(GameManager.Players[i].name, i, GameManager.Players[i].vampire)

func host_game():
    peer = ENetMultiplayerPeer.new()

    # port to listen on, max number of players
    var error = peer.create_server(PORT, max_players)
    if error != OK:
	    print("cannot host " + error)
	    return


    multiplayer.set_multiplayer_peer(peer)
    # send host name and id
    Send_Player_Information("", multiplayer.get_unique_id(), is_vampire)

    print("waiting for players")

func join_game():
    peer = ENetMultiplayerPeer.new()
    # first the IP address, then the port to listen on (1--9999)
    peer.create_client(network, PORT)
    multiplayer.set_multiplayer_peer(peer)
    print("join button - vampire is " + str(is_vampire))


# everybody gets this rpc, including the player who calls it
@rpc("any_peer", "call_local")
func start_game(): 
    var scene = load("res://level.tscn").instantiate()
    get_tree().get_root().add_child(scene)
    self.hide()


func _on_start_button_pressed():
    start_game.rpc()


func _on_vampire_button_toggled(toggled_on):
    if toggled_on: 
	    $selection_container/vampire_button.text = "Human"
	    is_vampire = false
	    print("button pressed. this player's vampirical nature is " + str(is_vampire))
    else: 
	    $selection_container/vampire_button.text = "Vampire"
	    is_vampire = true
	    print("button pressed. this player's vampirical nature is " + str(is_vampire))

`