godot-rust/gdext

Static methods in Godot API

Bromeon opened this issue · 2 comments

Some Godot types are only constructible through static factory methods, e.g. DirAccess.open().

We could provide a method without self parameter, possibly reusing the singleton functionality.

let dir: Gd<DirAccess> = DirAccess::open("path".into());

We could in a later step also consider static user-provided methods (#[func]), in case GDExtension allows that.

For starters, we could skip generating wrappers for static methods, to avoid accidents. Right now, this compiles even though Image::create is static (flagged in JSON as is_static: true), but it doesn't do what you expect:

let image = Image::new();
image.create(2, 4, false, Format::FORMAT_RGBA8);

Instead of initializing image, it creates and returns a new Image, and ends up leaking one of them as well:

WARNING: ObjectDB instances leaked at exit (run with --verbose for details).
     at: cleanup (core/object/object.cpp:1982)
Leaked instance: Image:-9222503972424645408 - Resource path: 
Hint: Leaked instances typically happen when nodes are removed from the scene tree (with `remove_child()`) but not freed (with `free()` or `queue_free()`).

Great work! 👏