godot::obj::script

Struct SiMut

pub struct SiMut<'a, T>
where T: ScriptInstance,
{ /* private fields */ }
Expand description

Mutable/exclusive reference guard for a T where T implements ScriptInstance.

This can be used to access the base object of a ScriptInstance, which in turn can be used to make reentrant calls to engine APIs. For details see SiMut::base_mut().

Implementations§

§

impl<'a, T> SiMut<'a, T>
where T: ScriptInstance,

pub fn base(&self) -> ScriptBaseRef<'_, T>

Returns a shared reference suitable for calling engine methods on this object.

Holding a shared guard prevents other code paths from obtaining a mutable reference to self, as such it is recommended to drop the guard as soon as you no longer need it.

struct ExampleScriptInstance;

impl ScriptInstance for ExampleScriptInstance {
    type Base = Node;

    fn call(
        this: SiMut<Self>,
        method: StringName,
        args: &[&Variant],
    ) -> Result<Variant, sys::GDExtensionCallErrorType>{
        let name = this.base().get_name();
        godot_print!("name is {name}");
        // However, we cannot call methods that require `&mut Base`, such as:
        // this.base().add_child(node);
        Ok(Variant::nil())
    }
}

pub fn base_mut(&mut self) -> ScriptBaseMut<'_, T>

Returns a mutable reference suitable for calling engine methods on this object.

This method will allow you to call back into the same object from Godot (re-entrancy).

Holding a mutable guard prevents other code paths from obtaining any reference to self, as such it is recommended to drop the guard as soon as you no longer need it.

struct ExampleScriptInstance;

impl ScriptInstance for ExampleScriptInstance {
    type Base = Object;

    fn call(
        mut this: SiMut<Self>,
        method: StringName,
        args: &[&Variant],
    ) -> Result<Variant, sys::GDExtensionCallErrorType> {
        // Check whether method is available on this script
        if method == StringName::from("script_method") {
            godot_print!("script_method called!");
            return Ok(true.to_variant());
        }

        let node = Node::new_alloc();

        // We can call back into `self` through Godot:
        this.base_mut().call("script_method".into(), &[]);

        Ok(Variant::nil())
    }
}

Trait Implementations§

§

impl<'a, T> Deref for SiMut<'a, T>
where T: ScriptInstance,

§

type Target = T

The resulting type after dereferencing.
§

fn deref(&self) -> &<SiMut<'a, T> as Deref>::Target

Dereferences the value.
§

impl<'a, T> DerefMut for SiMut<'a, T>
where T: ScriptInstance,

§

fn deref_mut(&mut self) -> &mut <SiMut<'a, T> as Deref>::Target

Mutably dereferences the value.

Auto Trait Implementations§

§

impl<'a, T> Freeze for SiMut<'a, T>

§

impl<'a, T> !RefUnwindSafe for SiMut<'a, T>

§

impl<'a, T> !Send for SiMut<'a, T>

§

impl<'a, T> !Sync for SiMut<'a, T>

§

impl<'a, T> Unpin for SiMut<'a, T>

§

impl<'a, T> !UnwindSafe for SiMut<'a, T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.