+1 vote

After a server is created the the player that owns the server joins the game and and the server stores this data on his side.
Later on another player joins the server that need the data that says [The server owner is in game].

It's easy to send that data when both players connected because the server can send it directly to another player.

what is the best way to do it?

Edit: Thats how I did that

func _ready():
    get_tree().connect("network_peer_connected", self, "_player_connected")
    get_tree().connect("network_peer_disconnected", self, "_player_disconnected")
    players[1] = 1

func _player_connected(id):
    if get_tree().is_network_server():
        players[id] = id
    else:
        rpc_id(1,"sync_func")

func _player_disconnected(id):
    players.erase(id)

remote func sync_func():
    for peers in get_tree().get_network_connected_peers():
        if peers != 1:
            rpc_id(peers,"sync_players",players)

remote func sync_players(player_data):
    players = player_data
asked Apr 5 in Projects by axilirate (23 points)
edited Apr 6 by axilirate

1 Answer

0 votes

For my game ,I also ran into this problem and i solved this by using authoritarian model.

when a player joins a server , the player requests the server to provide data about players playing the game.

######Network.gd
func ready():
    if is_network_master():
        rpc_id(1,"serv_get_player_data", my_id)

remote func serv_get_player_data(new_player_id):
    player_data_dict = { pname = "", pos = Vector2() , hp = 0  }
    var final_data = Array()

    var players = get_tree.get_nodes_in_group("Players")
    for i in players:
        #duplicate dictionary
        var data = player_data_dict.duplicate(true)
        data.pname = i.pname
        data.pos = i.position
        data.hp = i.hp
        final_data.append(data)

    #send all player data to new player
    rpc_id(new_player_id, "new_player_recieve_data",final_data)


remote func new_player_recieve_data(data):
    for i in data:
        var p = Player.instance()
        p.position = i.pos
        p.pname = i.pname
        p.hp = i.hp

        #spawn existing player
        add_child(p)
answered Apr 5 by supper_raptor (719 points)
edited Apr 5 by supper_raptor

how are you able to execute this?

 if get_tree().is_network_server():
        rpc_id(1,"serv_get_player_data", my_id)

get_tree().is_network_server(): means that it's the server and rpc_id(1) means you execute it on yourself which returns an error.

rpcp: RPC 'do' on yourself is not allowed by selected mode.

extremely sorry

it should be isnetworkmaster() instead of gettree().isnetwork_server():

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.