Mimic onready vars · Issue #130 · godot-rust/gdext (original) (raw)

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> #[base] base: Base }

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> #[base] base: Base }

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: