Skip to reading
Progress Bars, Hints & Journals

Add an amount without overflowing

Edition 0.86.4.1 HF1

0.86.4.1 Hotfix 1 edition. Earlier lessons and screenshots retain their original version context; revised references and supplements describe the newer behavior.

Download PDFView PDF page

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:

Code example
extends "res://scripts/runtime/script_base.gd"
Code example
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 true

1. 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.

Page 14 of 53Permalink
Go to page