Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support (or document) downcasting Nodes to scripts #328

Closed
wyattjsmith1 opened this issue Apr 20, 2020 · 4 comments
Closed

Support (or document) downcasting Nodes to scripts #328

wyattjsmith1 opened this issue Apr 20, 2020 · 4 comments

Comments

@wyattjsmith1
Copy link

I am guessing this is supported and just not documented (or I'm blind), but I would like a way to "cast" a Node to a game script / rust struct. For example:

#[derive(NativeClass)]
#[inherit(Node)]
pub struct TestClass;

#[methods]
impl TestClass{

    fn _init(_owner: Node) -> Self {
        return TestClass;
    }

    pub fn test_method(&self) {
        godot_print!("Method Called");
    }
}

// Then, somewhere in another `impl` where "root/TestClass" is properly implemented by a TestClass.
self.get_node(NodePath::from_str("root/TestClass")).cast_to_test_class().test_method();

I understand that there is a cast() method which sounds great, but doesn't work for me. When I try let result: TestClass= self.get_node(NodePath::from_str("root/TestClass")).unwrap().cast().unwrap();, I get the following error:

error[E0277]: the trait bound `input::TestClass: gdnative_core::object::GodotObject` is not satisfied
  --> godot_utils\src\singleton.rs:20:120
   |
20 |         let result: TestClass= self.get_node(NodePath::from_str("root/TestClass")).expect("").cast().expect("");
   |                                                                                               ^^^^ the trait `gdnative_core::object::GodotObject` is not implemented for `input::TestClass`

From a cpp/oop perspective, this doesn't make sense because TestClass is a Node which extends GodotObject. From a Rust perspective, however, this kinda makes sense because TestClass doesn't actually imelement GodotObject
Ultimately, I understand Rust doesn't have an inheritance tree like most OO languages, but I am not sure how to get the behavior I want. Any help would be appreciated.

@ghost
Copy link

ghost commented Apr 20, 2020

Try looking for Instance<T> in rustdoc? It is documented, but admittedly hard to find if you don't know what to look for. There are plans to clean up the top-level namespace in the next release: #304

@wyattjsmith1
Copy link
Author

wyattjsmith1 commented Apr 20, 2020 via email

@ghost
Copy link

ghost commented Apr 20, 2020

You have to access it using map due to how script storage work. See module-level documentation on user_data for details. Note that type arguments can be () if you don't want a return value.

@ghost
Copy link

ghost commented Apr 24, 2020

Superseded by #330.

@ghost ghost closed this as completed Apr 24, 2020
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant