Networking: Master creates node instances and share it through RPC

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

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()):

func instPlayer(id):
	player = player_scene.instance()
	player.setLabel("player "+String(id))
	if (id == 1):
		player.position = Vector2(300,200)
		player.position = Vector2(100,200)
	rpc("addPlayer", player)
sync func addPlayer(p):
:bust_in_silhouette: Reply From: hilfazer

You’ll probably be very disappointed to hear that…
nodes can not be passes through RPC :frowning:

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()
	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 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 that will be called at client’s side

remote func copyUnit(unitData):
	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())