The lines of code you have learned, and when to use each one. Change them to fit your game.
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 framehook 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")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.0wait 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 = $Sprite2Dadd 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 += 1put 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)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")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 * deltawhere 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()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")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")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")what Godot sends when a mouse button goes down or comes back up.
Use it for something that should happen once per click. event.pressed is true when the button goes down.
func _unhandled_input(event):
if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_RIGHT and event.pressed:
print("right click")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()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")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)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()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 * deltaload 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")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)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
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 = truehow 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