Moonstorm Spark
Based on game build 714014 | Last updated: 2026-03-06
Overview
moonstorm_spark is a transient, flying entity spawned during moonstorms. It floats near the ground and periodically emits an electric shock (via dospark) that damages eligible entities within a 4-unit radius. Sparks can be collected and later recharged via the moonsparkchargeable component. They have a finite lifespan governed by perishable, and their behavior is managed by the SGspore stategraph and a dedicated brain. The spark emits light, sound, and avoids insulation/ghost states on targets.
Usage example
-- Spawning a spark manually (typically done via worldgen, not modder code)
local spark = SpawnPrefab("moonstorm_spark")
spark.Transform:SetPosition(x, y, z)
Dependencies & tags
Components used:
combat(viaCanBeAttacked,GetAttacked)electricattacks(viaAddSource)hauntable(viaPanic)health(viaIsDead)inventory(viaIsInsulated,GiveItem)inventoryitem(viaIsHeld,OnDropped,SetOnPutInInventoryFn)locomotor(viaEnableGroundSpeedMultiplier,SetTriggersCreep,walkspeed)moonsparkchargeable(viaDoSpark)moonstormmanager(viaDoTestForSparks)perishable(viaSetLocalMultiplier,SetOnPerishFn,SetPercent,SetPerishTime,StartPerishing)stackable(viaGet,StackSize)workable(viaSetOnFinishCallback,SetWorkAction,SetWorkLeft)
Tags added: show_spoilage, moonstorm_spark
Tags checked (on targets): moonsparkchargeable, playerghost, INLIMBO, flight, invisible, notarget, noattack, moonstorm_static, wall, structure
Properties
| Property | Type | Default Value | Description |
|---|---|---|---|
sparktask | Task or nil | nil | Task handle for scheduled spark events. |
crowdingtask | Task or nil | nil | Task handle for crowding density checks. |
scrapbook_damage | number | TUNING.MOONSTORM_SPARK_DAMAGE * TUNING.ELECTRIC_DAMAGE_MULT | Damage value shown in scrapbook. |
scrapbook_animpercent | number | 0.5 | Animation frame percentage for scrapbook preview. |
scrapbook_anim | string | "idle_flight_loop" | Animation used in scrapbook preview. |
scrapbook_animoffsetx | number | 20 | X offset for scrapbook preview. |
scrapbook_animoffsety | number | 35 | Y offset for scrapbook preview. |
displayadjectivefn | function | DisplayAdjectiveFn | Returns string adjective for UI based on spoilage state. |
Main functions
dospark(inst)
- Description: Initiates the spark’s discharge sequence: spawns a shock FX prefab, schedules
dospark1, and recursively schedules the nextdosparkafter a delay. - Parameters:
inst(Entity) — the spark instance. - Returns: Nothing.
- Error states: Does not return early; fails silently if
instis invalid.
dospark1(inst, dospark)
- Description: Performs the main electric shock: damages all eligible entities within 4 units, charges
moonsparkchargeableentities, and schedulesdospark2after 0.5s and the nextdosparkafter 5–15s. - Parameters:
inst(Entity) — the spark instance.dospark(function) — reference to thedosparkfunction to enable recursion.
- Returns: Nothing.
- Error states: Targets are only damaged if they meet multiple conditions: valid, not in limbo, not insulated, not dead, have
combatandCanBeAttacked()returns true. Permanently skips绝缘 (insulated) entities.
dospark2(inst)
- Description: Reduces the spark’s light radius to 1.5 after 0.5 seconds, simulating a fading discharge.
- Parameters:
inst(Entity) — the spark instance. - Returns: Nothing.
depleted(inst)
- Description: Handles spark expiration: disables workability, pushes
"death"event, removes"spore"tag, setspersists = false, and removes the entity after 3s. - Parameters:
inst(Entity) — the spark instance. - Returns: Nothing.
checkforcrowding(inst)
- Description: Monitors spore density within
TUNING.MUSHSPORE_MAX_DENSITY_RAD. If overpopulated, spoils the spark immediately by settingperishableto 0%; otherwise, schedules the next check. - Parameters:
inst(Entity) — the spark instance. - Returns: Nothing.
onpickup(inst)
- Description: Called when the spark is placed in an inventory. Slows spoilage, stops idle sound, and cancels tasks (
crowdingtask,sparktask) and light. - Parameters:
inst(Entity) — the spark instance. - Returns: Nothing.
ondropped(inst)
- Description: Called when the spark is dropped. Restores spoilage rate, resumes idle sound (if awake), restores workability, splits stacks if oversized, resumes light and spark timer.
- Parameters:
inst(Entity) — the spark instance. - Returns: Nothing.
onworked(inst, worker)
- Description: Callback for when the spark is worked on (e.g., with a net). Transfers the spark to the worker’s inventory and forces a moonstorm spark test.
- Parameters:
inst(Entity) — the spark instance.worker(Entity) — the entity performing the work.
- Returns: Nothing.
OnWake(inst)
- Description: Restarts spark timer and idle sound if the entity wakes and is not held.
- Parameters:
inst(Entity) — the spark instance. - Returns: Nothing.
OnSleep(inst)
- Description: Cancels spark timer and kills idle sound when the entity goes to sleep.
- Parameters:
inst(Entity) — the spark instance. - Returns: Nothing.
DisplayAdjectiveFn(inst)
- Description: Returns a localized string (
STRINGS.UI.HUD.STALE_POWERorSTRINGS.UI.HUD.SPOILED_POWER) if the spark is stale or spoiled, otherwisenil. - Parameters:
inst(Entity) — the spark instance. - Returns:
string?— adjective string ornil.
fn()
- Description: Constructor function that creates the spark entity, attaches all required components and assets, sets up behavior, and returns the fully initialized prefab instance.
- Parameters: None.
- Returns: Entity — a fully initialized
moonstorm_sparkprefab.
Events & listeners
- Listens to:
"onputininventory"— triggersonpickup."ondropped"— triggersondropped."death"— internal event fromperishableviadepleted, also manually pushed when depleted.
- Pushes:
"death"— when spark is depleted."perishchange"— triggered byperishable:SetPercent()orSetPerishTime()(internal to component).