Trait gdnative_core::export::NativeClass
source · pub trait NativeClass: Sized + 'static {
type Base: GodotObject;
type UserData: UserData<Target = Self>;
fn nativeclass_init(_owner: TRef<'_, Self::Base, Shared>) -> Self { ... }
fn nativeclass_register_properties(_builder: &ClassBuilder<Self>) { ... }
fn new_instance() -> Instance<Self, Unique>
where
Self::Base: Instanciable,
{ ... }
fn emplace(self) -> Instance<Self, Unique>
where
Self::Base: Instanciable,
{ ... }
}
Expand description
Trait used for describing and initializing a Godot script class.
This trait is used to provide data and functionality to the “data-part” of the class, such as name, initialization and information about exported properties.
A derive macro is available for this trait. See documentation on the
NativeClass
macro for detailed usage and examples.
For exported methods, see the NativeClassMethods
trait.
Required Associated Types§
sourcetype Base: GodotObject
type Base: GodotObject
Base type of the class.
In Godot, scripting languages can define “script instances” which can be attached to objects. Because of the dynamic nature, the intended “inheritance” is not easily implementable properly.
Instead, delegation is used and most calls to a Godot object query the script instance first. This way, some methods can be “overwritten” and new ones can be exposed.
This only works when using so called “variant calls”, since the querying of the script instance is performed there. When not using variant calls, any direct(*) calls have to be made to the Godot object directly.
The base type describes the “most general” type of object this script class can be attached to.
(*): GDNative enables use of “ptrcall“s, which are wrappers for function pointers. Those do not do explicit checks for script implementations unless the method implementation does.
Provided Methods§
sourcefn nativeclass_init(_owner: TRef<'_, Self::Base, Shared>) -> Self
fn nativeclass_init(_owner: TRef<'_, Self::Base, Shared>) -> Self
Function that creates a value of Self
, used for the script-instance. The default
implementation simply panics.
This function has a reference to the owner object as a parameter, which can be used to set state on the owner upon creation or to query values
It is possible to declare script classes without zero-argument constructors. Instances
of such scripts can only be created from Rust using Instance::emplace
. See
documentation on Instance::emplace
for an example.
sourcefn nativeclass_register_properties(_builder: &ClassBuilder<Self>)
fn nativeclass_register_properties(_builder: &ClassBuilder<Self>)
Register any exported properties to Godot.
sourcefn new_instance() -> Instance<Self, Unique>where
Self::Base: Instanciable,
fn new_instance() -> Instance<Self, Unique>where
Self::Base: Instanciable,
Convenience method to create an Instance<Self, Unique>
. This is a new Self::Base
with the script attached.
If Self::Base
is manually-managed, then the resulting Instance
must be passed to
the engine or manually freed with Instance::free
. Otherwise, the base object will be
leaked.
Must be called after the library is initialized.
sourcefn emplace(self) -> Instance<Self, Unique>where
Self::Base: Instanciable,
fn emplace(self) -> Instance<Self, Unique>where
Self::Base: Instanciable,
Convenience method to emplace self
into an Instance<Self, Unique>
. This is a new
Self::Base
with the script attached.
If Self::Base
is manually-managed, then the resulting Instance
must be passed to
the engine or manually freed with Instance::free
. Otherwise, the base object will be
leaked.
Must be called after the library is initialized.