Add an amount without overflowing
For a simple increase, Change Variable with operation add is available as an action. It does not automatically cap the number. This complete GDScript record adds ten and keeps the source between 0 and 100:
extends "res://scripts/runtime/script_base.gd"func run(_context: Dictionary = {}) -> Variant:
var values: Dictionary = Runtime.get("variables") as Dictionary
var current: float = float(values.get("power", 0.0))
var updated: float = clampf(current + 10.0, 0.0, 100.0)
await SetVariable({"name": "power", "value": updated})
return true1. Create and apply a script record with ID add_power.
2. Add an Add 10 Button. In Events, choose run-visual-script and target add_power. That behavior can run a GDScript record as well.
3. Press it several times in a running game. The meter and source number should stop at 100.
4. Use the same approach with a negative amount when the machine consumes power.
The Dictionary is the game’s current variables. The script reads power, calculates a new amount and uses SetVariable to notify the game of the change. If you choose Maximum 5, change the upper limit in this script to 5 too.
A progress bar is not a clock. For a countdown, your timer or repeated game logic must change the variable. Do not run an endless, no-wait loop from a GUI click to animate the meter.
