instance a node as a child in multiplayer.

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

What is the best way to instance a node in multiplayer for everyone to see it.
if I instance a new node in a player node the only one who can see it is the network_master, but other players cannot.

:bust_in_silhouette: Reply From: klaas

I use a factory-programming-pattern for that.

This is a centralized object for creating any type of object.
I integrate this as a “submodul” in my NetworkManager.
Whenever i want to create something i call:

Where app is a autoloaded singleton as base for all global calls.
and all components like NetworkManager register to this app-object.

# this is my custom component system ... ignore this
extends NetworkModul
class_name NetworkFactory

func _init():
	app.register_component( self, "NetworkFactory", ["NetworkManager"] )
var NetworkManager:NetworkManager

var instance_count = 0

var instances:Array
var instance_cache = {}
var instance_preload = {}

var instance_path = {
	"player": "res://gameobjects/player/Character.tscn",
	"steam": "res://gameobjects/fx/steam/Steam.tscn",
	"cable": "res://gameobjects/consumables/cable/Cable.tscn",
	"FlowPipe": "res://gameobjects/FlowNodes/FlowPipeline/Pipe/Pipe.tscn"

#------------------------ actually create things
func __make( what ):
	var a
	if instance_cache.has(what):
		#use allready loaded from cache
		a = instance_cache[what].instance()
		#load instance
		var o = load(instance_path[what])
		instance_cache[what] = o
		a = o.instance()
	return a

#------------------------ get rid of things
func dispose( inst ):
	rpc("net_free", inst.get_path() )

#------------------------ actually get rid of things
remotesync func net_free( instPath ):
	print_debug("network-factory: free instance "+instPath)
	var inst = get_node( instPath )
	instances.erase( inst )

#------------------------ the function to call when creating something
func create( what, parent, name=null, on_ready=null ):
	print_debug("network-factory: create instance of "+what+" in "+parent.get_path())
	assert( instance_path.has(what), "NetworkFactory has no instance path of \""+what+"\"")
	var a = __make( what )
	if not name:
		name = "instance_"+str(instance_count)
		instance_count += 1 = name
	if on_ready:
		a.connect("ready",on_ready, "call_func", [], CONNECT_ONESHOT)
	rpc("net_create", what, parent.get_path(), name )
	return a

#------------------------ gets called on the other clients
remote func net_create( what, parentPath, name ):
	var parent = get_node(parentPath)
	var a = __make( what )
	var icount = int(name)
	if icount > instance_count:
		instance_count = icount = name