Skip to content

Mimic onready vars #130

@WinstonHartnett

Description

@WinstonHartnett

Currently the pattern for structs with Gd references (which have to be manually initialized in ready) looks like:

#[derive(GodotClass)]
#[class(base=Node)]
struct MyStruct {
    my_child: Option<Gd<Node>>
    #[base]
    base: Base<Node>
}

Then, map, unwrap, or user-defined accessors are needed to bypass the Option.

How about:

#[derive(GodotClass)]
#[class(base=Node)]
struct MyStruct {
    #[on_ready("MyChild")]
    my_child: OnReady<Gd<Node>>
    #[base]
    base: Base<Node>
}

where OnReady is a MaybeUninit<Gd<T>> with a Deref/DerefMut instance to avoid unwrapping.

It certainly clutters the struct definition a bit more, but you avoid:

  • Initializing an Option in ready (which can easily be forgotten)
  • Using map or accessors to get the obviously initialized field
  • Paying for the Option<Gd<T>>'s 16 bytes compared to Gd<T>

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