class
Eagle::Node
- Eagle::Node
- Reference
- Object
Overview
The building block of Eagle's scene tree. Everything in a scene is a node: sprites, cameras, bodies, sounds, UI widgets and your own game objects.
Nodes form a tree. Adding a node under SceneTree.root (directly or through a parent)
brings it to life, and Eagle then calls its hooks:
#readyonce, after it and its children have entered the tree#process(dt)every frame#physics_process(dt)at the fixed rate#draw(g)every frame, in tree order, with#z_indexbreaking ties among siblings#input(event)for each event, deepest node first
Subclass a node type and override the hooks you need. Plain Node has no position;
use Node2D or Node3D for things that live in space.
class Blinker < Node2D
@t = 0.0
def ready : Nil
add(Label.new("hello"))
end
def process(dt : Float32) : Nil
@t += dt
self.visible = @t % 1 < 0.5
queue_free if @t > 10
end
end
blinker = Blinker.new
blinker.position = v2(200, 100)
SceneTree.root.add(blinker)
Nodes are found by name with #get_node("Player/Sprite"), by type with #find_all, and
by group with #add_to_group plus SceneTree.group.
Direct Known Subclasses
- Eagle::AudioPlayer
- Eagle::CanvasLayer
- Eagle::CollisionShape2D
- Eagle::Node2D
- Eagle::Node3D
- Eagle::SceneTree::RootNode
- Eagle::Timer
Defined in:
eagle/core/node.crConstructors
-
.new(name : String = "")
Creates a node.
Instance Method Summary
-
#<<(child : Node) : self
Adds child and returns self, so additions can be chained.
-
#[](path : String) : Node
Short form of
#get_node. -
#[]?(path : String) : Node | Nil
Short form of
#get_node?. -
#add(child : Node) : Node
--- tree manipulation ---------------------------------------------------- Adds child as the last child and returns it.
-
#add(*children : Node) : Nil
Adds several children in order.
-
#add_child(child : Node) : Node
Same as
#add, for readers coming from Godot. -
#add_to_group(group : String) : self
--- groups --------------------------------------------------------------- Adds this node to a named group and returns self.
-
#ancestor_of?(node : Node) : Bool
True when node is somewhere below this one.
-
#can_process? : Bool
--- processing ----------------------------------------------------------- True when this node should run
#processright now, given pause state and#process_mode. -
#child?(name : String) : Node | Nil
The direct child called name, or
nil. -
#child_added : Eagle::Emitter(Node)
Emitted after a child is added, with the child.
-
#child_count : Int32
Number of direct children.
-
#child_removed : Eagle::Emitter(Node)
Emitted after a child is removed, with the child.
-
#children : Array(Eagle::Node)
Child nodes in the order they were added.
-
#children_of(type : T.class) : Array(T) forall T
Direct children of the given type.
-
#clear_children : Nil
Removes every child.
-
#draw(g : Graphics) : Nil
Draws this node.
-
#dump(io : IO = STDOUT, indent = 0) : Nil
Prints the subtree with indentation, which is handy when debugging.
-
#each_ancestor(& : Node -> ) : Nil
Yields the parent, then its parent, up to the root.
-
#each_child(& : Node -> ) : Nil
Yields each direct child.
-
#each_descendant(&block : Node -> ) : Nil
Yields every descendant, depth-first.
-
#emit_child_added(*args) : Nil
Emitted after a child is added, with the child.
-
#emit_child_removed(*args) : Nil
Emitted after a child is removed, with the child.
-
#emit_tree_entered(*args) : Nil
Emitted each time the node enters the tree.
-
#emit_tree_exited(*args) : Nil
Emitted each time the node leaves the tree.
-
#enter_tree : Nil
Called every time the node is attached to the tree, before
#ready. -
#exit_tree : Nil
Called every time the node leaves the tree.
-
#find(name : String) : Node | Nil
The first descendant with this name, searched depth-first.
-
#find_all(type : T.class) : Array(T) forall T
Every descendant of the given type.
-
#first_child : Node | Nil
The first child, or
nil. -
#free : Nil
Removes this node right away.
-
#get_node(path : String, type : T.class) : T forall T
Finds a node by path and casts it, so you get a typed result.
-
#get_node(path : String) : Node
Finds a node by path and raises if it doesn't exist.
-
#get_node?(path : String) : Node | Nil
Finds a node by path, or returns
nil. -
#groups : Set(String)
Names of the groups this node belongs to.
-
#in_group?(group : String) : Bool
True when this node belongs to group.
-
#in_tree? : Bool
True while the node is attached, directly or indirectly, to
SceneTree.root. -
#input(event : Event) : Nil
Receives input events.
-
#name : String
The node's name, used by
#get_nodepaths. -
#name=(name : String)
The node's name, used by
#get_nodepaths. -
#on_child_added(&block : Node -> Nil) : Proc(Node, Nil)
Emitted after a child is added, with the child.
-
#on_child_removed(&block : Node -> Nil) : Proc(Node, Nil)
Emitted after a child is removed, with the child.
-
#on_tree_entered(&block : -> Nil) : Proc(Nil)
Emitted each time the node enters the tree.
-
#on_tree_exited(&block : -> Nil) : Proc(Nil)
Emitted each time the node leaves the tree.
-
#parent : Node | Nil
The parent node, or
nilif this node hasn't been added anywhere. -
#path : String
The node's path from the root, such as
"/root/Level/Player". -
#physics_process(dt : Float32) : Nil
Called at the fixed rate (
Config#fixed_fps), after the physics world steps. -
#process(dt : Float32) : Nil
Called every frame with the frame time in seconds.
-
#process_mode : Eagle::Node::ProcessMode
Controls behavior while the tree is paused; see
ProcessMode. -
#process_mode=(process_mode : Eagle::Node::ProcessMode)
Controls behavior while the tree is paused; see
ProcessMode. -
#queue_free : Nil
Removes this node at the end of the frame.
-
#queued_free? : Bool
True after
#queue_freeand before the removal happens. -
#ready : Nil
--- overridable hooks ---------------------------------------------------- Called once, the first time this node enters the tree, after its children are ready.
-
#ready_called? : Bool
True once
#readyhas run. -
#remove(child : Node) : Nil
Detaches child right away.
-
#remove_child(child : Node) : Nil
Same as
#remove. -
#remove_from_group(group : String) : Nil
Removes this node from a group.
-
#remove_from_parent : Nil
Detaches this node from its parent right away.
-
#resized(width : Int32, height : Int32) : Nil
Called on every node in the tree when the window is resized.
-
#root : Node
The topmost ancestor.
-
#to_s(io : IO) : Nil
Appends a short String representation of this object which includes its class name and its object address.
-
#tree_entered : Eagle::Emitter()
Emitted each time the node enters the tree.
-
#tree_exited : Eagle::Emitter()
Emitted each time the node leaves the tree.
-
#visible=(visible : Bool)
Hidden nodes and their children are not drawn.
-
#visible? : Bool
Hidden nodes and their children are not drawn.
-
#z_index : Int32
Draw order among siblings: higher values draw later, on top.
-
#z_index=(z_index : Int32)
Draw order among siblings: higher values draw later, on top.
Constructor Detail
Instance Method Detail
--- tree manipulation ----------------------------------------------------
Adds child as the last child and returns it. If this node is in the tree, the child
enters too and gets #ready. Raises if child already has a parent.
enemy = Node2D.new("Enemy")
SceneTree.root.add(enemy).add(Sprite2D.new(Texture.new(Image.circle(8, Color::RED))))
--- groups ---------------------------------------------------------------
Adds this node to a named group and returns self. Groups make it easy to act on many
nodes at once with SceneTree.group and SceneTree.call_group.
--- processing -----------------------------------------------------------
True when this node should run #process right now, given pause state and #process_mode.
Prints the subtree with indentation, which is handy when debugging.
Emitted each time the node enters the tree. Connect with on_tree_entered { ... }.
Every descendant of the given type.
SceneTree.root.find_all(Sprite2D).each { |s| s.modulate = Color::RED }
Finds a node by path and casts it, so you get a typed result.
class Player < Node2D
def ready : Nil
add(Label.new("P1", name: "Tag"))
get_node("Tag", Label).text = "Player 1"
end
end
Finds a node by path, or returns nil. Paths work like file paths:
"Sprite", "Player/Gun", "../Sibling", and "/root/Hud" from the top.
Receives input events. Set event.handled = true to stop them from reaching other nodes.
Emitted after a child is added, with the child.
Emitted after a child is removed, with the child.
Emitted each time the node enters the tree. Connect with on_tree_entered { ... }.
Called at the fixed rate (Config#fixed_fps), after the physics world steps.
Move physics bodies and read contacts here.
Called every frame with the frame time in seconds. Put per-frame logic here.
Controls behavior while the tree is paused; see ProcessMode.
Controls behavior while the tree is paused; see ProcessMode.
Removes this node at the end of the frame. This is the safe way for a node to delete
itself from inside #process, a signal handler or a collision callback.
--- overridable hooks ---------------------------------------------------- Called once, the first time this node enters the tree, after its children are ready. Build child nodes, look up siblings and connect signals here.
True once #ready has run. It runs only the first time the node enters the tree.
Called on every node in the tree when the window is resized.
Appends a short String representation of this object which includes its class name and its object address.
class Person
def initialize(@name : String, @age : Int32)
end
end
Person.new("John", 32).to_s # => #<Person:0x10a199f20>
Emitted each time the node enters the tree. Connect with on_tree_entered { ... }.