🚢
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
  • Network Signals
  • Creating a Network Signal
  • Client -> Server
  • Server -> Client
  • Data Limitations
  • Caching
  1. Networking

Network Signals

PreviousLocal Server ModeNextNetwork Functions

Last updated 9 months ago

are used to communicate between the client and server. They are a critical piece of building a multiplayer game.


Overview

Imagine a scenario where a player would like to use a health potion. We first need to tell the server that a player would like to use a potion and have it apply the potion's effects.

PotionManager.ts
export default class PotionManager extends AirshipSingleton {
    // Creating a NetworkSignal
    private usePotion = new NetworkSignal<{ potion: string }>("UsePotion");

    public override Start(): void {
        if (Game.IsClient()) {
            // Tell the server that the local player is trying to use a potion
            this.usePotion.client.FireServer({ potion: "health" });
        }

        if (Game.IsServer()) {
            // Listen for a player use potion request
            this.usePotion.server.OnClientEvent((player, event) => {
                const hasPotion = DoesPlayerHavePotion(player, event.potion);
                if (!hasPotion) return;
                // Apply the potion's effect
                player.character?.SetHealth(100);
            });
        }
    }
}

Network Signals

Example Use Cases

Example
Explanation

Ability Use

Notifying the server when a player uses a special ability

Trade Request

Letting the server know two players would like to trade

Item Drop

Telling the server a player has dropped an item

Example
Explanation

Weather Change

Informing clients of a weather change to keep them visually synchronized

Leaderboard Updates

Letting clients know the latest leaderboard standings

Status Effects

Communicating that a client has had a status effect applied to their character

Creating a Network Signal

Network Signals can be created inside of any script. NetworkSignal takes a single generic parameter that is the type of data passed through the signal.

Shared
const usePotion = new NetworkSignal<{ potion: string }>("UsePotion");

The NetworkSignal constructor accepts a single string parameter that uniquely identifies the signal. Providing a string that is already in use will raise an error when entering play mode.


Client -> Server

Firing the usePotion Network Signal on the client

Client
usePotion.client.FireServer({ potion: "health" });

Listening for usePotion Network Signals on the server

Server
usePotion.server.OnClientEvent((player, event) => {
    print(`${player.username} is using potion: ${event.potion}`);
});

Server -> Client

Once the server has applied the potion, we want to tell every player about it so they can play particle effects, sounds, update UI, etc.

Server to client Network Signals can be sent to any number of players

Shared
const potionUsed 
    = new NetworkSignal<{ userId: string; potion: string }>("PotionUsed");

Telling all players a potion has been consumed

Server
potionUsed.server.FireAllClients({ userId: "1", potion: "health" });

Listening for potionUsed Network Signals on the client

Client
potionUsed.client.OnServerEvent((event) => {
    print(`User ${event.userId} has used potion: ${event.potion}`);
});

Data Limitations

Primitive data types and their composite forms (arrays, objects) can be sent over the network when using NetworkSignals and NetworkFunctions. The following Unity types are also supported

Caching

If a network signal receives an event, but does not have any existing connection handlers, the event will be cached until the first connection is created. At this point, all events will be passed along to the connection handler.

This is typically useful when the server sends a client a network signal event right after the client has joined, but before the client has connected to the signal. Thus, the event is not missed by the client, as it is successfully handed to the first connection to the signal on the client.

The cache is limited in size (currently hard-coded to 10,000), and will throw errors if exceeded.

The first argument passed to OnClientEvent is always the who fired the Network Signal.

A common requirement is sending Characters and Players over the network. Send and ids and utilize the and APIs to fetch their respective objects. This ensures that only permitted data types are sent over the network, while the actual player and character objects are retrieved locally.

Network Signals
Player
Vector3
Vector2
Quaternion
Color
Player
Character
Player
Character
Network Signal: Client -> Server
Network Signal: Server -> Client(s)