Actor components

Actor components play a crucial role in Hology by enabling you to modularise and extend the functionality of your game objects (actors) in a clean and organised manner. They allow you to break down complex actors into smaller, reusable, and manageable pieces, making your game development process more efficient and maintainable.

  1. Modularity and Reusability:

    • Actor components are self-contained units of functionality that can be attached to one or more actors. This modularity allows you to create reusable pieces of functionality that can be used across multiple actors or projects.

  2. Organization:

    • Actor components help you organise your actors' functionality by separating different aspects of an actor's behaviour into distinct components. For example, you can have separate components for character movement, health, inventory, and more.

  3. Code Encapsulation:

    • Each actor component can encapsulate its own logic and data, making it easier to manage and maintain your code. This separation of concerns helps prevent code duplication and keeps your codebase cleaner.

  4. Customisation and Extensibility:

    • You can easily add or remove actor components from an actor to customise its behaviour without having to modify the core actor class. This makes it simpler to iterate on your game's design and mechanics.

  5. Editor Friendliness:

    • Hology's editor provides a user-friendly interface for configuring actor components, making it accessible to designers and artists without requiring programming knowledge.

Example component

In the example below, we have a component used to hold the state of an object's health. The max health is a parameter so that can be adjusted in the editor but it is also assigned a default value.

The current health is set to the max health on initialisation. This is because the max health value is set after the class is instantiated so it is not available at the time the currentHealth property is defined

@Component()
class HealthComponent extends ActorComponent {
    @Parameter() maxHealth: number = 100
    currentHealth: number
    
    onInit() {
        this.currentHealth = this.maxHealth
    }
    
    update(change: number) {
        this.currentHealth = Math.max(0, this.currentHealth + change)
    }
}

Attaching components

Actors can add components to themselves as properties by using the attach(Type<ActorComponent>) function to which you pass in the class you wish to attach.

@Actor()
class CharacterActor extends BaseActor {
    health = attach(HealthComponent)
    
    takeHit(damage: number) {
        this.health.update(-damage)
    }
}

Component options

With the @Component() decorator, you can pass additional options.

@Component({ inEditor: false, editorOnly: false })
class HealthComponent extends ActorComponent {

}
  • inEditor (type: boolean, default: false) - Whether or not the component should be used in the editor. In the editor, is only important to give visual feedback as to what the actor looks like. Any other functionality should be disabled. Note that some functionality that works in your game, may not work in the editor.

  • editorOnly (type: boolean, default: false) - Whether the component should only be used in the editor. If true, then it will not be in your game. This is useful for displaying 3D meshes that you want to provide some visual representation in your editor.

Last updated