Creator

The creator component is a base class for entity creator components. The creator component duplicates the entity, its descendants and any components when the create() method is called. Please see the API documentation for more details on creating custom entity creator components.

Properties

The creator component properties that are accessible through the inspector.

Display

The display method determines how the entity with the creator component should be displayed when the simulation is running. It is one of the following:

  • None: The entity is hidden.
  • Standard: The entity is displayed.
  • Highlighted: The entity is displayed and highlighted.

Example

The scripted component provided below is an example implementation of a custom entity creator component. The period of time T between which entities are created is modeled as a Gaussian random variable T ~ N(mean, variance). The sequence of random numbers, generated by Random.global.nextGaussian() is deterministically pseudo-random. In other words, it produces the same sequence of random numbers every time the simulation is run.

The RandomCreator component extends the base class CreatorComponent and calls the create() method to duplicate the entity:

import { type Entity, CreatorComponent, Random, Readonly, Units, UnitType } from "prototwin";

export class RandomCreator extends CreatorComponent {
    @Readonly(true)
    @Units(UnitType.Time)
    time: number;

    @Units(UnitType.Time)
    mean: number;

    variance: number;

    constructor(entity: Entity) {
        super(entity);
        this.time = 0;
        this.mean = 1;
        this.variance = 0;
    }

    public override update(dt: number): void {
        while (this.time <= 0) {
            this.create(); // Duplicate the entity.
            this.time = Math.max(0, Random.global.nextGaussian(this.mean, Math.sqrt(this.variance)));
        }
        this.time -= dt;
    }
}