Skip to reading
GDScript Programming & Godot

6. Variables, constants and enums

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 illustrations
Code example
const KEY_REWARD: int = 5
enum LockState { LOCKED, OPEN, JAMMED }
var call_count: int = 0
Code example
func run() -> Array:
    var state: LockState = LockState.LOCKED
    call_count += 1
    return [KEY_REWARD, state, call_count]

A local variable is declared inside a function and belongs to that call. A member variable is declared outside functions and belongs to the script instance. A constant cannot be reassigned. An enum gives meaningful names to a small set of integer values.

In PCA, an ordinary native record gets a fresh instance for each call. Its member values start again. Turn on Keep global instance alive when a script must keep members, timers or connected callbacks between calls. That makes the instance persistent for the session; it does not put those members in save games.

For saved puzzle facts, use the Variable Designer and the Variable registry or variable actions. For example, Variable.door_open.value is game state, while a member named door_open is only that instance’s data.

Figure 3. Keep global instance alive controls the lifetime of the authored script. Saved game variables remain a separate store.

Page 14 of 378Permalink
Go to page