A movement Behaviour might read input, then use a physics component. A weapon Behaviour might ask for an audio component and a prefab spawner. A health Behaviour might update a UI component.
Get One Component
Use getComponent() when a Behaviour expects one component of a type.
<?php
declare(strict_types=1);
namespace MyGame\Game\Scripts;
use Lenga\Engine\Core\Behaviour;
use Lenga\Engine\Core\Rigidbody2D;
final class MovementReader extends Behaviour
{
private ?Rigidbody2D $body = null;
public function awake(): void
{
$this->body = $this->gameObject->getComponent(Rigidbody2D::class);
}
}
Keep the result nullable unless the Behaviour guarantees the component exists.
Require Components When the Behaviour Needs Them
Use #[RequireComponent(...)] when a Behaviour cannot work without another component beside it.
<?php
declare(strict_types=1);
namespace MyGame\Game\Scripts;
use Lenga\Engine\Attributes\RequireComponent;
use Lenga\Engine\Core\Behaviour;
use Lenga\Engine\Core\Rigidbody2D;
#[RequireComponent(Rigidbody2D::class)]
final class PhysicsMover extends Behaviour
{
private ?Rigidbody2D $body = null;
public function awake(): void
{
$this->body = $this->gameObject->getComponent(Rigidbody2D::class);
}
}
That makes the dependency visible and protects the scene from missing setup.
Search Children or Parents When Structure Matters
Some components live on child helper objects or parent objects.
Use child or parent component searches when the hierarchy is part of the design:
getComponentInChildren()for helper objects under this GameObjectgetComponentsInChildren()when several children may have the same componentgetComponentInParent()when a child needs to talk to the owning parentgetComponentsInParent()when parent hierarchy context matters
Use these intentionally. If the search path surprises you later, the hierarchy probably needs clearer structure.