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

Static methods and custom constructors on custom types #175

Closed
HenryWConklin opened this issue Mar 12, 2023 · 1 comment · Fixed by #252
Closed

Static methods and custom constructors on custom types #175

HenryWConklin opened this issue Mar 12, 2023 · 1 comment · Fixed by #252
Labels
c: register Register classes, functions and other symbols to GDScript feature Adds functionality to the library

Comments

@HenryWConklin
Copy link
Contributor

Question/Feature Request

Is there a way to define methods on your custom types in Rust which do not take a self parameter?

A minimal example of how I would expect it to work:

use godot::prelude::*;

#[derive(GodotClass)]
#[class(base=Object)]
pub struct MyType {
    pub foo: f32,
}

#[godot_api]
impl MyType {
    #[func]
    pub fn from_foo(foo: f32) -> Gd<MyType> {
        Gd::new(MyType { foo })
    }
}

But, I currently get a somewhat cryptic error from the macro:

error: no rules expected the token `foo`
   --> mycrate/src/lib.rs:125:21
    |
125 |     pub fn from_foo(foo: f32) -> Gd<MyType> {
    |                     ^^^ no rules expected this token in macro call

error: could not compile `mycrate` due to previous error

Use case for this would be static methods and constructors that actually take arguments.
This would ideally be called from GdScript like MyType.from_foo(foo) which constructs a MyType from a foo.

There is godotengine/godot#61963 which seems to suggest it's supported by gdextension, but I haven't found a way to do it in Rust using this repo.

There's also #43 which looks like the other direction, exposing static methods on Godot types in Rust.

@Bromeon Bromeon added feature Adds functionality to the library c: register Register classes, functions and other symbols to GDScript labels Mar 12, 2023
@Bromeon Bromeon linked a pull request Mar 29, 2023 that will close this issue
@ttencate
Copy link
Contributor

I have a PR (#210) that will make this compile, but it's blocked behind some other work (#204) that will actually make it useful. Right now it would probably either crash or leak if you tried anything nontrivial like returning a Gd type.

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 feature Adds functionality to the library
Projects
None yet
3 participants