Skip to main content

Variables

Variables are Resources that hold a typed value and emit a signal when the value changes.

Most variable scripts live under addons/godot_meds_core/scripts/variables/.

At a glance

  • Every typed variable exposes a typed value, a typed set_value(...) helper, and a value_changed(...) signal.
  • BaseVariable provides the shared runtime behavior.
  • NumericVariable adds shared numeric rules for FloatVariable and IntVariable.

Class hierarchy

IconTypeExtendsPurpose
BaseVariable iconBaseVariableResourceShared base resource for all variable types
BaseVariable iconNumericVariableBaseVariableShared numeric behavior for clamped float and int variables

Typed variables

IconTypeValue typeNotes
BoolVariable iconBoolVariableboolBasic boolean state
IntVariable iconIntVariableintOptional clamping and range metadata
FloatVariable iconFloatVariablefloatOptional clamping and range metadata
StringVariable iconStringVariableStringText values
ColorVariable iconColorVariableColorColors and tints
Vector2Variable iconVector2VariableVector22D positions and directions
Vector3Variable iconVector3VariableVector33D positions and directions

Common API

The following behavior comes from BaseVariable and applies to every typed variable resource.

MemberPurpose
valueThe current typed runtime value
value_changed(new_value)Signal emitted when the value changes
initial_valueExported starting value used when the resource is initialized
debug_logsLogs value changes, listeners, and caller context
save_to_devicePersists the value to user://settings.cfg and restores it on load
reset_onControls whether the value resets on scene load or only when the application starts

reset_on supports two modes:

  • On Scene Load: reset when a new scene is loaded.
  • On Application Start: keep the runtime value cached across scene changes until the app restarts.

When save_to_device is enabled, values are stored in the variables section of user://settings.cfg using the resource path basename as the key.

Numeric behavior

NumericVariable is the shared base for FloatVariable and IntVariable.

MemberPurpose
clamp_valueEnables runtime clamping
min_valueLower bound for numeric values
max_valueUpper bound for numeric values
range_changed(clamp_enabled, min_value, max_value)Signal emitted when numeric range settings change

Both initial_value and runtime value assignments are sanitized through the numeric clamp rules.

Creating a variable resource

  1. In the FileSystem dock: Right click → New Resource…
  2. Pick a typed resource such as BoolVariable, FloatVariable, or StringVariable.
  3. Save it as a .tres.
  4. Assign that .tres to exported fields in scripts.

alt text

tip

Use FloatVariable or IntVariable when you want sliders, progress bars, or other UI to react to value ranges.

Script examples

Basic variable usage

This example connects to a BoolVariable and reads its current value.

extends Node

@export var bool_variable: BoolVariable

func _ready() -> void:
bool_variable.value_changed.connect(_on_bool_changed)
print("Initial value:", bool_variable.value)

func _on_bool_changed(new_value: bool) -> void:
print("Bool changed to:", new_value)

When you want debug output to include the caller, prefer the typed helper method:

bool_variable.set_value(true, self)

Numeric variable usage

This example shows a FloatVariable with range notifications and a typed setter.

@export var health: FloatVariable

func _ready() -> void:
health.range_changed.connect(_on_health_range_changed)
health.value_changed.connect(_on_health_changed)
health.set_value(125.0, self)

func _on_health_range_changed(clamp_enabled: bool, min_value: float, max_value: float) -> void:
print("Clamp:", clamp_enabled, "Range:", min_value, "-", max_value)

func _on_health_changed(new_value: float) -> void:
print("Health:", new_value)