+1 vote

I wanted to create instance nodes (like players) only on the master and share that node through rpc as parameter. Finally I called addChild in a sync function on every game instance. It does not work.
What is the reason behind that? I am just interested in that.

It seems like it is mandatory to create new instances on every game instances.

My twisted concept:
OnPlayer connection event:

func connected(id):
    if (!is_network_master()):
        return

    instPlayer(netId)
    instPlayer(id)


func instPlayer(id):
    player = player_scene.instance()
    player.set_network_master(id)
    player.setLabel("player "+String(id))
    if (id == 1):
        player.position = Vector2(300,200)
    else:
        player.position = Vector2(100,200)
    rpc("addPlayer", player)

sync func addPlayer(p):

    add_child(p)
in Engine by (300 points)

1 Answer

+3 votes
Best answer

You'll probably be very disappointed to hear that...
nodes can not be passes through RPC :(

First you will need to send data necessary to create a new node. In my code it's this:

remote func insertUnit(unitFilename, path, unitName):
    var unit = load(unitFilename).instance()
    unit.set_name(unitName)
    get_node(path).add_child( unit )

unitFilename is a path to .tscn file, path is a path in scene tree, name is a name of a node.

After a unit is created on client's game server calls this function of my Unit.gd script:

func sendToClient(clientId):
    var unitData = {
        position = get_position(),
        nameLabelText = get_node(UnitNameLabel).get_text()
    }

    rpc_id(clientId, "copyUnit", unitData)

This is a function (also in Unit.gd) that will be called at client's side

remote func copyUnit(unitData):
    set_position(unitData.position)
    get_node(UnitNameLabel).text = unitData.nameLabelText

it initializes a unit that was created via insertUnit before.

And here is a function that sends all units from server to a client:

func sendToClient(clientId):
    var units = self.get_children()

    for unit in units:
        var unitFilename = unit.get_filename()
        var path = get_path()
        rpc_id(clientId, "insertUnit", unitFilename, path, unit.get_name())
        unit.sendToClient(clientId)
by (2,274 points)
selected by
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.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.