On this page

particlefx

Functions and properties for controlling particle effect component playback and shader constants.

Functions

particlefx.play(url: string | Hash | Url, emitter_state_function?: function(self, id, emitter, state))

Starts playing a particle FX component. Particle FX started this way need to be manually stopped through particlefx.stop(). Which particle FX to play is identified by the URL. A particle FX will continue to emit particles even if the game object the particle FX component belonged to is deleted. You can call particlefx.stop() to stop it from emitting more particles.

// How to play a particle fx when a game object is created.
// The callback receives the hash of the path to the particlefx, the hash of the id
// of the emitter, and the new state of the emitter as particlefx.EMITTER_STATE_.
function emitter_state_change(self, id, emitter, state) {
  if (emitter === hash("exhaust") && state === particlefx.EMITTER_STATE_POSTSPAWN) {
    // exhaust is done spawning particles...
  }
}

export default defineScript({
  init(self) {
    particlefx.play("#particlefx", emitter_state_change);
  },
});

Parameters

  • url: string | Hash | Url — the particle fx that should start playing.
  • emitter_state_function?: function(self, id, emitter, state) — optional callback function that will be called when an emitter attached to this particlefx changes state.

self object The current object id hash The id of the particle fx component emitter hash The id of the emitter state constant the new state of the emitter:

  • particlefx.EMITTER_STATE_SLEEPING

  • particlefx.EMITTER_STATE_PRESPAWN

  • particlefx.EMITTER_STATE_SPAWNING

  • particlefx.EMITTER_STATE_POSTSPAWN

particlefx.stop(url: string | Hash | Url, options?: Record<string | number, unknown>)

Stops a particle FX component from playing. Stopping a particle FX does not remove already spawned particles. Which particle FX to stop is identified by the URL.

// How to stop a particle fx when a game object is deleted and immediately also
// clear any spawned particles:
export default defineScript({
  final(self) {
    particlefx.stop("#particlefx", { clear: true });
  },
});

Parameters

  • url: string | Hash | Url — the particle fx that should stop playing

  • options?: Record<string | number, unknown> — Options when stopping the particle fx. Supported options:

  • boolean clear: instantly clear spawned particles

particlefx.set_constant(url: string | Hash | Url, emitter: string | Hash, constant: string | Hash, value: Vector4)

Sets a shader constant for a particle FX component emitter. The constant must be defined in the material assigned to the emitter. Setting a constant through this function will override the value set for that constant in the material. The value will be overridden until particlefx.reset_constant is called. Which particle FX to set a constant for is identified by the URL.

// The following examples assume that the particle FX has id "particlefx", it
// contains an emitter with the id "emitter" and that the default-material in
// builtins is used, which defines the constant "tint".
// If you assign a custom material to the sprite, you can reset the constants
// defined there in the same manner.
// How to tint particles from an emitter red:
export default defineScript({
  init(self) {
    particlefx.set_constant("#particlefx", "emitter", "tint", vmath.vector4(1, 0, 0, 1));
  },
});

Parameters

  • url: string | Hash | Url — the particle FX that should have a constant set
  • emitter: string | Hash — the id of the emitter
  • constant: string | Hash — the name of the constant
  • value: Vector4 — the value of the constant

particlefx.reset_constant(url: string | Hash | Url, emitter: string | Hash, constant: string | Hash)

Resets a shader constant for a particle FX component emitter. The constant must be defined in the material assigned to the emitter. Resetting a constant through this function implies that the value defined in the material will be used. Which particle FX to reset a constant for is identified by the URL.

// The following examples assume that the particle FX has id "particlefx", it
// contains an emitter with the id "emitter" and that the default-material in
// builtins is used, which defines the constant "tint".
// If you assign a custom material to the sprite, you can reset the constants
// defined there in the same manner.
// How to reset the tinting of particles from an emitter:
export default defineScript({
  init(self) {
    particlefx.reset_constant("#particlefx", "emitter", "tint");
  },
});

Parameters

  • url: string | Hash | Url — the particle FX that should have a constant reset
  • emitter: string | Hash — the id of the emitter
  • constant: string | Hash — the name of the constant

Constants

particlefx.EMITTER_STATE_SLEEPING

The emitter does not have any living particles and will not spawn any particles in this state.

particlefx.EMITTER_STATE_PRESPAWN

The emitter will be in this state when it has been started but before spawning any particles. Normally the emitter is in this state for a short time, depending on if a start delay has been set for this emitter or not.

particlefx.EMITTER_STATE_SPAWNING

The emitter is spawning particles.

particlefx.EMITTER_STATE_POSTSPAWN

The emitter is not spawning any particles, but has particles that are still alive.

Properties

material: Hash

The material used during rendering by an emitter in a particle FX component. The property type is a hash and refers to a material resource.

image: Hash

The image used during rendering by an emitter in a particle FX component. The property type is a hash and refers to an image resource (atlas or tile source). Note: When setting the image, if the currently playing animation of the emitter isn't found in the new image, the animation will be set to the first animation found.

animation: Hash

The animation used during rendering by an emitter in a particle FX component. The property type is a hash and refers to a valid animation in an atlas or a tile source resource. If the animation isn't found, and error will be thrown.