🚢
Airship
  • Getting Started
    • Welcome to Airship
    • Installing Airship
  • TypeScript
    • Overview
    • AirshipBehaviour
      • Adding Inspector Properties
      • AirshipSingleton
      • Using Component Decorators
      • Accessing Other Components
  • Publishing
    • Publish Game
    • Developer Console
  • Networking
    • Multiplayer
    • Local Server Mode
    • Network Signals
    • Network Functions
    • Network Identity
    • Network Transform
  • Input
    • User Input
      • Actions
      • Keyboard
      • Mouse
    • Proximity Prompts
  • Core Package
    • What is the Core Package?
    • Chat Commands
    • Inventory
    • Spawning Characters
    • Enable / Disable Core Features
  • Physics
    • Physics Settings
    • Physics Layers
  • Platform Services
    • Data Store
      • Locking
    • Cache Store
    • Leaderboards
    • Platform Inventory
    • Server Management
    • Server List
    • Server Transfers
    • Users
    • Parties
    • Matchmaking
    • External Services
  • CHARACTERS
    • Quick Configuration
    • Character Movement System
      • Character Movement Data
      • Character Movement Events
    • Character Camera
      • First Person Camera
      • Simple Usage
      • Camera Structure
      • Default Camera Modes
      • Disabling the Camera System
    • Character Animations
      • Character Blender Animations
      • Character Ragdoll
  • Accessories
    • Accessories Intro
    • Creating Accessories
    • Using Accessories
  • ANIMATIONS
    • Animation Events
  • Optimization
    • Live Game Profiler
    • Reducing Bundle Size
  • Game Settings
    • Game Visibility
  • Other
    • Project Status
    • FAQ
    • DataType Math
    • JS to Luau
    • Tags
    • Terrain
    • AirshipNetworkBehaviour
      • Lifecycle Events
      • ServerRpc
      • ObserversRpc
      • TargetRpc
    • VoxelWorld
      • Voxel World Getting Started
      • Voxel World Tips
      • Prefab Voxels
      • QuarterBlocks
    • Easy Helper Utils
      • Easy Motion
      • Easy Grid Align
      • Easy Look At
      • Easy Shake
      • Easy Destroy
Powered by GitBook
On this page
  • Overview
  • NetworkIdentity
  • Attaching a NetworkIdentity Component
  • Adding a NetworkIdentity to a NetworkPrefabCollection
  • Spawning a NetworkIdentity
  • Despawning a NetworkIdentity
  • Sending a NetworkIdentity over network
  1. Networking

Network Identity

PreviousNetwork FunctionsNextNetwork Transform

Last updated 8 months ago

The component enables you to spawn GameObjects on the server and have them automatically appear on each client.


Overview

CubeManager.ts
export default class CubeManager extends AirshipSingleton {
    // This is a reference to a prefab with a NetworkIdentity component
    public Cube: GameObject;

    public override Start(): void {
        if (Game.IsServer()) {
            // Spawn a cube on the server and replicate it to each player
            const cube = Object.Instantiate(
                this.Cube, 
                new Vector3(0, 1, 0), 
                Quaternion.identity
            );
            NetworkServer.Spawn(cube);
            // Despawn cube after 4 seconds
            task.delay(4, () => NetworkServer.Destroy(cube));
        }
    }
}

NetworkIdentity

Attaching a NetworkIdentity Component


Adding a NetworkIdentity to a NetworkPrefabCollection

In order to be able to spawn and despawn a NetworkIdentity through code, it must be a part of a game's NetworkPrefabCollection. The template project includes a collection in the Assets/Resources folder.

If your project does not already contain a collection, you can automatically generate one through the "Generate Network Prefab Collections" menu item.

To add a NetworkIdentity to your game's NetworkPrefabCollection, drag a prefab from your Hierarchy or Project tab onto the NetworkPrefabCollection "Network Prefabs" list, or select the prefab through the search menu.


Spawning a NetworkIdentity

Server
const cube = Object.Instantiate(cubePrefab);
NetworkServer.Spawn(cube);

When a player joins a game all networked GameObjects in the server scene are automatically replicated to the player.


Despawning a NetworkIdentity

Server
NetworkServer.Destroy(cube);

Sending a NetworkIdentity over network

You can use networkIdentity.netId to send a reference to a NetworkIdentity via NetworkSignals and NetworkFunctions.

Use NetworkUtil.GetNetworkIdentity(netId) to retrieve a NetworkIdentity from the netId.

The NetworkIdentity component can be added to any through the "Add Component" menu.

When a NetworkPrefabCollection is generated, it is automatically populated with every prefab in the project that contains a . For smaller projects, this tool can be used to entirely manage a game's collection. If a game has a large amount of assets, developers should opt to manually add new prefabs to their game's collection.

NetworkIdentity
GameObject
NetworkIdentity
Spawning and despawning a NetworkIdentity
Adding a NetworkIdentity component to a prefab
Generating a NetworkPrefabCollection
Adding a prefab to a NetworkPrefabCollection