Guides Components Create Components with Behaviours

Components 2 min read Updated Apr 2026

Create Components with Behaviours

A Behaviour is a scene-attached component class for gameplay logic.

Use a Behaviour when code needs to live on a GameObject, use lifecycle methods, expose fields in the Inspector, or coordinate other components in the scene.

Use regular PHP classes, enums, value objects, and utility classes when code does not need to be attached to a GameObject.

Behaviours Are Components

When a class that extends Behaviour is attached to a GameObject, it lives beside the native components on that object.

That means it can:

  • read or write exposed fields
  • find other components
  • respond to lifecycle methods
  • coordinate child objects
  • call APIs such as setActive()

The GameObject owns the Behaviour component. The Behaviour adds scene-attached gameplay logic.

A Small Behaviour Component

This example creates a Behaviour component that can hide or show a referenced door object.

<?php

declare(strict_types=1);

namespace MyGame\Game\Scripts;

use Lenga\Engine\Attributes\AddComponentMenu;
use Lenga\Engine\Attributes\Header;
use Lenga\Engine\Core\Behaviour;
use Lenga\Engine\Core\GameObject;

#[AddComponentMenu('Gameplay/Door Trigger')]
final class DoorTrigger extends Behaviour
{
    #[Header('Target')]
    public ?GameObject $door = null;

    public bool $hideDoorOnTrigger = true;

    public function trigger(): void
    {
        if ($this->door === null) {
            return;
        }

        $this->door->setActive(!$this->hideDoorOnTrigger);
    }
}

The public fields become editable component data. The method contains the scene logic.

Decide What Should Be a Behaviour

Create a Behaviour when the logic belongs to a GameObject in the scene.

Good Behaviour examples:

  • PlayerController
  • EnemyPatrol
  • DoorTrigger
  • PickupCollector
  • MainMenu
  • CameraFollow

Avoid writing one giant Behaviour that controls everything. Small Behaviour components are easier to inspect, test, reuse, and replace.

Keep Plain PHP Plain

Not every piece of game code should be a Behaviour.

If a calculation, enum, formatter, lookup table, or helper does not need GameObject ownership, lifecycle methods, or Inspector fields, keep it as normal PHP.

<?php

declare(strict_types=1);

namespace MyGame\Game\Combat;

final class CombatCalculator
{
    public function getDamage(float $baseAttack, float $enemyDefence): float
    {
        return $baseAttack - ($enemyDefence * 0.5);
    }
}

A Behaviour can use that class when it needs combat math, but the calculator itself does not need to be attached to a GameObject.

What Comes Next