Skip to content

No way to get Gd<Self> without casting #302

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

Closed
MatrixDev opened this issue Jun 8, 2023 · 2 comments
Closed

No way to get Gd<Self> without casting #302

MatrixDev opened this issue Jun 8, 2023 · 2 comments
Labels
c: register Register classes, functions and other symbols to GDScript quality-of-life No new functionality, but improves ergonomics/internals

Comments

@MatrixDev
Copy link

MatrixDev commented Jun 8, 2023

AFAIK the only way to get Gd<Self> is to use base as base.share().cast().

impl NoiseEmitter {
    fn f_ready(&mut self) {
        self.get_character().bind_mut().noise_emitter = Some(self.base.share().cast());
    }
}

But not taking to account cast overhead this is not safe and can lead to runtime bugs that ideally should be caught during compilation:

impl NoiseEmitter {
    fn f_ready(&mut self) {
        // notice that the field is '_receiver', but we're assigning '_emitter'
        self.get_character().bind_mut().noise_receiver = Some(self.base.share().cast());
    }
}

It would be nice if #[derive(GodotClass)] automatically generated Self-typed share method for such cases:

// generated
impl GodotClass for NoiseEmitter {
    pub fn share(&self) -> Gd<Self> {
        // TODO, ideally implemented without casting overhead but this is nitpicking
    }
}
@Bromeon
Copy link
Member

Bromeon commented Jun 8, 2023

Thanks for the use case and description!
Something along those lines was the 2nd suggestion in #131.

@Bromeon Bromeon added quality-of-life No new functionality, but improves ergonomics/internals c: register Register classes, functions and other symbols to GDScript labels Jun 8, 2023
@Bromeon
Copy link
Member

Bromeon commented Jan 5, 2024

Implemented as self.to_gd().

#497

@Bromeon Bromeon closed this as completed Jan 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c: register Register classes, functions and other symbols to GDScript quality-of-life No new functionality, but improves ergonomics/internals
Projects
None yet
Development

No branches or pull requests

2 participants