Entity-Component System

Source: assets/modules/xs_ec.wren


Class Component

Base class for components that can be added to entities

Components should inherit from this class and override initialize(), update(), and/or finalize()

Each entity can only have one component of each type

construct new()

Creates a new component

IMPORTANT: Always call super() first when creating a new component subclass constructor

Note: Other components might not be available yet - use initialize() to query them

initialize()

Called right before the first update

This is the ideal place to query and cache references to other components on the same entity

Example: _transform = owner.get(Transform)

finalize()

Called when the component/entity is deleted

Clean up any references to other entities and components by setting them to null

This prevents memory leaks from circular references

update(dt)

Called once per frame with delta time in seconds

Put your game logic here - this is only called when the component is enabled

dt is typically 1/60 (0.0166...) for 60 FPS

owner

Gets the Entity object that owns this component

owner=(o)

Sets the owner (used internally by Entity)

enabled

Checks if the component is enabled

If not enabled, the update() function will not be called

enabled=(e)

Sets the enabled state of the component

initialized_

Gets the initialized state (used internally by Entity)

initialized_=(i)

Sets the initialized state (used internally by Entity)


Class Entity

Represents a game object that can contain multiple components

Entities are managed by the Entity-Component system and should be created with Entity.new()

Call Entity.initialize() and Entity.update(dt) in your game's initialize() and update() methods

construct new()

Creates a new entity that will be visible to the rest of the game in the next update

The entity won't appear in Entity.entities until the next frame

add(component)

Adds a component to the entity

The component must be a subclass of Component

If a component of the same type already exists, it will be finalized and replaced

Components are initialized and updated in the order they were added

get(type)

Gets a component of the matching type, or null if not found

Example: var transform = entity.get(Transform)

remove(type)

Marks a component for removal at the end of the current update frame

The component's finalize() method will be called before removal

components

Gets all components attached to this entity

deleted

Checks if the entity is marked for deletion

If true, you should set any references to this entity to null to avoid accessing deleted entities

delete()

Marks the entity for removal at the end of the current update frame

All components will have their finalize() methods called before the entity is removed

name

Gets the name of the entity (useful for debugging)

name=(n)

Sets the name of the entity

tag

Gets the tag (used as a bitflag when filtering entities)

tag=(t)

Sets the tag

enabled=(e)

Sets the enabled state of all components

static initialize()

Initializes the entity system - MUST be called once at game startup

Call this from your game's initialize() method before creating any entities

Example: Entity.initialize()

static update(dt)

Updates all entities and their components - MUST be called every frame

Call this from your game's update(dt) method

Handles adding new entities, removing deleted ones, and updating all component logic

Example: Entity.update(dt)

static withTag(tag)

Gets all entities where the tag matches exactly with the given tag (using bitwise AND)

Use this when you need entities with ALL specified tag bits set

static withTagOverlap(tag)

Gets all entities where the tag has ANY bit overlap with the given tag

Use this when you need entities with at least one matching tag bit

static withoutTagOverlap(tag)

Gets all entities where the tag does not have bit overlap with the given tag

static setEnabled(tag, enabled)

Sets the enabled state for all entities with matching tag overlap

static entities

Gets all entities active in the system

toString

Returns a string representation of this entity

static print()

Prints a formatted list of all entities and their components (for debugging)

removeDeletedComponents_()

Removes components marked for deletion (used internally)