a function Godot runs over and over, about 60 times a second. Movement and physics go in here.
Use itfor anything that has to keep happening while the game runs.
func _physics_process(delta: float) -> void:
# runs every framea function Godot runs every time the screen is drawn.
Use itfor things that only change what is on screen, like a label. Movement goes in _physics_process.
func _process(_delta):
$ScoreLabel.text = "Score: " + str(score)how many seconds passed since the last frame. A small number, around 0.016.
Use itwhenever a value grows over time, so the game runs at the same speed on a fast computer and a slow one.
position.x += 200 * deltareads two opposite actions as one number. It gives back -1, 0 or 1.
Use itwhen 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")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.
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")true on the ONE frame a key goes down, and false while it is held.
Use itfor 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")true the whole time a mouse button is held down.
Use itfor 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")where the mouse is right now, as a spot in the game.
Use itwhenever something needs to know where the mouse is.
crosshair.global_position = get_global_mouse_position()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.
to decide where a body is heading. Setting it moves nothing on its own.
velocity.x = 150
velocity.y = -400the line that actually moves the body, using whatever velocity is holding right now.
Use itat the end of _physics_process, after velocity is set. Without it nothing moves, however carefully velocity was worked out.
move_and_slide()true when the body's feet are touching something solid. Only a CharacterBody2D has it.
to tell standing from falling.
if is_on_floor():
print("standing")move a node a little in a direction, every frame.
Use itin _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 * deltaturn a node so its front points at a spot. The front is the side that faces right in the editor.
Use itwhen 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)which way a node is turned. On its own it means THIS node. With a name in front, it means that other node.
Use itto make one node face the same way as another.
arrow.rotation = rotation
turn an arrow by an angle.
Use itto 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)mirror a picture left to right. true is mirrored, false is the way it was drawn.
Use itwhen art is drawn facing one way and the thing can go both ways.
sprite.flip_h = truehook a signal up to a function, so the function runs every time the signal is sent.
Use itin _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")ask a node if it is in a group, like "player".
Use itto check what touched an area, so only the right thing sets it off.
if area.is_in_group("coins"):
print("got a coin")give a script a name the whole game knows. Another script can then ask if a node "is" one.
Use itwhen 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")add to what a value already holds, instead of replacing it.
Use itwhen something should build up over time rather than jump straight to a number.
score += 1puts a value in the Inspector so it can be changed without opening the script.
Use itfor numbers worth tuning, like a speed or a jump height.
@export var speed: float = 200.0a spot in the game, as two numbers: x (across) and y (down).
Use itfor a spot, like where something starts or comes back to. ZERO is the spot (0, 0).
var start_spot = Vector2(300, 120)
position = Vector2.ZEROload a scene file once, when the script is read, so it is ready to copy.
Use itat the top of a script, for a scene you will make copies of.
const COIN_SCENE = preload("res://coin.tscn")make one real copy of a loaded scene.
Use iteach 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()put a node into the game, under another node.
Use itright 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)wait until the scene is built, then grab a node once and keep it in a variable.
Use itat the top of a script, so a node is not looked up every frame.
@onready var sprite: Sprite2D = $Sprite2Dfind another node by its name.
Use itwith @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")turn a number into text, so it can be joined to words with +.
Use itwhen a label shows a number, like a score or a timer.
$ScoreLabel.text = "Score: " + str(score)swap the whole game for another scene.
Use itto 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")