-
-
Notifications
You must be signed in to change notification settings - Fork 207
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
How to #[export] Sprite2D
or any node reference type?
#972
Comments
You have to wrap it in the Gd<> smart pointer e.g |
I did read the book, and I couldn't find any mention of how this would be done in the book, I also looked at the API docs, and there is no mention in there either (that I could find). Okay, so using the I thought I could do this, but I guess not: fn init(base: Base<Node2D>) -> Self {
Self {
base,
sprite: Gd::<Sprite2D>::default(),
}
} |
In addition to the book, check also the API docs, specifically about Btw, if you want to use late initialization, consider using |
Cool Thanks! This is what I am using: impl INode2D for Planet {
fn init(base: Base<Node2D>) -> Self {
Self {
base,
sprite: Sprite2D::new_alloc(),
}
}
} Now, in the docs it says:
In my case above, I don't have to worry about Thanks again for the help, as I am still fairly new to Rust. |
You said it's set via export, not via scene tree? If it's part of the scene tree, then yes, freeing is taken care of by Godot. (You should see a leak report on Godot shutdown if not). If it's directly in the scene tree -- not exported -- consider using #[init(node = "NameOfSpriteInTree")]
sprite: OnReady<Gd<Sprite>> |
Do you need the export? I.e. user should be able to change the referenced node? Because within the class, you can refer to the child via node path ( |
personally i often prefer exporting it like this, means my project isn't strongly coupled to being structured in a specific way with specific names. you should generally use |
is there an equivalent way to write the following?
I tried this, but I get the error:
`#[var]` properties require `Var` trait; #[export] ones require `Export` trait the trait `Var` is not implemented for `Sprite2D`
I then replaced
Sprite2D
withNodePath
, and that works error wise, but doesn't works the same, as it is a string and not a Sprite2D reference, also, the editor allows any item to be passed to it and I only want sprite nodes.The text was updated successfully, but these errors were encountered: