Rusted Warfare Mod Workshop
All coursesCreative tools中文

Give a unit a button with memory

Declare variables, initialize them on creation and toggle a state with a button

Decide what to remember

The switchfire tank remembers a shot count and whether enhanced ammunition is enabled, use number and boolean, memory belongs to each unit rather than the whole team

[core]
defineUnitMemory: number shots, boolean powered

Set initial values on creation

Use the created event for one-time initialization, start shots at 0 and powered at false so later actions read a clear state

[hiddenAction_initialize]
autoTriggerOnEvent: created
setUnitMemory: shots=0, powered=false

Keep the button focused

The button flips powered with setUnitMemory, buildSpeed:0 suits this immediate toggle, add costs, duration and restrictions later when the basic behavior works

[action_toggle]
text: 切换弹种 / Switch ammo
description: 射击次数 / Shots: %{memory.shots}
buildSpeed: 0
setUnitMemory: powered=not memory.powered

Separate assignments from display

Use the variable name on the left of an assignment and memory.powered when reading it, %{memory.shots} evaluates inside supported dynamic text, plain text does not evaluate variable names

Try it yourself

Add a boolean marked variable with its own button and status text

Open interactive lesson and download the companion mod