You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
/// Defers the given closure to run during [idle time](https://docs.godotengine.org/en/stable/classes/class_object.html#class-object-method-call-deferred).
671
+
///
672
+
/// This is a type-safe alternative to [`Object::call_deferred()`][crate::classes::Object::call_deferred]. The closure receives
673
+
/// `&mut Self` allowing direct access to Rust fields and methods.
674
+
///
675
+
/// This method is only available for user-defined classes with a `Base<T>` field.
676
+
/// For engine classes, use [`run_deferred_gd()`][Self::run_deferred_gd] instead.
677
+
///
678
+
/// See also [`WithBaseField::run_deferred()`] if you are within an `impl` block and have access to `self`.
679
+
///
680
+
/// # Panics
681
+
/// If called outside the main thread.
682
+
pubfnrun_deferred<F>(&mutself,mut_self_method:F)
683
+
where
684
+
T:WithBaseField,
685
+
F:FnOnce(&mutT) + 'static,
686
+
{
687
+
self.run_deferred_gd(move |mut gd| {
688
+
letmut guard = gd.bind_mut();
689
+
mut_self_method(&mut*guard);
690
+
});
691
+
}
692
+
693
+
/// Defers the given closure to run during [idle time](https://docs.godotengine.org/en/stable/classes/class_object.html#class-object-method-call-deferred).
694
+
///
695
+
/// This is a type-safe alternative to [`Object::call_deferred()`][crate::classes::Object::call_deferred]. The closure receives
696
+
/// `Gd<T>`, which can be used to call engine methods or [`bind()`][Gd::bind]/[`bind_mut()`][Gd::bind_mut] to access the Rust object.
697
+
///
698
+
/// See also [`WithBaseField::run_deferred_gd()`] if you are within an `impl` block and have access to `self`.
699
+
///
700
+
/// # Panics
701
+
/// If called outside the main thread.
702
+
pubfnrun_deferred_gd<F>(&mutself,gd_function:F)
703
+
where
704
+
F:FnOnce(Gd<T>) + 'static,
705
+
{
706
+
let obj = self.clone();
707
+
assert!(
708
+
is_main_thread(),
709
+
"`run_deferred` must be called on the main thread"
710
+
);
711
+
712
+
let callable = Callable::from_once_fn("run_deferred",move |_| {
713
+
gd_function(obj);
714
+
Ok(Variant::nil())
715
+
});
716
+
callable.call_deferred(&[]);
717
+
}
668
718
}
669
719
670
720
/// _The methods in this impl block are only available for objects `T` that are manually managed,
/// Defers the given closure to run during [idle time](https://docs.godotengine.org/en/stable/classes/class_object.html#class-object-method-call-deferred).
545
+
///
546
+
/// This is a type-safe alternative to [`Object::call_deferred()`][crate::classes::Object::call_deferred]. The closure receives
547
+
/// `&mut Self` allowing direct access to Rust fields and methods.
548
+
///
549
+
/// See also [`Gd::run_deferred()`] to defer logic outside of `self`.
550
+
///
551
+
/// # Panics
552
+
/// If called outside the main thread.
553
+
fnrun_deferred<F>(&mutself,mut_self_method:F)
554
+
where
555
+
F:FnOnce(&mutSelf) + 'static,
556
+
{
557
+
// We need to copy the Gd, because the lifetime of `&mut self` does not extend throughout the closure, which will only be called
558
+
// deferred. It might even be freed in-between, causing panic on bind_mut().
559
+
self.to_gd().run_deferred(mut_self_method)
560
+
}
561
+
562
+
/// Defers the given closure to run during [idle time](https://docs.godotengine.org/en/stable/classes/class_object.html#class-object-method-call-deferred).
563
+
///
564
+
/// This is a type-safe alternative to [`Object::call_deferred()`][crate::classes::Object::call_deferred]. The closure receives
565
+
/// `Gd<Self>`, which can be used to call engine methods or [`bind()`][Gd::bind]/[`bind_mut()`][Gd::bind_mut] to access the Rust object.
566
+
///
567
+
/// See also [`Gd::run_deferred_gd()`] to defer logic outside of `self`.
568
+
///
569
+
/// # Panics
570
+
/// If called outside the main thread.
571
+
fnrun_deferred_gd<F>(&mutself,gd_function:F)
572
+
where
573
+
F:FnOnce(Gd<Self>) + 'static,
574
+
{
575
+
self.to_gd().run_deferred_gd(gd_function)
576
+
}
543
577
}
544
578
545
579
/// Implemented for all classes with registered signals, both engine- and user-declared.
0 commit comments