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.
Before you start
Section titled “Before you start”- Entities and components. Each effect is a component you read with
getComponent, change withsetPropsand switch withsetEnabled. See Components. - Tweens. Smooth changes, such as a storm building up, use
Tween.value. See Animation.
| Component | Type | What it does |
|---|---|---|
| Fire | comp-fire | A particle flame; many fires share one draw call. |
The Fire component
Section titled “The Fire component”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).
Examples
Section titled “Examples”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.
#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 }); });}Click the wood pile and a campfire flares up with a point light glowing above it: a Fire entity saved disabled, a Selectable with a Signal Emitter, and a Behavior script that grows the flames and the light in together.
Click the wood pile to light the fire. Drag to orbit the camera, scroll to zoom.
- The fire starts disabled, so it is invisible and costs nothing until it is lit.
- The
litflag 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.valuegrows the particlesizefrom 0 to 0.4 and the light from 0 to 3, so flames and glow rise together.Ease.OutCubicmakes the fire flare up fast and settle. size,speedandcolorare cheap to change every frame.height,radiusandparticleCountrebuild the fire’s particles, so tweening them stutters: change those now and then (as the next example does, at most four times a second).
2. A fire that burns down unless fed
Section titled “2. A fire that burns down unless fed”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.
#pragma lifecycle(startup, update)
const BURN_PER_SECOND = 0.05; // fuel lost each secondconst 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();}A campfire loses fuel every second and shrinks with it, throttled to four prop changes a second; Add wood feeds it, and an empty fire switches off until fed again.
Watch the fire shrink, then press Add wood to feed it; let it run out and it goes dark until you add more. Drag to orbit the camera, scroll to zoom.
fuelis a number between 0 and 1 that drops byBURN_PER_SECOND × dteach frame, so the fire burns for 20 seconds without help.throttle(fn, 250)returns a function that runs at most once every 250 ms.updatecalls 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.
particleCountmust be a whole number, henceMath.round. on('campfire/fuel', (data) => …)receives the data another script passed toemit('campfire/fuel', { amount: 0.5 }). The?? 0.3fallback gives a default when no amount is sent.- When the fuel runs out, the Campfire entity is switched off and
campfire/outtells 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.
Performance
Section titled “Performance”- Fire:
particleCountis 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.
Common mistakes
Section titled “Common mistakes”- 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
setPropsevery frame with the same values. Throttle, ease or skip unchanged values, as in the examples.