Struct Base
pub struct Base<T>where
T: GodotClass,{ /* private fields */ }Expand description
Restricted version of Gd, to hold the base instance inside a user’s GodotClass.
Behaves similarly to Gd, but is more constrained. Cannot be constructed by the user.
Implementations§
§impl<T> Base<T>where
T: GodotClass,
impl<T> Base<T>where
T: GodotClass,
pub fn to_init_gd(&self) -> Gd<T>
pub fn to_init_gd(&self) -> Gd<T>
Workaround for base being unitialized during object initialization and NOTIFICATION_POSTINITIALIZE
for Godot versions before 4.7.
§Behaviour after Godot 4.7
Since Godot 4.7 initialization layer receives fully-constructed object to work with – therefore in Godot 4.7 and later this method simply returns a clone of a given instance.
Use this method if you want to support Godot versions older than 4.7.
§Behaviour before Godot 4.7
Returns a Gd referencing the base object, for exclusive use during object initialization and NOTIFICATION_POSTINITIALIZE.
Can be used during an initialization function I*::init() or Gd::from_init_fn(), or POSTINITIALIZE.
The base pointer is only pointing to a base object; you cannot yet downcast it to the object being constructed. The instance ID is the same as the one the in-construction object will have.
§Lifecycle for ref-counted classes
If T: Inherits<RefCounted>, then the ref-counted object is not yet fully-initialized at the time of the init function and POSTINITIALIZE running.
Accessing the base object without further measures would be dangerous. Here, godot-rust employs a workaround: the Base object (which
holds a weak pointer to the actual instance) is temporarily upgraded to a strong pointer, preventing use-after-free.
This additional reference is automatically dropped at an implementation-defined point in time (which may change, and technically delay
destruction of your object as soon as you use Base::to_init_gd()). Right now, this refcount-decrement is deferred to the next frame.
Ref-counted bases can only use to_init_gd() on the main thread.
§Panics (Debug)
In Godot before 4.7, if called outside an initialization function, or for ref-counted objects on a non-main thread.