Component

Namespace:

namespace Lenga\Engine\Core;

class Component

Component is the base class for engine components attached to a GameObject.

Inspector-assigned component fields can reference live scene components or compatible components stored inside prefab assets. When a component field points at a prefab asset, pass that component reference to GameObject::instantiate() to clone the prefab and receive the matching component on the clone.

Example

use Lenga\Engine\Core\Behaviour;
use Lenga\Engine\Core\Component;
use Lenga\Engine\Core\GameObject;
use Lenga\Engine\Core\InstantiateOptions;
use Lenga\Engine\Core\ParticleSystem;
use Lenga\Engine\Core\Rigidbody3D;
use Lenga\Engine\Core\Vector3;

class ComponentExample extends Behaviour
{
    public ?ParticleSystem $hitSparkPrefab = null;

    public function start(): void
    {
        // Get a component from this GameObject
        $rb = $this->getComponent(Rigidbody3D::class);
        $instanceId = $rb->getInstanceId();

        // Access the GameObject that owns this component
        $gameObject = $this->gameObject;

        Debug::log('Component instance ID: ' . $instanceId);
    }

    public function spawnHitSpark(Vector3 $hitPoint): void
    {
        if ($this->hitSparkPrefab === null) {
            return;
        }

        $spark = GameObject::instantiate(
            $this->hitSparkPrefab,
            InstantiateOptions::at($hitPoint, name: 'Hit Spark'),
        );
        $spark->play();
    }
}