GameObject

Namespace:

namespace Lenga\Engine\Core;

class GameObject

GameObject represents an object in a scene hierarchy. Every GameObject has a Transform, can contain components, and can have child GameObjects.

Parenting affects transforms in the usual hierarchy-friendly way: moving a parent moves its children in world space, but moving a child does not move the parent. Child objects keep their own local position, rotation, and scale relative to the parent.

Methods

setActive

public function setActive(bool $value)

isActiveSelf

public function isActiveSelf()

isActiveInHierarchy

public function isActiveInHierarchy()

compareTag

public function compareTag(string $tag)

getScene

public function getScene()

getChildren

public function getChildren()

setParent

public function setParent(?GameObject $parent, bool $worldPositionStays = true): bool

Moves this GameObject under another GameObject, or back to the scene root when $parent is null. When $worldPositionStays is true, Lenga preserves the object's world-space placement and recalculates its local transform. When it is false, Lenga keeps the current local transform relative to the new parent.

getComponents

public function getComponents(?string $type = null)

getComponentsInChildren

public function getComponentsInChildren(?string $type = null, bool $includeInactive = false)

getComponentInChildren

public function getComponentInChildren(string $type, bool $includeInactive = false)

getComponentsInParent

public function getComponentsInParent(?string $type = null, bool $includeInactive = false)

getComponentInParent

public function getComponentInParent(string $type, bool $includeInactive = false)

addComponent

public function addComponent(string $type)

clone

public function clone(?string $name = null)

find

public static function find(string $name)

findGameObjectsWithTag

public static function findGameObjectsWithTag(string $tag)

Example

use Lenga\Engine\Core\Behaviour;
use Lenga\Engine\Core\GameObject;

class GameObjectInteraction extends Behaviour
{
    public function start(): void
    {
        // Access the GameObject this Behaviour is attached to
        $gameObject = $this->gameObject;

        // Get information about this GameObject
        Debug::log('Name: ' . $gameObject->name);
        Debug::log('Active: ' . ($gameObject->isActiveInHierarchy() ? 'yes' : 'no'));

        // Find other game objects by tag
        $enemies = GameObject::findGameObjectsWithTag('Enemy');
        foreach ($enemies as $enemy) {
            Debug::log('Found enemy: ' . $enemy->name);
        }
    }
}