Skip to content

Consolidated API to pass custom types over FFI boundary #414

@bluenote10

Description

@bluenote10

This issue is similar to #227 and perhaps also related to @lilizoey's comment on improving error messages. So I'm not quite sure if the necessary features are already all there, and I'm just failing to connect the dots.

I'm wondering how to support custom primitive data types in function signatures. For instance:

// Example 1

#[derive(Debug, Clone, Copy, PartialEq, FromVariant, ToVariant)]
enum EventKind {
    DrawStart,
    Drag,
    DragEnd,
}

#[godot_api]
impl CustomProgressBar {
    #[signal]
    fn progress_bar_change(progress: i64, event_kind: EventKind);
}

// Example 2

#[godot_api]
impl CustomItemList {
    #[signal]
    fn item_selected(index: Option<u32>);
}

In these examples the compiler complains:

  • First example: that neither GodotFfi nor EngineEnum is implemented for EventKind.
  • Second example: that neither EngineEnum nor GodotNullablePtr is implemented Option<u32> / u32.

My work-around is typically to move the encoding/decoding into the user code, for instance:

#[godot_api]
impl CustomProgressBar {
    #[signal]
    fn progress_bar_change(progress: i64, event_kind: Variant);
}

But now this signature is kind of vague, and every implementator of the signal must remember how to actually decode that variant.

In both cases it would be great if I could specify a kind of encoding/decoding such that e.g. the Option<u32> encodes the None as -1 etc.

I'd assume that the FromVariant / ToVariant traits solve this problem partially, but somehow even in the case where the custom type implements these traits, it is not possible to use it directly. The error message also mentions VariantMetadata is required to be implemented as well, but according to this comment, it is probably not a trait the user should implement manually?

Metadata

Metadata

Assignees

No one assigned

    Labels

    c: registerRegister classes, functions and other symbols to GDScriptfeatureAdds functionality to the library

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions