Lightningrod
Based on game build 722832 | Last updated: 2026-04-27
Overview
lightningrod.lua registers a spawnable structure entity that serves dual purposes: attracting lightning strikes during storms and storing electrical charge that can be consumed by battery-powered devices. The prefab's fn() constructor builds the physics body, light emitter, and client-side components; server-side logic (after TheWorld.ismastersim check) attaches gameplay components including workable, lootdropper, inspectable, and battery. The lightningrod holds 1 charge that decays over 3 days (tracked as chargeleft counter), not 3 separate charges. Instantiated with SpawnPrefab("lightning_rod").
Usage example
-- Spawn at world origin:
local inst = SpawnPrefab("lightning_rod")
inst.Transform:SetPosition(0, 0, 0)
-- Check charge status:
if inst.charged then
print("Charges remaining: " .. tostring(inst.chargeleft))
end
Dependencies & tags
External dependencies:
prefabutil-- utility functions for prefab construction (MakeSnowCoveredPristine, MakeHauntableWork, etc.)
Components used:
lootdropper-- drops loot when hammered to destructionworkable-- enables hammering with 4 work units, HAMMER actioninspectable-- provides status string viagetstatuscallbackbattery-- allows other devices to consume stored charge via callback functions
Tags:
structure-- added in fn() for targeting and loot ruleslightningrod-- added in fn() for lightning attraction logic
Properties
| Property | Type | Default Value | Description |
|---|---|---|---|
assets | table | --- | Array of Asset(...) entries for animation banks and minimap icon. |
prefabs | table | {"lightning_rod_fx", "collapse_small"} | Dependent prefab names spawned for effects and destruction FX. |
inst.charged | boolean | false | Whether the lightningrod currently holds electrical charge. Set by setcharged(). |
inst.chargeleft | number | nil | Number of days of charge remaining. Starts at 3 when struck by lightning, decrements daily. |
inst.zaptask | task | nil | Scheduled task reference for periodic zap effects. Cancelled on discharge. |
inst.Light | component | --- | Light emitter configured with orange colour (235/255, 121/255, 12/255), radius 1.5, intensity 0.5. |
inst.scrapbook_specialinfo | string | "LIGHTNINGROD" | Identifier for scrapbook/codex entries. |
Main functions
fn()
- Description: Client-side prefab constructor. Creates the entity, adds Transform, AnimState, MiniMapEntity, Light, SoundEmitter, and Network components. Configures light properties, sets animation bank/build, adds tags, and returns
inst. On master sim, continues to attach gameplay components (lootdropper, workable, inspectable, battery), registers event listeners, and sets save/load hooks. - Parameters: None
- Returns: entity instance
- Error states: None — runs on every host (client and server). Server-side component attachment skipped on clients via
TheWorld.ismastersimguard.
onhammered(inst, worker)
- Description: Callback when hammering work completes (4 work units). Drops loot via
lootdropper:DropLoot(), spawnscollapse_smalldestruction FX at entity position with metal material, then removes the entity. - Parameters:
inst-- lightningrod entityworker-- player entity performing the hammering
- Returns: None
- Error states: Errors if
inst.components.lootdropperis nil (should not occur — component added in fn()).
onhit(inst, worker)
- Description: Callback when work is performed (each hammer hit). Plays "hit" animation then pushes "idle" animation to queue.
- Parameters:
inst-- lightningrod entityworker-- player entity performing the hammering
- Returns: None
- Error states: None.
dozap(inst)
- Description: Scheduled task. Creates visual and audio zap effects. Cancels existing
zaptaskif present, plays lightningrod sound, spawnslightning_rod_fxprefab at entity position, then schedules next zap after random 10-40 seconds. Called repeatedly while charged. - Parameters:
inst-- lightningrod entity - Returns: None
- Error states: None — guards
zaptasknil check before cancel.
discharge(inst)
- Description: Removes all charge from the lightningrod. Stops watching world state "cycles", clears bloom effect handle, sets
chargedto false, clearschargeleft, disables light emitter, and cancelszaptask. Called when charge depletes or consumed by battery. - Parameters:
inst-- lightningrod entity - Returns: None
- Error states: None — guards
zaptasknil check before cancel.
setcharged(inst, charges)
- Description: Sets the lightningrod to charged state with specified charge amount. If not already charged, enables bloom effect, enables light, starts watching "cycles" world state, and sets
chargedto true. Setschargeleftto maximum of current or new charges, then triggersdozap(). - Parameters:
inst-- lightningrod entitycharges-- number of charge days to set (typically 3 from lightning strike)
- Returns: None
- Error states: None.
onlightning(inst)
- Description: Handler for
lightningstrikeevent. Plays hit animation and sets charge to 3 days viasetcharged(). - Parameters:
inst-- lightningrod entity - Returns: None
- Error states: None.
ondaycomplete(inst)
- Description: Handler for day cycle completion. Triggers
dozap()visual effect. Ifchargeleftis greater than 1, decrements by 1. Otherwise (chargeleftis 1 or less), callsdischarge()to fully deplete charge. - Parameters:
inst-- lightningrod entity - Returns: None
- Error states: None — assumes
chargeleftis valid when called (guarded by world state watch).
OnSave(inst, data)
- Description: Save hook. Writes
chargedandchargeleftto save data table if entity is currently charged. - Parameters:
inst-- lightningrod entitydata-- table to populate with save state
- Returns: None
- Error states: None.
OnLoad(inst, data)
- Description: Load hook. Restores charge state from save data if
data.chargedis true anddata.chargeleftis valid (>0). Callssetcharged()with loaded value. - Parameters:
inst-- lightningrod entitydata-- table containing saved state
- Returns: None
- Error states: None — guards all data access with nil checks.
getstatus(inst)
- Description: Inspectable status callback. Returns "CHARGED" string if entity is charged, otherwise returns
nil(no status displayed). - Parameters:
inst-- lightningrod entity - Returns: "CHARGED" string or
nil - Error states: None.
CalcActualCharge(inst) (local)
- Description: Code note: V2C comment states lightningrod holds 1 charge that decays over 3 days, NOT 3 separate charges. Calculates actual usable charge value as
chargeleft - (ceil(chargeleft) - 1). Returns fractional charge representation for battery consumption logic. - Parameters:
inst-- lightningrod entity - Returns: number representing actual charge, or
nilifchargeleftis nil - Error states: None.
CanBeUsedAsBattery(inst, user, mult)
- Description: Battery component callback. Determines if lightningrod can power a device. If
mult(multiplier/charge cost) is provided, checks ifactual_charge >= mult. Otherwise returns true ifchargedis true. Returns false with "NOT_ENOUGH_CHARGE" error string if insufficient charge. - Parameters:
inst-- lightningrod entityuser-- entity attempting to use the batterymult-- charge multiplier/cost required (optional)
- Returns:
trueif usable, orfalse, "NOT_ENOUGH_CHARGE"if not - Error states: None.
UseAsBattery(inst, user, mult)
- Description: Battery component callback. Consumes charge from the lightningrod. If
multis provided andactual_charge > mult, triggersdozap()and decrementschargeleftbymult. Otherwise callsdischarge()to fully deplete. - Parameters:
inst-- lightningrod entityuser-- entity consuming the batterymult-- charge multiplier/cost to consume (optional)
- Returns: None
- Error states: None.
ResolvePartialChargeMult(inst, user, mult)
- Description: Battery component callback. Returns the actual charge amount that can be consumed, capped at the requested
mult. Used when device requests more charge than available. - Parameters:
inst-- lightningrod entityuser-- entity consuming the batterymult-- requested charge multiplier
- Returns: number (minimum of
multandactual_charge), ormultif no charge available - Error states: None.
onbuilt(inst)
- Description: Handler for
onbuiltevent. Plays "place" animation, pushes "idle" to queue, and plays craft sound. - Parameters:
inst-- lightningrod entity - Returns: None
- Error states: None.
Events & listeners
- Listens to:
lightningstrike-- triggersonlightning()to add 3 days of charge. - Listens to:
onbuilt-- triggersonbuilt()for placement animation and sound. - Watches:
cyclesworld state -- triggersondaycomplete()at day end to decrement charge. - Pushes: None directly (lootdropper component pushes
entity_droplooton destruction).