@@ -13,7 +13,7 @@ use godot_ffi::is_main_thread;
1313use sys:: { SysPtr as _, static_assert_eq_size_align} ;
1414
1515use crate :: builtin:: { Callable , NodePath , StringName , Variant } ;
16- use crate :: meta:: error:: { ConvertError , FromFfiError } ;
16+ use crate :: meta:: error:: { ConvertError , ErrorKind , FromFfiError } ;
1717use crate :: meta:: shape:: GodotShape ;
1818use crate :: meta:: {
1919 AsArg , ClassId , Element , FromGodot , GodotConvert , GodotNullableType , GodotType , RefArg , ToGodot ,
@@ -226,9 +226,15 @@ where
226226impl < T : GodotClass > Gd < T > {
227227 /// Looks up the given instance ID and returns the associated object, if possible.
228228 ///
229- /// If no such instance ID is registered, or if the dynamic type of the object behind that instance ID
230- /// is not compatible with `T`, then `None` is returned.
229+ /// # Errors
230+ /// - If no such instance ID is registered, or if the dynamic type of the object behind that instance ID
231+ /// is not compatible with `T`, then `Err(...)` is returned.
232+ /// - If called outside the main-thread, then `Err(...)` is returned as well.
231233 pub fn try_from_instance_id ( instance_id : InstanceId ) -> Result < Self , ConvertError > {
234+ if !sys:: is_main_thread ( ) {
235+ return Err ( ConvertError :: with_kind ( ErrorKind :: InvalidThread ) ) ;
236+ }
237+
232238 let ptr = classes:: object_ptr_from_id ( instance_id) ;
233239
234240 // SAFETY: assumes that the returned GDExtensionObjectPtr is convertible to Object* (i.e. C++ upcast doesn't modify the pointer)
@@ -243,10 +249,15 @@ impl<T: GodotClass> Gd<T> {
243249 /// Corresponds to Godot's global function `instance_from_id()`.
244250 ///
245251 /// # Panics
246- /// If no such instance ID is registered, or if the dynamic type of the object behind that instance ID
247- /// is not compatible with `T`.
252+ /// - If no such instance ID is registered, or if the dynamic type of the object behind that instance ID
253+ /// is not compatible with `T`.
254+ /// - If called on a different thread than the main-thread.
248255 #[ doc( alias = "instance_from_id" ) ]
249256 pub fn from_instance_id ( instance_id : InstanceId ) -> Self {
257+ if !sys:: is_main_thread ( ) {
258+ panic ! ( "Gd::instance_from_id is can not be called outside the main-thread" ) ;
259+ }
260+
250261 Self :: try_from_instance_id ( instance_id) . unwrap_or_else ( |err| {
251262 panic ! (
252263 "Instance ID {} does not belong to a valid object of class '{}': {}" ,
0 commit comments