module Eagle::SceneTree

Overview

The global tree of nodes that Eagle updates and draws every frame.

Everything you add under SceneTree.root gets its process, physics_process, draw and input hooks called automatically. Use .change_scene to switch between screens such as a title menu and a level, and groups to reach many nodes at once.

class Level < Node2D
  def ready : Nil
    3.times { |i| add(Sprite2D.new(Texture.new(Image.circle(8, Color::RED)), v2(100 + i * 50, 100)).add_to_group("enemies")) }
  end
end

SceneTree.change_scene(Level.new)
SceneTree.call_group("enemies") { |n| n.queue_free }
SceneTree.paused = true # stops nodes whose process_mode is Pausable or Inherit

Defined in:

eagle/core/scene_tree.cr

Class Method Summary

Class Method Detail

def self.add(node : Node) : Node #

Adds node to the root. Same as SceneTree.root.add(node).


def self.call_group(name : String, & : Node -> ) : Nil #

Yields every node in the group. Safe even if the block removes nodes.


def self.change_scene(scene : Node) : Nil #

Replaces the current scene with scene at the end of the frame. The old scene is removed. Safe to call from inside the old scene's callbacks.


def self.change_scene!(scene : Node) : Nil #

Replaces the current scene immediately. Prefer .change_scene unless you know no callbacks are running.


def self.current_scene : Node | Nil #

The scene set by .change_scene, if any.


def self.defer(&block : -> ) : Nil #

Runs the block at the end of the current frame, after all nodes have processed. Use it to change the tree safely from inside a callback.


def self.group(name : String) : Array(Node) #

Every node in the tree that belongs to the group name.


def self.node_count : Int32 #

Total number of nodes under the root, for debug overlays.


def self.paused=(v : Bool) #

Pauses or resumes the tree. Paused nodes skip process and physics_process but still draw. Nodes with ProcessMode::Always keep running, which suits pause menus.


def self.paused? : Bool #

True while the tree is paused.


def self.root : Node #

The top node. Add your scenes and global nodes (HUD, music player) here.