Prefab

Namespace:

namespace Lenga\Engine\Core;

class Prefab

Methods

instantiate

public static function instantiate(string $assetPath, ?string $name = null)

Example

use Lenga\Engine\Core\Behaviour;
use Lenga\Engine\Core\Prefab;

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

    public function update(): void
    {
        if (Input::getKeyDown(KeyCode::SPACE)) {
            // Spawn a bullet from a prefab
            $bullet = Prefab::instantiate('assets/prefabs/bullet.prefab');
            $bullet->transform->position = $this->transform->position;
        }
    }
}