Supported on
Network Spawner
NetworkSpawner is the quickest path to spawning a networked prefab. Point it at
an Instantiator and a prefab, and it handles the spawn and
despawn for you.
It covers two shapes:
- Host managed. One instance for the whole session, created and deleted by the host. Use it for something that must exist exactly once and only in a state the host drives, entered and left by the whole session together.
- Per client. Every client gets its own instance, owned by whoever spawned it. This is the shape you want for player avatars.
Component Inputs
| Name | Type | Description |
|---|---|---|
| Instantiator | Instantiator | The Instantiator that performs the spawn. Required. |
| Prefab | ObjectPrefab | The prefab to spawn. Must be in the Instantiator's Prefabs list. |
| Host Managed | boolean | One instance for the whole session, created and deleted by the host. Leave off to give every client its own instance, owned by whoever spawned it. Defaults to true. |
| Network Id | string | Shown when Host Managed is on. A globally unique id, the same string on every client. It lets any client clean the instance up on state exit, even after the host that created it has left. Defaults to enter_global_unique_id. |
| Ownership | Host Owned or Unowned | Shown when Host Managed is on. Who may write to the instance's store. Host Owned lets only the current host write; Unowned lets every client write. The host removes the instance on state exit either way. Defaults to Host Owned. |
| Persistence | Ephemeral, Owner, Session, or Persist | Shown when Host Managed is off. How long the instance's store outlives its owner. Defaults to Owner. |
| Spawn Location | SceneObject (optional) | Optional spawn location. Defaults to this scene object's transform. |
The default enter_global_unique_id is treated as unset. If you enable Host
Managed and leave it, the component logs an error and does not spawn:
A host managed NetworkSpawner needs a unique Network Id to manage "<prefab name>" across clients. Please change it
Component API
| Name | Description |
|---|---|
instance | Read-only getter. The spawned NetworkRootInfo, or null before it exists. |
onSpawned | Event, called with the NetworkRootInfo. |
onDespawned | Event, called with no arguments. |
Usage
Most of the time you configure this in the Inspector and never touch it from code. When you do need to react to the spawn:
import { NetworkSpawner } from 'ConnectedFramework.lspkg/Components/NetworkSpawner';
@component
export class BallWatcher extends BaseScriptComponent {
@input
spawner!: NetworkSpawner;
onAwake() {
this.spawner.onSpawned.add((root) => {
print(`Ball spawned, owned by ${root.getOwnerUserId()}`);
});
}
}
To extend the spawn behavior, subclass it. This is what the Multiplayer Game Starter Project does for its player spawner:
import { NetworkSpawner } from 'ConnectedFramework.lspkg/Components/NetworkSpawner';
@component
export class PlayerSpawner extends NetworkSpawner {
protected onAwake(): void {
super.onAwake();
this.onSpawned.add((root) => {
// Wire the spawned avatar to scene-level references here.
});
}
}
Related
Was this page helpful?