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

Virtual function dispatch for Resources not working #698

Open
mio991 opened this issue May 4, 2024 · 4 comments
Open

Virtual function dispatch for Resources not working #698

mio991 opened this issue May 4, 2024 · 4 comments

Comments

@mio991
Copy link
Contributor

mio991 commented May 4, 2024

If I have a CustomResource like this:

#[derive(GodotClass)]
#[class(init, base=Resource)]
pub struct BaseResource {
    _base: Base<Resource>,
}

#[godot_api]
impl BaseResource {
    #[func(virtual, rename = _get_value)]
    pub fn get_value(&self) -> GString {
        unimplemented!("get_value should be overridden");
    }
}

and extend it into an actual implementation like this:

extends BaseResource
class_name TestResource

func _get_value() -> String:
	return "Lila"

I get an Unimplemented Error, if I try to call get_value() from rust like so:

impl BaseResource {
    pub fn test_virtual(&self) {
        godot_print!("Value: {}", self.get_value());
    }
}

Reproduction Steps

  1. decrompress demo.zip
  2. add Godot 4.3-dev6 in the ./bin folder
  3. build the rust library
  4. start Godot 4.3-dev6
  5. open the Project under ./godot
  6. execute main.tscn
  7. click the Test-Button

There are Exceptions in the Output.

Expected Behaviour

I expect a console output of Value: Lila.

@Bromeon
Copy link
Member

Bromeon commented May 4, 2024

Can you please describe the problem in a minimal complete example here, so users don't need to download a ZIP package? Thanks!

@mio991
Copy link
Contributor Author

mio991 commented May 4, 2024

I edited my initial Comment, I hope that helps. I can also push the demo project from the zip file t github if you want.

@Bromeon
Copy link
Member

Bromeon commented May 4, 2024

I meant http://www.sscce.org, i.e. a small code example that is enough to demonstrate the problem, focuses only on the problem and removes anything that's not absolutely necessary. But it's still self-contained, so others can quickly test it, without the need to download + compile + load the whole project. If Godot scenes are necessary to reproduce the issue, you can either set them up in code or describe how they look in some comments.

Don't get me wrong, the .zip can still be useful, but it should be an addition to the actual problem description. It must be possible to understand and discuss the issue without download.

Thanks for your understanding! 🙂

@lilizoey
Copy link
Member

lilizoey commented Jun 7, 2024

I think if you want virtual methods like this to work you'll have to call them like

self.base().call("test_virtual".into(), &[]);

because otherwise you'll just end up calling the rust function.

maybe we should change this so that the godot_api macro automatically overwrites the original rust function to do something like the call above?

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

3 participants