Spawning actors

To create a new instance of an actor class within the game world at runtime is referred to as a spawning an actor.

An actor can be spawned from anywhere in your code by injecting the World service. The spawnActor function takes 3 parameters.

  • type (type: Type<BaseActor>) - The actor class from which a new instance should be created.

  • position (type: THREE.Vector3) - Where in the scene the actor should initially be positioned. Defaults to {x: 0, y: 0, z: 0}.

  • rotation (type: THREE.Euler) - How the actor should initially be rotated around each axis. Defaults to {x: 0, y: 0, z: 0}.

The code below illustrates how an actor is created and returned.

import { Service, GameInstance, inject, World } from "@hology/core/gameplay"
import ExampleActor from "./actors/example-actor"

@Service()
class Game extends GameInstance {
  private world = inject(World)
  
  async onStart() {
    const actor = await this.world.spawnActor(ExampleActor)
  }
}

Last updated