Toolbox

Run code every frame

3
  • _physics_process(delta) L02 · Platformer Movement
    What it does

    a function Godot runs over and over, about 60 times a second. Movement and physics go in here.

    Use it

    for anything that has to keep happening while the game runs.

    func _physics_process(delta: float) -> void:
        # runs every frame
  • _process(delta) L11 · Lives System
    What it does

    a function Godot runs every time the screen is drawn.

    Use it

    for things that only change what is on screen, like a label. Movement goes in _physics_process.

    func _process(_delta):
        $ScoreLabel.text = "Score: " + str(score)
  • delta L02 · Platformer Movement
    What it does

    how many seconds passed since the last frame. A small number, around 0.016.

    Use it

    whenever a value grows over time, so the game runs at the same speed on a fast computer and a slow one.

    position.x += 200 * delta

Read the keyboard and mouse

5
  • Input.get_axis() L02 · Platformer Movement
    What it does

    reads two opposite actions as one number. It gives back -1, 0 or 1.

    Use it

    when movement runs along a single line, so one number can say which way and how much.

    var amount = Input.get_axis("negative_action", "positive_action")
  • Input.get_vector() L01 · Top-Down Movement
    What it does

    reads four actions at once as one Vector2 arrow. Its length is never more than 1, so a diagonal is not faster than a straight line.

    Use it

    when something can move in all four directions, like a top-down player. For a single line of movement, Input.get_axis() is enough.

    var direction = Input.get_vector("left_action", "right_action", "up_action", "down_action")
  • Input.is_action_just_pressed() L02 · Platformer Movement
    What it does

    true on the ONE frame a key goes down, and false while it is held.

    Use it

    for something that should happen once per press. The plain is_action_pressed() stays true the whole time the key is held.

    if Input.is_action_just_pressed("action_name"):
        print("pressed")
  • Input.is_mouse_button_pressed() L04 · Shooting Bullets
    What it does

    true the whole time a mouse button is held down.

    Use it

    for holding the button, like a machine gun. For one shot per click, use Input.is_action_just_pressed("shoot") instead: "shoot" is the left mouse button.

    if Input.is_mouse_button_pressed(MOUSE_BUTTON_LEFT):
        print("holding")
  • get_global_mouse_position() L03 · Mouse Aiming
    What it does

    where the mouse is right now, as a spot in the game.

    Use it

    whenever something needs to know where the mouse is.

    crosshair.global_position = get_global_mouse_position()

Move things

4
  • velocity L02 · Platformer Movement
    What it does

    how fast a CharacterBody2D moves AND which way. velocity.x is left and right, velocity.y is up and down. Negative x is left. Negative y is UP.

    Use it

    to decide where a body is heading. Setting it moves nothing on its own.

    velocity.x = 150
    velocity.y = -400
  • move_and_slide() L02 · Platformer Movement
    What it does

    the line that actually moves the body, using whatever velocity is holding right now.

    Use it

    at the end of _physics_process, after velocity is set. Without it nothing moves, however carefully velocity was worked out.

    move_and_slide()
  • is_on_floor() L02 · Platformer Movement
    What it does

    true when the body's feet are touching something solid. Only a CharacterBody2D has it.

    Use it

    to tell standing from falling.

    if is_on_floor():
        print("standing")
  • position += direction L04 · Shooting Bullets
    What it does

    move a node a little in a direction, every frame.

    Use it

    in _physics_process, for anything that keeps moving the same way, like a cloud drifting or a car driving.

    var direction = Vector2.LEFT
    position += direction * 100 * delta

Turn and face

