Skip to content

Commit

Permalink
Added this and this_dyn functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
LunaticWyrm467 committed Nov 2, 2024
1 parent 6f3a866 commit 3df9a72
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions node_tree_core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "node_tree"
version = "0.8.0"
version = "0.8.1"
edition = "2021"
rust-version = "1.78"

Expand All @@ -13,7 +13,7 @@ exclude = [
keywords = ["framework", "utility", "gamedev"]
repository = "https://github.com/LunaticWyrm467/node_tree"
description = """
An extendable system made up of autonomous execution services known as nodes organized in a tree of processes.
An extendable scene graph made up of autonomous execution services known as nodes organized in a tree of processes.
Inspired by Godot!
"""
license = "MIT OR Apache-2.0"
Expand Down
31 changes: 31 additions & 0 deletions node_tree_core/src/structs/node_base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,37 @@ impl NodeBase {
depth: 0
}
}

/// Returns a `Tp<T>` pointer for itself.
///
/// # Panics
/// The correct type (type of `self`) will have to be provided, or this function will panic!
/// Panics also if this Node is not connected to a `NodeTree`.
pub fn this<S: Node>(&self) -> Tp<S> {
if self.tree.is_none() {
panic!("Cannot get a tree pointer to a node that is not in a `NodeTree`!");
}

unsafe {
Tp::new(self.tree.unwrap_unchecked(), self.rid, self.rid).expect("Called `this` with the wrong type provided")
}
}

/// Returns a `TpDyn` pointer for itself.
///
/// # Panics
/// Panics if this Node is not connected to a `NodeTree`.
pub fn this_dyn(&self) -> TpDyn {
if self.tree.is_none() {
panic!("Cannot get a tree pointer to a node that is not in a `NodeTree`!");
}

// SAFETY:
// There are no invariants that could result in this being unwrapped on an `Err` value.
unsafe {
TpDyn::new(self.tree.unwrap_unchecked(), self.rid, self.rid).unwrap_unchecked()
}
}

/// Gets the name of the node.
/// Each name is unique within the context of the parent's children vector.
Expand Down
10 changes: 9 additions & 1 deletion node_tree_core/tests/signals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,20 @@ class! {

hk ready(&mut self) {
let child: Tp<NodeB> = self.get_child(0).unwrap();
let this: Tp<NodeA> = self.this();

connect! { on_event -> child.listener };
connect! { on_event -> this.listener };
}

hk process(&mut self, _delta: f32) {
self.on_event.emit(self.count);
self.count += 1;
}

fn listener(&self, count: &u8) {
self.post(Log::Warn(&format!("Count is {count}")));
}
}


Expand All @@ -26,7 +33,8 @@ class! {

fn listener(&self, count: &u8) {
if *count == 3 {
panic!("This was successful!");
self.post(Log::Panic("This was successful!"));
panic!();
}
}
}
Expand Down

0 comments on commit 3df9a72

Please sign in to comment.