LogoLogo
  • Get Started
  • Getting Started
    • Initial Setup
    • Create Your First Game
    • API Reference
    • Build Your First World Map
    • Multiplayer Testing
    • Use Templates & Examples
    • Styling & Assets
      • Modeling Guidelines
      • Texturing Guidelines
      • Default Assets
  • Build Faster With AI Tools
  • SDK Guides
    • Assets
    • Audio & SFX
      • Audio Manager
    • Blocks & Chunks
      • Block Types
      • Block Type Registry
      • Chunks
      • Chunk Lattice
    • Camera
    • Chat & Commands
    • Debugging
    • Entities
      • Animations
      • Block Entities
      • Colliders & Hitbox
      • Child Entities
      • Entity Controllers
        • Base Entity Controller
        • Pathfinding Entity Controller
        • DefaultPlayer Entity Controller
        • Simple Entity Controller
      • Entity Manager
      • Model Entities
      • Movement & Pathfinding
      • Player Controlled Entities
    • Events
    • Input & Controls
    • Lighting
      • Ambient Light
      • Light Manager
      • Point Lights
      • Spot Lights
      • Sun Light (Directional)
    • Mobile
    • Persisted Data
    • Players
      • Player Manager
      • Persisted Player Data
    • Plugins
    • Physics
      • Colliders
      • Collision Groups
      • Debugging
      • Gravity
      • Raycasts
      • Rigid Bodies
    • User Interface
      • Overlay UI
      • Scene UIs
      • Scene UI Manager
    • Worlds
      • Map Data Format
  • Helpful Resources
    • HYTOPIA Architecture & Platform Overview
    • Useful Third-Party Tools
Powered by GitBook
On this page
Export as PDF
  1. SDK Guides
  2. Entities
  3. Entity Controllers

Base Entity Controller

The BaseEntityController class is what all other types of entity controllers inherit from.

Below is the SDK's internal implementation of the BaseEntityController for reference, giving direct insight into how it works.

/**
 * A base class for entity controller implementations.
 * 
 * @remarks
 * The BaseEntityController should be extended 
 * by a more specific entity controller that you or a
 * plugin implements. Entity controllers are intended to
 * be used as one controller instance per entity, but
 * are flexible enough for edge cases such as if you want to create
 * niche behavior of one controller for many entities that
 * behave in unison.
 * 
 * <h2>Events</h2>
 * 
 * This class is an EventRouter, and instances of it emit
 * events with payloads listed under {@link BaseEntityControllerEventPayloads}
 * 
 * @public
 */
export default abstract class BaseEntityController extends EventRouter {
  /**
   * Override this method to handle the attachment of an entity
   * to your entity controller.
   * @param entity - The entity to attach the controller to.
   */
  public attach(entity: Entity): void {
    this.emit(BaseEntityControllerEvent.ATTACH, { entity });
  };

  /**
   * Override this method to handle the despawn of an entity
   * from your entity controller.
   * @param entity - The entity to despawn.
   */
  public despawn(entity: Entity): void {
    this.emit(BaseEntityControllerEvent.DESPAWN, { entity });
  };

  
  /**
   * Override this method to handle the detachment of an entity
   * from your entity controller.
   * @param entity - The entity to detach.
   */
  public detach(entity: Entity): void {
    this.emit(BaseEntityControllerEvent.DETACH, { entity });
  };
  
  /**
   * Override this method to handle the spawning of an entity
   * to your entity controller.
   * @param entity - The entity to spawn.
   */
  public spawn(entity: Entity): void {
    this.emit(BaseEntityControllerEvent.SPAWN, { entity });
  };

  /**
   * Override this method to handle entity movements
   * based on player input for your entity controller.
   * This is called every tick by a PlayerEntity with a
   * entity controller.
   * @param entity - The entity to tick.
   * @param input - The current input state of the player.
   * @param cameraOrientation - The current camera orientation state of the player.
   * @param deltaTimeMs - The delta time in milliseconds since the last tick.
   */
  public tickWithPlayerInput(entity: PlayerEntity, input: PlayerInput, cameraOrientation: PlayerCameraOrientation, deltaTimeMs: number): void {
    this.emit(BaseEntityControllerEvent.TICK_WITH_PLAYER_INPUT, { entity, input, cameraOrientation, deltaTimeMs });
  }

  /**
   * Override this method to handle entity movements
   * based on your entity controller.
   * @param deltaTimeMs - The delta time in milliseconds since the last tick.
   */
  public tick(entity: Entity, deltaTimeMs: number): void {
    this.emit(BaseEntityControllerEvent.TICK, { entity, deltaTimeMs });
  }
}

Diving Deeper

PreviousEntity ControllersNextPathfinding Entity Controller

Last updated 3 months ago

The BaseEntityController class is constantly evolving. You can find the latest .

If there are features that we don't currently support for the BaseEntityController that you'd like to see added to the HYTOPIA SDK, you can .

BaseEntityController API Reference here
submit a feature request here