RequireComponent

Applies to: classes (repeatable)

Namespace:

namespace Lenga\Engine\Attributes;

Declares that this Behaviour depends on one or more other components being present on the same GameObject.

Use this when a Behaviour cannot work correctly unless another component exists beside it. For example, a player movement Behaviour might require a Rigidbody2D, or a pickup Behaviour might require a renderer and collider.

When Lenga attaches the Behaviour, it checks the required components and adds any missing ones before the Behaviour lifecycle methods run. This applies both when the Behaviour is authored in the editor and when the Behaviour is added from code.

Multiple types can be listed in a single attribute or by stacking multiple RequireComponent attributes.

Parameters

Parameter Type Description
...$componentTypes string One or more fully-qualified component class names.

Example

use Lenga\Engine\Attributes\RequireComponent;
use Lenga\Engine\Core\Behaviour;
use Lenga\Engine\Core\Rigidbody2D;
use Lenga\Engine\Core\BoxCollider2D;

// Ensure a 2D physics body and collider are always present.
#[RequireComponent(Rigidbody2D::class, BoxCollider2D::class)]
class PlayerController extends Behaviour
{
    public float $speed = 5.0;

    public function update(): void
    {
        $body = $this->getComponent(Rigidbody2D::class);
        // Move using the rigidbody...
    }
}