Core Built-in Types

Use randomize() only if you need re-seeding; Godot 4 seeds the global RNG randomly at startup.

7.1 Overview

true, false 42, -7, 0xFF, 0b1010, 1_000_000 3.14, 1e-3, INF, NAN

64-bit double (vectors use 32-bit unless built with double precision)

"hello", 'hi', """multi-line""", r"raw\path" Unicode, value semantics (copy-on-write)

Interned, fast comparison; used for names, actions, signals Pre-parsed path to a node/property _on_hit, func(): pass Reference to a method or lambda First-class signal object Opaque handle to server-side resources Base class of all non-built-in types

7.2 Numbers

Use randomize() only if you need re-seeding; Godot 4 seeds the global RNG randomly at startup. For reproducible randomness use RandomNumberGenerator with a fixed seed.

var a := 7 / 2          # 3 (integer division; the editor warns about it)
var b := 7 / 2.0        # 3.5
var c := 7 % 3          # 1 (int only)
var d := fmod(7.5, 2.0) # 1.5
var e := 2 ** 10        # 1024 (power operator, 4.0+)
var f := posmod(-3, 5)  # 2 (always non-negative)
var g := int(3.9)       # 3 (truncates)
var h := roundi(3.5)    # 4
var r := randf_range(0.0, 10.0)
var i := randi_range(1, 6)
var near := is_equal_approx(0.1 + 0.2, 0.3)   # true; never use == on computed floats
var clamped := clampf(1.7, 0.0, 1.0)
var t := lerpf(0.0, 100.0, 0.25)              # 25.0
var mapped := remap(5.0, 0.0, 10.0, 0.0, 1.0) # 0.5
var snapped_v := snappedf(3.14159, 0.01)      # 3.14

7.3 Strings

Escape sequences: \n, \t, \", \\. Strings are compared with == by value.

var s := "Hello, World"
print(s.length())              # 12
print(s.to_upper())            # HELLO, WORLD
print(s.substr(0, 5))          # Hello
print(s.find("World"))         # 7
print("World" in s)            # true
print(s.begins_with("Hell"))   # true
print(s.replace("World", "Godot"))
print(s.split(", "))           # ["Hello", "World"]
print(", ".join(["a", "b"]))   # "a, b"
print("  padded  ".strip_edges())
print("42".to_int() + 1, " ", "3.5".to_float())
print("abc".is_valid_int())    # false
print(str(10) + " coins")      # convert to string with str()
print(s[0], s[-1])             # H d  (indexing; strings are immutable per character via [])

# Formatting
print("HP: %d/%d" % [hp, max_hp])
print("Pos: %.2f, %.2f" % [pos.x, pos.y])
print("%s has %d gold" % ["Ada", 50])
print("%05d" % 42)              # 00042
print("{0} vs {1}".format(["Red", "Blue"]))
print("{name} is level {lvl}".format({"name": "Ada", "lvl": 3}))

var multi := """Line one
Line two"""
var path := r"C:\folder\file"   # raw string (4.0+): backslashes not escaped

7.4 StringName and NodePath

Most engine APIs accept String and convert automatically; StringName avoids repeated hashing in hot code.

var action: StringName = &"jump"
if Input.is_action_just_pressed(action): pass
var p: NodePath = ^"Body/Sprite2D"
var sprite := get_node(p)
var prop_path := ^"modulate:a"     # property subpath, used by tweens/animations

7.5 Callable, Signal, RID

Covered in Chapters 12, 17, and 37. Quick preview:

var c: Callable = print_message          # method reference
c.call("hi")
var sig: Signal = $Button.pressed
sig.connect(c.bind("pressed!"))
var rid: RID = $Sprite2D.get_canvas_item()

7.6 Type conversion and checking

typeof(5) == TYPE_INT            # true
type_string(typeof(5.0))         # "float"
var n := int("12")               # 12
var f := float("1.5")
var s := str(Vector2(1, 2))      # "(1, 2)"
var v = str_to_var("Vector2(1, 2)")
var text := var_to_str({"a": 1})
node.get_class()                 # "Sprite2D" (engine class name)
node.is_class("Node2D")          # true