Entity Controllers

Entity controllers are the brains behind an entity. They enable you to physically define how an entity is controlled or interacts with the world. Entity controllers often handle things like entity movement logic, entity movement related state, pathfinding, AI behavior, and many other things.

All control related logic of an entity is suited to be included in an entity controller.

Lifecycle Overview

All entity controllers inherit from the BaseEntityController class. This class defines the lifecycle methods an entity controller has and can override.

The lifecycle of an entity controller is based on the following order.

  1. Attach - the attach()method of the controller is called when it is attached to an entity. This is where you can do initialization or additional collider assignments to the attached entity if necessary depending on what you need your entity controller to do.

  2. Spawn - the spawn()method of the controller is called when the entity first spawns. If the attached entity is already spawned, this will not be called.

  3. TickWithPlayerInput - If the controller is attached to an entity that is an instance of the PlayerEntity class, or a class that extends it, tickWithPlayerInput() will be called with the controlling players current input.

  4. Tick - the tick() method of the controller is called each tick while the entity is still spawned.

  5. Detach - the detach() method of the controller is called when the controller is detached from the entity. This is also called right before the entity is despawned from entity.despawn().

  6. Despawn - the despawn()method of the controller is called when the entity despawns.

Premade Entity Controllers

The HYTOPIA SDK currently provides 2 premade entity controllers out of the box that work great for simple needs, or as a foundation to build your own game-specific entity controllers on top of.

  • Player Entity Controller - The default controller automatically assigned to a PlayerEntityif your own entity controller is not provided. This controller handles basic movement behaviors like WASD movement, jump, camera movement, platform sticking, and more.

  • Simple Entity Controller - A "simple" entity controller that implements basic quality of life controls like .move()and .face(), allowing you to create basic or complex pathfinding based on target coordinates and movement speeds without needing to do any additional complex math for movements. Completion callbacks are also supported for both methods this entity controller exposes.

Next Steps

Entity controllers are a core concept when creating any type of custom movement, npc or AI behaviors for your game. You can dive deeper into the different types of entity controllers to learn more.

Last updated