Fire
Updated
A particle flame; many fires share one draw call.
- Type
ent-fire- Script class
FireEntity- Family
- Effects
Overview
Section titled “Overview”A Fire entity draws an animated particle flame shaped like a cone: a campfire, a torch, a brazier, a burning barrel. The flame is computed entirely on the graphics card, and every fire in the world is an instance of one shared mesh, so a village full of torches costs about one draw call.
The fire’s position, rotation and scale come from its entity’s transform; its look comes from its required Fire component (comp-fire). In the editor every fire is visible but only the selected one animates; in play every fire animates.
Pick a Fire for flames. To light the surroundings with a warm glow, add a Point Light next to it.
The props that matter most:
heightandradiusset the size of the cone, in local metres.particleCountis the number of particles, a whole number. It is the main cost setting.sizeis the width of each particle. Larger particles make a fuller, softer flame.colortints the flame.speedis how fast the flame animates, in cycles per second.0freezes it and negative values play it backwards.seedsets the starting phase. Give neighbouring fires different seeds so they do not flicker in step.
Set it up
Section titled “Set it up”- Add a Fire entity to the scene (see Scenes and hierarchy) and give it a clear name, such as
Campfire. - Move it where the flame should start. The cone grows up from the entity’s position.
- Set
height,radiusandcolorfor the look you want, and lowerparticleCountfor small or distant fires. - To light a glow, add a Point Light a little above the fire with a warm
colorand a shortdistance. - To react to clicks, add a Selectable and a Signal Emitter to the Fire entity: each fire has an invisible box for picking.
Examples
Section titled “Examples”Desynchronise a row of torches
Section titled “Desynchronise a row of torches”Goal: make every torch of a corridor flicker on its own rhythm at startup.
#pragma lifecycle(startup)
const TORCHES = ['Torch 1', 'Torch 2', 'Torch 3', 'Torch 4'];
function startup() { for (const name of TORCHES) { const entity = findEntity(name); if (!entity) continue;
const fire = entity.getComponent(FireComponent); fire.setProps({ seed: Random.integer(0, 10000), speed: Random.range(0.9, 1.2), color: '#ff5a10', }); }}findEntity(name)returns the entity with that name, orundefinedwhen it is not in the scene, which thecontinueskips.getComponent(FireComponent)returns the entity’s Fire component;setPropschanges only the keys passed to it.- A different
seedper torch shifts each flame’s starting phase, and a slightly differentspeedkeeps them from drifting back into step.
Light and put out a fire with a click
Section titled “Light and put out a fire with a click”Goal: clicking the fire toggles it, with the flame growing in or dying down instead of popping.
The Fire entity Campfire has a Selectable and a Signal Emitter whose signal is campfire.
#pragma lifecycle(startup)
const FULL_HEIGHT = 2;let lit = true;let tween = null;
function startup() { const fire = findEntity('Campfire').getComponent(FireComponent);
on('campfire', (event) => { if (event.eventType !== EventType.Selectable.Select) return; findEntity(event.entityId)?.getComponent(SelectableComponent)?.setSelected(false); // so the next click selects it again lit = !lit;
if (tween) tween.stop(); tween = Tween.value(fire.props.height, lit ? FULL_HEIGHT : 0, (value) => { fire.setProps({ height: value }); }, { duration: lit ? 1.2 : 2, ease: lit ? Ease.OutCubic : Ease.InSine }); });}on('campfire', …)receives every event the Signal Emitter forwards. The script keeps onlyEventType.Selectable.Select, the click.fire.props.heightis the flame’s current height, so a click in the middle of a fade continues from where the flame is.Tween.value(start, end, onValue, options)animates a plain number and passes each value tosetProps. A running tween is stopped first so two never fight over the height.- At a
heightof0the flame is invisible but the fire still exists, so it can be relit at once.
A fire that burns down unless fed
Section titled “A fire that burns down unless fed”Goal: the fire shrinks as it uses fuel, goes out when the fuel runs out, and grows again when another script sends campfire/fuel.
#pragma lifecycle(startup, update)
const BURN_PER_SECOND = 0.05;let fire;let fuel = 1;
const refresh = throttle(() => { fire.setProps({ height: 0.4 + fuel * 1.6, radius: 0.3 + fuel * 0.3, particleCount: Math.round(80 + fuel * 200), });}, 250);
function startup() { fire = findEntity('Campfire').getComponent(FireComponent);
on('campfire/fuel', (data) => { fuel = Math.min(1, fuel + (data?.amount ?? 0.3)); if (!fire.enabled) fire.setEnabled(true); refresh(); });}
function update(dt) { if (!fire.enabled) return;
fuel -= BURN_PER_SECOND * dt; if (fuel <= 0) { fuel = 0; fire.setEnabled(false); emit('campfire/out'); return; } refresh();}fuelruns from 1 to 0. It drops byBURN_PER_SECOND × dteach frame, so a full fire burns for 20 seconds.throttle(fn, 250)returns a function that runs at most once every 250 ms:updatecalls it every frame, but the flame changes only four times a second.particleCountmust be a whole number, henceMath.round.- Another script feeds the fire with
emit('campfire/fuel', { amount: 0.5 });?? 0.3is the amount when none is sent. - When the fuel runs out,
setEnabled(false)switches the Fire component off andemit('campfire/out')tells other scripts, for example to start smoke or stop a crackling sound.
- A fire is drawn from a shared batch, so a Mask does not clip it and a Decal does not stamp it.
- Outline and Silhouette draw around the fire’s invisible picking box, not around the flame.
- Fade a fire in by tweening
heightfrom0before enabling it, so it does not pop in at full size.
Components
Section titled “Components”What a new Fire is made of. Required components are always present and cannot be removed; the others can. Per entity says whether an entity can hold one of that component or several. Layer, Lock and Enable are on every placed entity, and Transform holds its position, rotation and scale.
| Component | Type | Role | Per entity | What it does |
|---|---|---|---|---|
| Layer | comp-layer |
Required | One | Puts the entity on a render/raycast layer so cameras and rays can include or skip it. |
| Lock | comp-lock |
Required | One | Editor-only flag that stops the entity from being selected or moved. |
| Enable | comp-enable |
Required | One | Turns the entity and its children on or off: hides them and stops their behavior. |
| Transform | comp- |
Required | One | Position, rotation and scale of the entity, with optional editor-only per-axis locks. |
| Fire | comp-fire |
Required | One | A particle flame; many fires share one draw call. |
You can also add 9 optional components, such as Enable On Signal, Grabbable, Networkable, Networkable Trait, Outline, Pivotable.
The settings of a new entity, as the runtime creates it. Change them in the Properties panel of XR Creator, or from a script with getComponent(…).setProps({ … }).
| Prop | Default | Range |
|---|---|---|
radius |
0.5 |
0 ≤ x ≤ 100 |
height |
2 |
0 ≤ x ≤ 100 |
particleCount |
240 |
0 ≤ x ≤ 2048 |
color |
"#ff2200" |
|
size |
0.4 |
0 ≤ x ≤ 10 |
speed |
1 |
-10 ≤ x ≤ 10 |
seed |
0 |
Events
Section titled “Events”With a Signal Emitter on the entity, these events reach scripts as { entityId, eventType, data }. Compare eventType with the EventType constants.
It reports the events every entity has: Transform::change (and position-change, rotation-change, scale-change), Enable::change, Layer::change and Lock::change.
Live example
Section titled “Live example”Run it in the browser, read its scripts, and download it as an XR Creator project.
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.
From a script
Section titled “From a script”Find the entity by its name, check its class, and work with its components. Name the entity in the Hierarchy first; this example expects My Fire.
#pragma lifecycle(startup)
function startup() { // Find the entity by the name it has in the Hierarchy. const entity = findEntity('My Fire'); if (!(entity instanceof FireEntity)) return;
// Its Fire component holds the settings listed under Props. const component = entity.getComponent(FireComponent); log('radius is', component.props.radius); component.setProps({ radius: 0.75 });}