Skip to content

Panic on accessing Gd<Self> in contructors #997

Open
@malj

Description

@malj

Accessing Gd<Self> pointer in constructors silently crashes the editor.

I haven't dug too deep into the library implementation so I'm not sure if this is a bug or a constructor API design limitation, but considering that WithBaseField::to_gd is part of the public API and the equivalent C++ and GDScript code works fine I thought I'd mention it.

Minimal example:

#[derive(GodotClass)]
#[class(base=Object)]
struct MyObject {
    base: Base<Object>,
}

#[godot_api]
impl IObject for MyObject {
    fn init(base: Base<Object>) -> Self {
        let instance = Self { base };
        instance.to_gd(); // <-- crash (downcast panic)
        instance
    }
}

The intended use case was to connect some local signals in the constructor while building a custom editor plugin, which seems to be a common pattern in engine code. This requires Gd<Self> for constructing a callable via Callable::from_object_method(&instance.to_gd(), "method_name").

EDIT: Removed "silent" since it was an unrelated issue

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugc: registerRegister classes, functions and other symbols to GDScript

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions