Godot Quick Reference Notes
Low Level
Print to console
print("Test called")
Preload a scene
# Returns a Resource from the filesystem located at path.# During run-time, the resource is loaded when the script is being parsed.# This function effectively acts as a reference to that resource.var platform_scene: PackedScene = preload("res://scenes/platform.tscn")
Instantiate preload PackedScene
#var child = packe_scene.instantiate()child.global_position = locationparent.add_child(child)
GDScript Language
Set ClassName Node type
class_name Player
Create class Instance
var rect_shape:RectangleShape2D = RectangleShape2D.new()
Cast in place
# Cast the value to a given type if possible.(body as Player).jump(jump_force)
Set script instance at startup
@onready var animated_sprite: AnimatedSprite2D = $AnimatedSprite2D
Connect Signal
trap.touched_player.connect(_on_trap_touch_player)
Emit Signal
signal touched_player...touched_player.emit()
Switch action on String
match button.path_id: "title/play": start_game.emit() _ print("default")
Check if instance is Type
# Tests whether a variable extends a given class, or is of a given built-in type.if (body is Player): animated_sprite.play("jump")
Character
Key CharacterBody2D methods
is_on_floor()move_and_slide()
Jump up
func jump(force: float): velocity.y = -force
Node
Await on Timer
# Returns a new SceneTreeTimer. After time_sec in seconds have passed,# the timer will emit SceneTreeTimer.timeout and will be automatically freed.await get_tree().create_timer(1.0).timeout
Quit Game
get_tree().quit()
Reload Current Scene
get_tree().reload_current_scene()
Queue for delete
node.queue_free()
Call method/property on group of Nodes
# Calls method on each node inside this tree added to the given group.# You can pass arguments to method by specifying them at the end of this method call.get_tree().call_group("buttons", "set_disabled", disabled)
Deffer call on Node
# Assigns value to the given property, at the end of the current frame.# This is equivalent to calling set() through call_deferred().shape.set_deferred("disabled", true);
Get Node children
# Returns all children of this node inside an Array.var children: Array[Node] = parent.get_children()for child in children: child.queue_free()
Add a child to Node
# Adds a child node. Nodes can have any number of children, but every child must have a unique name.parent.add_child(child)
Area2D Get Overlapping Areas
# Returns a list of intersecting Area2Ds.# The overlapping area's CollisionObject2D.collision_layer must be part of this area's# CollisionObject2D.collision_mask in order to be detected.var overlapping_areas = area.get_overlapping_areas()if (overlapping_areas.size() > 0): for area in overlapping_areas: print(area)
CanvasItem
Show / Hide Node
- https://docs.godotengine.org/en/stable/classes/class_canvasitem.html#class-canvasitem-property-visible
- https://docs.godotengine.org/en/stable/classes/class_canvasitem.html#class-canvasitem-method-show
- https://docs.godotengine.org/en/stable/classes/class_canvasitem.html#class-canvasitem-method-hide
# If true, this CanvasItem may be drawn.# Whether this CanvasItem is actually drawn depends on the visibility of all of its CanvasItem ancestors.hud.visible = false# Show the CanvasItem if it's currently hidden. This is equivalent to setting visible to true.hud.show()# Hide the CanvasItem if it's currently visible. This is equivalent to setting visible to false.hud.hide()
SceneTree
Pause Scene Tree
# If true, the scene tree is considered paused.get_tree().paused = true
Input
Input Action
- https://docs.godotengine.org/en/stable/classes/class_input.html#class-input-method-is-action-pressed
- https://docs.godotengine.org/en/stable/classes/class_input.html#class-input-method-is-action-just-pressed
- https://docs.godotengine.org/en/stable/classes/class_input.html#class-input-method-get-axis
# Returns true if you are pressing the action event.Input.is_action_pressed("quit")# Returns true when the user has started pressing the action event in the current frame or physics tick.Input.is_action_just_pressed("Quit")# Get axis input by specifying two actions, one negative and one positive.Input.get_axis("MoveLeft", "MoveRight")
Animation
Play AnimationPlayer
@onready var animator = $AnimationPlayer...# Plays the animation with key name. Custom blend times and speed can be set.animator.play("jump")
Viewport
Get the ViewPort rectangle and size
# Returns the viewport's boundaries as a Rect2.var view_port_rect: Rect = get_viewport_rect()var viewport_size: Vector2 = view_port_rect.size
Window / Display
Window event Callback
Events
WINDOW_EVENT_MOUSE_ENTER
WINDOW_EVENT_MOUSE_EXIT
WINDOW_EVENT_FOCUS_IN
WINDOW_EVENT_FOCUS_OUT
WINDOW_EVENT_CLOSE_REQUEST
WINDOW_EVENT_GO_BACK_REQUEST
WINDOW_EVENT_DPI_CHANGE
WINDOW_EVENT_TITLEBAR_CHANGE
# Sets the callback that will be called when an event occurs in the window specified by window_id.DisplayServer.window_set_window_event_callback(_on_window_event)...func _on_window_event(event): print("New Window Event " + str(event)) match event: DisplayServer.WINDOW_EVENT_FOCUS_IN: print("focus in") DisplayServer.WINDOW_EVENT_FOCUS_OUT: print("focus out") DisplayServer.WINDOW_EVENT_CLOSE_REQUEST: print("close request")
Get Display’s safe area
# Returns the unobscured area of the display where interactive controls should be rendered.var safe_area:Rect2 = DisplayServer.get_display_safe_area()