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>
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.
For now, ref-counted bases can only use to_init_gd()
on the main thread.
§Panics (Debug)
If called outside an initialization function, or for ref-counted objects on a non-main thread.