GDScript basics
GDScript is Gaminode's scripting language. It looks like Python, uses indentation instead of braces, and is made for games: vectors, nodes and signals are built in.
A script
enemy.gdextends CharacterBody2D # the node type this script is attached to
signal died # a message other nodes can listen to
@export var speed := 120.0 # shown in the Inspector
@export var hp := 3
var direction := Vector2.LEFT # a normal variable
const GRAVITY := 980.0 # never changes
func _physics_process(delta: float) -> void:
velocity.x = direction.x * speed
velocity.y += GRAVITY * delta
move_and_slide()
if is_on_wall():
direction = -direction
func take_damage(amount: int) -> void:
hp -= amount
if hp <= 0:
died.emit()
queue_free()
Variables and types
var lives := 3 # int (type guessed from the value)
var speed: float = 4.5 # type written out
var name := "Ali" # String
var pos := Vector2(100, 200) # 2D; Vector3 for 3D
var items: Array[String] = [] # typed array
var stats := {"hp": 10, "mp": 5} # Dictionary
Types are optional, but using them catches mistakes before you run the game and makes code faster.
Callbacks the engine calls
| Function | When |
|---|---|
_ready() | Once, when the node and its children are in the scene |
_process(delta) | Every frame. delta is the seconds since the last frame |
_physics_process(delta) | 60 times a second, for movement and physics |
_input(event) / _unhandled_input(event) | On every key, click, touch or gamepad event |
_exit_tree() | When the node leaves the scene |
Input
Name your controls in Project Settings > Input Map, then:
if Input.is_action_just_pressed("jump") and is_on_floor():
velocity.y = -500.0
var x := Input.get_axis("move_left", "move_right") # -1 to 1
var dir := Input.get_vector("left", "right", "up", "down")
Touch screens send InputEventScreenTouch and InputEventScreenDrag. Games from Gaminode AI already include on-screen buttons.
Finding nodes
$Sprite2D # a child by name
$HUD/Score # a deeper child
get_node("../Player") # a sibling
@onready var label := $HUD/Score # look it up once, when ready
Waiting
await get_tree().create_timer(1.5).timeout # wait 1.5 seconds
$AnimationPlayer.play("die")
await $AnimationPlayer.animation_finished
Saving progress
func save_best(score: int) -> void:
var cfg := ConfigFile.new()
cfg.set_value("game", "best", score)
cfg.save("user://save.cfg")
func load_best() -> int:
var cfg := ConfigFile.new()
if cfg.load("user://save.cfg") != OK:
return 0
return cfg.get_value("game", "best", 0)
user:// is a folder that belongs to your game on each device; res:// is your project and is read-only in exported games.
Getting help with code
Ask Gaminode AI in the help chat, for example "write a GDScript for an enemy that walks back and forth". Most Godot 4 code you find online works in Gaminode as it is.
Something wrong or missing on this page? Email gaminodeofficial@gmail.com.