Skip to content

Cannot use Option<Gd<T>> as signal argument when T is a user declared class #1372

@jsternberg

Description

@jsternberg

If you create a user declared class such as:

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

If you attempt to create a signal that takes this node as a parameter, it will allow it.

#[godot_api]
impl MyCustomNode {
    #[signal]
    pub fn my_custom_event(node: Gd<MyCustomNode>);
}

You can then emit this with a reference to the node. On the other hand, if you try to do the same thing with Option<Gd<MyCustomNode>>, I can't figure out what type it accepts.

#[godot_api]
impl MyCustomNode {
    #[signal]
    pub fn my_custom_event(node: Option<Gd<MyCustomNode>>);
}

There's no compile error when you do this. But, if you attempt to use it.

let my_node = Some(MyCustomNode::new_alloc());
// type mismatch resolving <Option<Gd<MyCustomNode>> as ToGodot>::Pass == ByRef
// expected enum ByRef
// found enum ByOption<godot::prelude::Gd<MyCustomNode>>
// required for &Option<godot::prelude::Gd<MyCustomNode>> to implement AsArg<Option<godot::prelude::Gd<MyCustomNode>>>
self.signals().my_custom_event().emit(&my_node);
// type mismatch resolving <MyCustomNode as Bounds>::Declarer == DeclEngine
// required for Option<&godot::prelude::Gd<MyCustomNode>> to implement AsArg<Option<godot::prelude::Gd<MyCustomNode>>>
self.signals().my_custom_event().emit(my_node.as_ref());

I've also tried other combinations but I can't figure out which type it expects. I could define the signal to take a type parameter that is the base class that's in the engine, but it would be nice to have it more strongly typed than that.

I can find other ways to work around this so it's not a pressing issue, but I didn't know if this was intentional or not.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions