🚢
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
  • Get and Add Components
  • Performance Considerations
  1. TypeScript
  2. AirshipBehaviour

Accessing Other Components

Get and Add Components

Similar to Unity, accessing other components can be done through the GetComponent<T>() method, as well as other component fetching methods, such as GetComponents<T>().

However, to fetch other AirshipBehaviour components, you must use the GetAirshipComponent<T>() method, et. al.

The same applies for adding components, such as AddComponent<T>() and AddAirshipComponent<T>().

// File 1
export default class Test extends AirshipBehaviour {}

// File 2
export default class MyComponent extends AirshipBehaviour {
    public Start() {
        // Adding a Unity and Airship component:
        this.gameObject.AddComponent<BoxCollider>();
        this.gameObject.AddAirshipComponent<Test>();
        
        // Get Unity and Airship components:
        const boxCollider = this.gameObject.GetComponent<BoxCollider>();
        const test = this.gameObject.GetAirshipComponent<Test>();
    }
}

Performance Considerations

Fetching components via methods such as GetComponent and GetAirshipComponent are expensive. Avoid calling these sorts of methods in per-frame code, such as the Update method. Instead, cache the result of the call to a variable.

export default class MyComponent extends AirshipBehaviour {
    private boxCollider: BoxCollider;
    
    public Start() {
        // Fetch box collider once:
        this.boxCollider = this.gameObject.GetComponent<BoxCollider>();
    }
    
    public Update(dt: number) {
        // Use box collider per-frame:
        const closestToOrigin = this.boxCollider.ClosestPoint(Vector3.zero);
    }
}

Alternatively, the above code could expose boxCollider as a public variable, in which the variable could be assigned in-editor.

PreviousUsing Component DecoratorsNextPublish Game

Last updated 9 months ago