extends Node2D
# Class variables
var speed: float = 100.0
@export var health: int = 100
@onready var sprite = $Sprite2D
# Called when node enters scene tree
func _ready():
print("Ready!")
# Called every frame
func _process(delta: float):
position.x += speed * delta
# Called at fixed intervals (physics)
func _physics_process(delta: float):
pass # Basic types
var integer: int = 42
var floating: float = 3.14
var text: String = "Hello"
var flag: bool = true
# Collections
var array: Array = [1, 2, 3]
var dict: Dictionary = {"key": "value"}
var vector: Vector2 = Vector2(10, 20)
var vector3: Vector3 = Vector3(1, 2, 3)
# Type inference
var inferred := 100 # int
# Constants
const MAX_SPEED := 500
# Enums
enum State { IDLE, WALKING, JUMPING } # Get child node
var child = $ChildName
var child = get_node("ChildName")
# Get nested child
var nested = $Parent/Child/GrandChild
# Get parent
var parent = get_parent()
# Get tree root
var root = get_tree().root
# Find nodes
var nodes = get_tree().get_nodes_in_group("enemies")
var node = get_node_or_null("MaybeExists")
# @onready - assigns after _ready
@onready var player = $Player # Add/remove children
var node = Node2D.new()
add_child(node)
remove_child(node)
node.queue_free() # Safe delete
# Reparent
node.reparent(new_parent)
# Instance scene
var scene = preload("res://Enemy.tscn")
var instance = scene.instantiate()
add_child(instance)
# Groups
add_to_group("enemies")
remove_from_group("enemies")
is_in_group("enemies") func _process(delta):
# Check action state
if Input.is_action_pressed("move_right"):
position.x += speed * delta
if Input.is_action_just_pressed("jump"):
jump()
if Input.is_action_just_released("attack"):
end_attack()
# Get axis value (-1 to 1)
var horizontal = Input.get_axis("move_left", "move_right")
var vertical = Input.get_axis("move_up", "move_down")
var direction = Vector2(horizontal, vertical).normalized() func _input(event):
if event is InputEventKey:
if event.pressed and event.keycode == KEY_ESCAPE:
get_tree().quit()
if event is InputEventMouseButton:
if event.button_index == MOUSE_BUTTON_LEFT and event.pressed:
shoot()
if event is InputEventMouseMotion:
look_at(event.position)
func _unhandled_input(event):
# Only called if not handled by UI extends CharacterBody2D
const SPEED = 300.0
const JUMP_VELOCITY = -400.0
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
func _physics_process(delta):
# Apply gravity
if not is_on_floor():
velocity.y += gravity * delta
# Jump
if Input.is_action_just_pressed("jump") and is_on_floor():
velocity.y = JUMP_VELOCITY
# Horizontal movement
var direction = Input.get_axis("move_left", "move_right")
if direction:
velocity.x = direction * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
move_and_slide() # Area2D signals
func _on_area_2d_body_entered(body):
if body.is_in_group("player"):
print("Player entered")
func _on_area_2d_body_exited(body):
pass
# Raycast
var ray = $RayCast2D
if ray.is_colliding():
var collider = ray.get_collider()
var point = ray.get_collision_point()
# Direct space query
var space = get_world_2d().direct_space_state
var query = PhysicsRayQueryParameters2D.create(from, to)
var result = space.intersect_ray(query) # Define signal
signal health_changed(new_health: int)
signal died
# Emit signal
func take_damage(amount: int):
health -= amount
health_changed.emit(health)
if health <= 0:
died.emit()
# Connect signal in code
func _ready():
health_changed.connect(_on_health_changed)
# Or with callable
died.connect(func(): print("Player died"))
func _on_health_changed(new_health):
$HealthBar.value = new_health
# One-shot connection
signal.connect(callback, CONNECT_ONE_SHOT) @onready var anim = $AnimationPlayer
func _process(delta):
if velocity.x != 0:
anim.play("walk")
else:
anim.play("idle")
# Animation methods
anim.play("attack")
anim.play_backwards("attack")
anim.stop()
anim.pause()
anim.queue("next_anim")
anim.seek(1.5) # Jump to time
anim.speed_scale = 2.0
# Check state
anim.is_playing()
anim.current_animation # Create tween
var tween = create_tween()
# Animate properties
tween.tween_property(self, "position", Vector2(100, 100), 1.0)
tween.tween_property($Sprite2D, "modulate:a", 0.0, 0.5)
# Chaining
tween.tween_property(self, "scale", Vector2(2, 2), 0.5)
tween.tween_property(self, "scale", Vector2(1, 1), 0.5)
# Parallel
tween.set_parallel(true)
# Easing
tween.set_ease(Tween.EASE_IN_OUT)
tween.set_trans(Tween.TRANS_BOUNCE)
# Callbacks
tween.tween_callback(func(): print("Done")) | F5 | Run project |
| F6 | Run current scene |
| F7 | Pause |
| F8 | Stop |
| Ctrl+S | Save scene |
| Ctrl+Shift+S | Save all |
| Q | Select mode |
| W | Move mode |
| E | Rotate mode |
| S | Scale mode |