Prefab

Namespace:

namespace Lenga\Engine\Core;

class Prefab

Methods

instantiate

public static function instantiate(
    string $assetPath,
    string|InstantiateOptions|array|null $options = null,
): GameObject

Instantiates the prefab at the project-relative asset path into the active scene. Pass a string to rename the instance, or pass InstantiateOptions when the instance should spawn at a position, rotation, or parent.

Example

use Lenga\Engine\Core\Behaviour;
use Lenga\Engine\Core\Input;
use Lenga\Engine\Core\InstantiateOptions;
use Lenga\Engine\Core\Prefab;
use Lenga\Engine\Enumerations\KeyCode;

class SpawnManager extends Behaviour
{
    public function start(): void
    {
        // Instantiate a prefab at this GameObject's position
        $enemyInstance = Prefab::instantiate(
            'Assets/Prefabs/Enemy.prefab.json',
            InstantiateOptions::at($this->gameObject->transform->position, name: 'Enemy_1'),
        );
    }

    public function update(): void
    {
        if (Input::getKeyDown(KeyCode::SPACE)) {
            // Spawn a bullet from a prefab
            $bullet = Prefab::instantiate(
                'Assets/Prefabs/Bullet.prefab.json',
                InstantiateOptions::at($this->transform->position),
            );
        }
    }
}