4
  • look_at() L03 · Mouse Aiming
    What it does

    turn a node so its front points at a spot. The front is the side that faces right in the editor.

    Use it

    when something should face a spot, like a turret that follows an enemy. Put it in _physics_process to keep facing it.

    look_at(enemy.global_position)
  • rotation L04 · Shooting Bullets
    What it does

    which way a node is turned. On its own it means THIS node. With a name in front, it means that other node.

    Use it

    to make one node face the same way as another.

    arrow.rotation = rotation
  • rotated() L04 · Shooting Bullets
    What it does

    turn an arrow by an angle.

    Use it

    to get an arrow that points the way something is facing.

    # Vector2.RIGHT points right. A quarter turn makes it point down.
    var down = Vector2.RIGHT.rotated(PI / 2)
  • sprite.flip_h L02 · Platformer Movement
    What it does

    mirror a picture left to right. true is mirrored, false is the way it was drawn.

    Use it

    when art is drawn facing one way and the thing can go both ways.

    sprite.flip_h = true

Notice a touch

3
  • .connect() L03 · Mouse Aiming
    What it does

    hook a signal up to a function, so the function runs every time the signal is sent.

    Use it

    in _ready(), for a signal like body_entered, area_entered, pressed or timeout.

    func _ready():
        area_entered.connect(_on_area_entered)
    
    func _on_area_entered(area):
        print("touched")
  • is_in_group() L08 · Collect Coins and Items
    What it does

    ask a node if it is in a group, like "player".

    Use it

    to check what touched an area, so only the right thing sets it off.

    if area.is_in_group("coins"):
        print("got a coin")
  • class_name L04 · Shooting Bullets
    What it does

    give a script a name the whole game knows. Another script can then ask if a node "is" one.

    Use it

    when one script needs to know what kind of thing it touched.

    # in coin.gd, at the very top
    class_name Coin
    
    # in any other script
    if area is Coin:
        print("got a coin")

Keep track of numbers and spots

3
  • += L02 · Platformer Movement
    What it does

    add to what a value already holds, instead of replacing it.

    Use it

    when something should build up over time rather than jump straight to a number.

    score += 1
  • @export L02 · Platformer Movement
    What it does

    puts a value in the Inspector so it can be changed without opening the script.

    Use it

    for numbers worth tuning, like a speed or a jump height.

    @export var speed: float = 200.0
  • Vector2 L11 · Lives System
    What it does

    a spot in the game, as two numbers: x (across) and y (down).

    Use it

    for a spot, like where something starts or comes back to. ZERO is the spot (0, 0).

    var start_spot = Vector2(300, 120)
    position = Vector2.ZERO

Make copies

3
  • preload() L04 · Shooting Bullets
    What it does

    load a scene file once, when the script is read, so it is ready to copy.

    Use it

    at the top of a script, for a scene you will make copies of.

    const COIN_SCENE = preload("res://coin.tscn")
  • instantiate() L04 · Shooting Bullets
    What it does

    make one real copy of a loaded scene.

    Use it

    each time you want a new one, like a coin, an arrow or an enemy. The copy is not in the game yet.

    var coin = COIN_SCENE.instantiate()
  • add_child() L04 · Shooting Bullets
    What it does

    put a node into the game, under another node.

    Use it

    right after instantiate(), or nothing appears. Adding to get_parent() puts the copy beside this node, so it does not move or turn along with it.

    get_parent().add_child(coin)

Reference another node

2
  • @onready L02 · Platformer Movement
    What it does

    wait until the scene is built, then grab a node once and keep it in a variable.

    Use it

    at the top of a script, so a node is not looked up every frame.

    @onready var sprite: Sprite2D = $Sprite2D
  • get_node() L11 · Lives System
    What it does

    find another node by its name.

    Use it

    with @onready, to reach a node outside this scene, like an enemy in the level. get_parent() is the node this one sits under.

    @onready var enemy = get_parent().get_node("Enemy")

Show words and numbers

1
  • str() L11 · Lives System
    What it does

    turn a number into text, so it can be joined to words with +.

    Use it

    when a label shows a number, like a score or a timer.

    $ScoreLabel.text = "Score: " + str(score)

Change scenes

1
  • change_scene_to_file() L13 · Multiple Levels
    What it does

    swap the whole game for another scene.

    Use it

    to go to a win screen, a lose screen or the next level. Inside a body_entered function, put call_deferred after it, as below, so Godot finishes the touch first.

    get_tree().change_scene_to_file.call_deferred("res://level_2.tscn")