Skip to content

Fire

Updated

Fire is an animated particle flame built to stay cheap in large numbers: every Fire of an application shares one draw call, so a village of torches costs about as much as one. Use it for campfires, torches, braziers, candles and burning wreckage. Scripts light and put out fires, make them grow and die down, and react when players click them.

Fire is a visual effect only: it does not light its surroundings. Pair it with a Point Light for a warm glow, and with a sound for crackle. For water, see Water.

  • Entities and components. Each effect is a component you read with getComponent, change with setProps and switch with setEnabled. See Components.
  • Tweens. Smooth changes, such as a storm building up, use Tween.value. See Animation.
ComponentTypeWhat it does
Firecomp-fireA particle flame; many fires share one draw call.

FireComponent is required on a Fire entity (ent-fire). It draws a particle flame shaped as a cone. Every fire is an instance of one shared mesh, so many flames, whatever their settings, cost about one draw call. The flame is animated entirely on the graphics card.

Prop Type Default Description
radius number 0.5 Radius of the cone’s base, 0 to 100 local metres.
height number 2 Height of the flame, 0 to 100 local metres.
particleCount number 240 Particles per fire, a whole number from 0 to 2048. This is the main cost setting.
color string '#ff2200' Flame tint.
size number 0.4 Width of each camera-facing particle, 0 to 10 metres.
speed number 1 Animation cycles per second, -10 to 10. 0 freezes the flame; negative values play it backwards.
seed number 0 Starting phase. Fires with different seeds flicker out of step with each other.
  • Editor and play. In the editor every fire is shown, but only the selected one animates. In play every fire animates.
  • Picking. Each fire has an invisible box for picking, so Selectable and Grabbable work on it. Outline and Silhouette draw a border around that box.
  • Not clipped or stamped. Because it is drawn from the shared batch, a fire is not clipped by a Mask and not stamped by decals.
  • A Fire entity supports Selectable, Grabbable, Outline, Silhouette, Enable on Signal, Signal Emitter and Networkable (NetworkTraitType.Fire).

1. Light a campfire when the player clicks it

Section titled “1. Light a campfire when the player clicks it”

The Fire entity Campfire is saved disabled. A wood pile next to it has a Selectable and a Signal Emitter with the signal campfire/click, and a Point Light named Fire Glow sits above the fire. This script turns the fire on and grows the flames and the glow in together.

LightCampfire.jx
#pragma lifecycle(startup)
let lit = false;
function startup() {
const fireEntity = findEntity('Campfire');
const fire = fireEntity.getComponent(FireComponent);
const glow = findEntity('Fire Glow').getComponent(PointLightComponent);
glow.setProps({ intensity: 0 });
on('campfire/click', (event) => {
if (event.eventType !== EventType.Selectable.Select || lit) return;
findEntity(event.entityId)?.getComponent(SelectableComponent)?.setSelected(false); // so the next click selects it again
lit = true;
fire.setProps({ size: 0 }); // the flames start too small to see
fireEntity.setEnabled(true); // and animate by themselves from now on
log('Campfire lit');
Tween.value(0, 1, (t) => {
fire.setProps({ size: 0.4 * t });
glow.setProps({ intensity: 3 * t });
}, { duration: 1.5, ease: Ease.OutCubic });
});
}
  • The fire starts disabled, so it is invisible and costs nothing until it is lit.
  • The lit flag ignores further clicks once the fire burns.
  • The Fire component animates its own flames once the entity is enabled. The script only grows them in.
  • One Tween.value grows the particle size from 0 to 0.4 and the light from 0 to 3, so flames and glow rise together. Ease.OutCubic makes the fire flare up fast and settle.
  • size, speed and color are cheap to change every frame. height, radius and particleCount rebuild the fire’s particles, so tweening them stutters: change those now and then (as the next example does, at most four times a second).

A realistic fire uses fuel. Each second burns a little; when another script emits campfire/fuel, the fire grows again. Props change at most four times a second.

BurningFuel.jx
#pragma lifecycle(startup, update)
const BURN_PER_SECOND = 0.05; // fuel lost each second
const MAX_FUEL = 1;
let fire;
let fireEntity;
let fuel = MAX_FUEL;
const refresh = throttle(() => {
fire.setProps({
height: 0.4 + fuel * 1.8,
particleCount: Math.round(80 + fuel * 240),
radius: 0.3 + fuel * 0.3,
});
}, 250);
function startup() {
fireEntity = findEntity('Campfire');
fire = fireEntity.getComponent(FireComponent);
on('campfire/fuel', (data) => {
fuel = Math.min(MAX_FUEL, fuel + (data?.amount ?? 0.3));
if (!fireEntity.isEnabled()) fireEntity.setEnabled(true);
refresh();
});
}
function update(dt) {
if (!fireEntity.isEnabled()) return;
fuel -= BURN_PER_SECOND * dt;
if (fuel <= 0) {
fuel = 0;
fireEntity.setEnabled(false);
emit('campfire/out');
return;
}
refresh();
}
  • fuel is a number between 0 and 1 that drops by BURN_PER_SECOND × dt each frame, so the fire burns for 20 seconds without help.
  • throttle(fn, 250) returns a function that runs at most once every 250 ms. update calls it every frame, but the flame is rewritten only four times a second, which is plenty for a slow change.
  • Height, particle count and radius all follow the fuel. particleCount must be a whole number, hence Math.round.
  • on('campfire/fuel', (data) => …) receives the data another script passed to emit('campfire/fuel', { amount: 0.5 }). The ?? 0.3 fallback gives a default when no amount is sent.
  • When the fuel runs out, the Campfire entity is switched off and campfire/out tells other scripts (smoke, sound, a quest). The entity is switched rather than its Fire component, so the glow, sound and anything else under it go out together.
  • Fire: particleCount is the cost setting. All fires together are about one draw call, but the particles still have to be drawn: lower the count for distant or small fires.
  • Expecting a Fire to light the scene. It draws flames only. Add a Point Light next to it, and flicker its intensity from a script if you want the glow to move.
  • Calling setProps every frame with the same values. Throttle, ease or skip unchanged values, as in the examples.