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

How to #[export] Sprite2D or any node reference type? #972

Open
TheColorRed opened this issue Dec 15, 2024 · 8 comments
Open

How to #[export] Sprite2D or any node reference type? #972

TheColorRed opened this issue Dec 15, 2024 · 8 comments
Labels
question Not a problem with the library, but a question regarding usage.

Comments

@TheColorRed
Copy link

TheColorRed commented Dec 15, 2024

is there an equivalent way to write the following?

@export var sprite: Sprite2D

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`

pub struct Example {
  base: Base<Node2D>,
  #[export]
  sprite: Sprite2D,
}

I then replaced Sprite2D with NodePath, 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.

@MrJoermungandr
Copy link

MrJoermungandr commented Dec 15, 2024

You have to wrap it in the Gd<> smart pointer e.g texture: Gd<Sprite2D>. This indicates that the property is managed by godot.
if you havent already read the book (https://godot-rust.github.io/book/) to get a few api examples and get started quickly :)

@Bromeon Bromeon added the question Not a problem with the library, but a question regarding usage. label Dec 15, 2024
@TheColorRed
Copy link
Author

TheColorRed commented Dec 15, 2024

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 Gd<Sprite2D>, how would you set the default in the init?

I thought I could do this, but I guess not:

  fn init(base: Base<Node2D>) -> Self {
    Self {
      base,
      sprite: Gd::<Sprite2D>::default(),
    }
  }

@Bromeon
Copy link
Member

Bromeon commented Dec 15, 2024

In addition to the book, check also the API docs, specifically about Gd. There's a whole section about object construction 🙂

Btw, if you want to use late initialization, consider using OnReady instead.

@TheColorRed
Copy link
Author

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:

The result must be manually managed, e.g. by attaching it to the scene tree or calling free() after usage. Failure to do so will result in memory leaks.

In my case above, I don't have to worry about free(), since it is attached to the scene tree through the editor, is that correct?

Thanks again for the help, as I am still fairly new to Rust.

@Bromeon
Copy link
Member

Bromeon commented Dec 15, 2024

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 OnReady as mentioned:

#[init(node = "NameOfSpriteInTree")]
sprite: OnReady<Gd<Sprite>>

@TheColorRed
Copy link
Author

Yeah it is added to the scene tree, and the node references it like this:

image

I don't think I want OnReady here but I will keep it in mind.

@Bromeon
Copy link
Member

Bromeon commented Dec 15, 2024

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 (OnReady or get_node_as)...

@lilizoey
Copy link
Member

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 Option<Gd<Sprite2D>> though since otherwise you may have issues if godot tries to set the node to null.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Not a problem with the library, but a question regarding usage.
Projects
None yet
Development

No branches or pull requests

4 participants