AudioStreamPlayer
node plays sounds.AnimationPlayer
plays animations.Sprite2D
node displays a sprite.Button
node creates a clickable button.CharacterBody2D
node moves and detects collisions with the environment.CollisionShape2D
node defines the character's collision shape and how it interacts with the environment.Sprite2D
node displays the character's visual representation.AnimationPlayer
node plays animations for the character.Player
node to define how the player moves, jumps, and interacts with the environment. The script can handle player input, control when to play animations, sounds, and define other game logic.get_node()
function to get a reference to a node. The function takes one argument: the path to the node you want to get. The path is the names of the child nodes separated by forward slashes (/
).func _ready() -> void:
var hitbox: Area3D = get_node("HitBox3D")
var camera: Camera3D = get_node("Pivot/Camera3D")
_ready()
function is called when the node and all its children were added to the scene tree and their _ready()
function was called, meaning they are ready to be used. It's a good place to get references to other nodes because you're sure they exist._ready()
function and how nodes are created in the study guide Node creation and the ready function.get_node()
function in GDScript: the dollar sign ($
). You can write node paths after the dollar sign without quotes. The following function calls are equivalent. First, with get_node()
:func _ready() -> void:
var hitbox: Area3D = get_node("HitBox3D")
var camera: Camera3D = get_node("Pivot/Camera3D")
func _ready() -> void:
var hitbox: Area3D = $HitBox3D
var camera: Camera3D = $Pivot/Camera3D
$"Player Healthbar"
.get_node()
in your code. To limit this problem, you can mark nodes as having a unique name in a scene. You can then reference the node using only its name in a script used within the same scene, regardless of where the node is in the scene tree.func _ready() -> void:
var camera: Camera3D = %Camera3D
@onready var hitbox: Area3D = $HitBox3D
@onready var camera: Camera3D = $Pivot/Camera3D
@onready
are initialized right before the _ready()
function call. Using the @onready
annotation is a shorthand for defining empty member variables and calling get_node()
at the top of _ready()
function:var hitbox: Area3D
var camera: Camera3D
func _ready() -> void:
hitbox = $HitBox3D
camera = $Pivot/Camera3D
@export
annotation before the variable declaration. You can then set the node reference in the inspector:@export var hitbox: Area3D
@export var camera: Camera3D
get_node()
for it.get_node(".").position
, which asks Godot to get the current node and then its position, has the same effect as writing just position
, with an unnecessary performance cost./
) followed by root
, the name of the root node, and then the names of the child nodes separated by forward slashes.get_node_or_null()
function to get a reference to a node using its absolute path safely. The function returns null
if the node doesn't exist.func _ready() -> void:
var hitbox: Area3D = get_node_or_null("/root/Player/HitBox3D")
get_parent()
function. The function returns the parent of the node on which it's called.func _ready() -> void:
var parent_node := get_parent()
var siblings := parent_node.get_children()
func _autodetect_shape() -> void:
var parent := get_parent() as CollisionShape
assert(has_valid_parent(), "Parent at '%s' isn't a valid CollisionShape" % [parent.get_path()])
if parent.shape is SphereShape:
shape_type = ShapeType.SPHERE
shape_size = Vector3(parent.shape.radius, parent.shape.radius, parent.shape.radius)
elif parent.shape is BoxShape:
shape_type = ShapeType.BOX
shape_size = parent.shape.extents
elif parent.shape is CapsuleShape:
shape_type = ShapeType.CAPSULE
shape_size = Vector3(parent.shape.radius, parent.shape.height, parent.shape.radius)
else:
push_error("Shape '%s' at '%s' isn't a supported shape" % [parent.shape, parent.get_path()])
add_sibling()
function as a shorthand for get_parent().add_child(node)
. This can be useful for mechanics like spawning items from a chest.# Items get set in the inspector.
@export var items: Array[PackedScene] = []
func spawn_items() -> void:
for item: PackedScene in items:
var new_item: Item = item.instance()
add_sibling(new_item)
new_item.global_position = global_position + Vector2(rand_range(-50, 50), rand_range(-50, 50))
SceneTree.get_nodes_in_group()
. It takes the group name as an argument and returns an array of nodes in that group.func kill_all_mobs_on_screen() -> void:
var screen_rect := get_viewport().get_visible_rect()
var mobs := get_tree().get_nodes_in_group("mobs")
for mob: Mob in mobs:
if screen_rect.has_point(mob.global_position):
mob.die()
Node.add_to_group()
method. The method takes the group name as an argument and adds the node to the group.func _ready() -> void:
add_to_group("mobs")
groups.gd
script:class_name Groups
const COLLECTIBLES := "collectibles"
collectible.gd
script:class_name Collectible extends Node2D
func _ready() -> void:
add_to_group(Groups.COLLECTIBLES)
Node.remove_from_group()
. It takes the group name as an argument and removes the node from the group.func _ready() -> void:
remove_from_group("mobs")
.tscn
extension. You can use this file to create new
instances of the scene in the editor or
at runtime.preload()
function to load a scene file as a packed scene resource.const PlayerScene := preload("res://player/player.tscn")
preload()
call for a resource by holding ctrl (or ⌘ on macOS) and clicking and dragging the resource from the FileSystem dock into the script.instantiate()
method on the loaded packed scene resource. The method creates a new instance of the scene in memory. The instance is a new node that you can add to the scene tree by calling the add_child()
method on the desired parent node.const BulletScene := preload("bullet.tscn")
var bullet: Bullet3D = BulletScene.instantiate()
add_child(bullet)
@export
annotation before the variable declaration and use the PackedScene
type hint.@export var enemy_scene: PackedScene
@export
annotation before the variable declaration and use the Array[PackedScene]
type hint.@export var enemy_scenes: Array[PackedScene]
@export var contained_items: Array[PackedScene]