Events

Events

Built-in Events

In addition to using Entities, you can also respond to events emitted by Dreamlab in order to run code in the world. These can be accessed using the Event Manager at game.events.

Events are split into three categories: client, server, and common. Client and Server events only fire on their respective platform, whereas common events are triggered on both.

Some common events include:

EventCategoryDescription
onInstantiateCommonFires when entities (including spawnable entities) are created
onSpawnCommonFires when Spawnable Entities are created
onDestroyCommonFires when entities are destroyed
onRenderFrameClientFires on every frame (after all entities have run)
onPhysicsStepCommonFires on every physics tick (after all entities have run)
💡

You can view a full list of built-in events and their types in the @dreamlab.gg/core repo.

Custom Events

We recommend using the event pattern in your world scripts to allow easy communication between entities. Be aware that event emitters are not synced over the network and events will only be able to be read on the platform they were emitted on.

events.ts
import { EventEmitter } from '@dreamlab.gg/core/events'
 
interface Events {
  // Define your events in an interface
  onCustomEvent: [arg: string, arg2: number]
}
 
// Export your event emitter for use in your world scripts
export const events = new EventEmitter<Events>()
entity.ts
import { events } from './events.ts'
 
// Emit events
events.emit('onCustomEvent', 'myString', 10)
 
// Respond to event
events.addListener('onCustomEvent', (arg1, arg2) => {
  // ...
})
💡

Dreamlab’s EventEmitter is a re-export of from the eventemitter3 package. For more information refer to their documentation.