Objects spawned for the room
Networking.Instantiate(template, position, rotation) makes a copy of an object for the whole room: every player sees it, players who arrive later too, and its scripts live as those of an object of the scene (Awake, Start, synced state, network events, ownership). Networking.Destroy(copy) removes it for everyone. There is no limit on the count of copies: only the 120 messages a second each player may send to the network of the scripts slow a burst down.
Object.Instantiate makes a copy for the local player only, not synchronized (objects and components).
Mark the template
- Make the object to copy in the scene, with its children, components and FL# Script components. Keep it inactive if it should not live on its own: the copies are always active.
- Add the Network Template component to it (Add Component, NodeWorld, Network Template). The editor gives it an id, the same for every player and for the server.
- Give the template to your script in a
GameObjectfield.
Spawn and remove
using NodeWorld.Flux;
public class Launcher : FluxScript
{
public GameObject ball; // the template (Network Template component)
protected override void OnInteract()
{
var copy = Networking.Instantiate(ball, transform.position + transform.forward, transform.rotation);
copy.GetComponent<Rigidbody>().velocity = transform.forward * 6f;
}
}
public class Ball : FluxScript
{
protected override void Start() => Networking.SyncTransform(gameObject, 20f);
protected override void OnCollisionEnter(Collision collision)
{
if (Networking.IsOwner(gameObject) && collision.collider.gameObject.name == "Goal")
Networking.Destroy(gameObject);
}
}- The copy exists at once on the player who spawns it. The server gives it a network id of its own in the room (
~1,~2…; the objects under it get~1/0,~1/0/1… by their order) and tells every player. Until the id comes back, the synced state and the network events its scripts send wait, then go. - Its owner is the player who spawned it. A copy spawned by a script of the server belongs to the master of the room.
Networking.Destroyis for the owner of the copy, the master, or a script of the server. The copy's scripts getOnDestroyeverywhere.Networking.IsSpawned(gameObject)says whether an object is a spawned copy.
When a player leaves
The copies of a player who leaves stay in the room and go to the master: their scripts hear OnOwnershipChanged with the master's id, as for any object of a player who leaves (synced objects and values).
Players who arrive later
A player who arrives gets the copies alive in the room, where their owner last sent them, with their owner and the last synced state of their scripts, before their first Update.
On the server
The world's scripts on the server run the copies' scripts too (those that are not [ClientOnly]), and may spawn copies themselves with Networking.Instantiate. References between the objects of the template (a field of a script pointing to a child of the template) point to the copy's own objects in every copy.
