Behaviour

Namespace:

namespace Lenga\Engine\Core;

class Behaviour

Behaviour is the base class for component logic that belongs on a GameObject. Use it when code needs scene ownership, lifecycle methods, Inspector fields, or nearby components. Use ordinary PHP classes, enums, and utilities for code that does not need to live on a GameObject.

Event Helpers

createSignal

protected function createSignal(): Signal

Creates an owner-scoped Signal and registers it for cleanup when the behaviour is destroyed.

Use this when a specific instance intentionally exposes a local callback surface. For most cross-system gameplay events, prefer emitEvent(...) and onEvent(...).

emitEvent

protected function emitEvent(string $eventName, mixed $payload = null): void

Emits a global EventBus event.

Use this when listeners should react without holding a reference to the dispatcher.

dispatchEvent

protected function dispatchEvent(string $eventName, mixed $payload = null): void

Alias for emitEvent(...).

onEvent

protected function onEvent(string $eventName, callable $listener, bool $disposeOnDisable = true): SignalSubscription

Subscribes this behaviour to a global EventBus event and automatically tracks the returned subscription.

By default the subscription is disposed when the behaviour disables.

onceEvent

protected function onceEvent(string $eventName, callable $listener, bool $disposeOnDisable = true): SignalSubscription

Like onEvent(...), but the listener is automatically removed after the first matching event.

trackSubscription

protected function trackSubscription(SignalSubscription $subscription, bool $disposeOnDisable = true): SignalSubscription

Tracks an existing subscription handle so the behaviour lifecycle can clean it up.

This is useful when you subscribe directly through EventBus::on(...) or another API that returns a SignalSubscription.

If disposeOnDisable is false, the subscription stays alive across disable and is only released on destroy.

Methods

startCoroutine

public function startCoroutine(\Generator $routine)

stopCoroutine

public function stopCoroutine(Coroutine|\Generator $routine)

stopAllCoroutines

public function stopAllCoroutines()

awake

public function awake()

Called when the Behaviour instance is being loaded.

onEnable

public function onEnable()

Called when the object becomes enabled and active.

start

public function start()

Called before the first frame update, after all Awake calls.

fixedUpdate

public function fixedUpdate()

Called on a fixed timestep, used for physics-like updates.

update

public function update()

Called once per frame.

lateUpdate

public function lateUpdate()

Called once per frame, after all Update calls.

onDisable

public function onDisable()

Called when the object becomes disabled or inactive.

onDestroy

public function onDestroy()

Called when the behaviour is destroyed.

Example

use Lenga\Engine\Core\Behaviour;
use Lenga\Engine\Core\Signal;

final class Door extends Behaviour
{
    public Signal $onOpened;

    public function awake(): void
    {
        $this->onOpened = $this->createSignal();
    }

    public function open(): void
    {
        $this->onOpened->dispatch($this);

        $this->emitEvent('game.door.opened', [
            'source' => $this,
        ]);
    }
}