@@ -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 ,
@@ -250,9 +250,15 @@ where
250250impl < T : GodotClass > Gd < T > {
251251 /// Looks up the given instance ID and returns the associated object, if possible.
252252 ///
253- /// If no such instance ID is registered, or if the dynamic type of the object behind that instance ID
254- /// is not compatible with `T`, then `None` is returned.
253+ /// # Errors
254+ /// - If no such instance ID is registered, or if the dynamic type of the object behind that instance ID
255+ /// is not compatible with `T`, then `Err(...)` is returned.
256+ /// - If called outside the main-thread, then `Err(...)` is returned as well.
255257 pub fn try_from_instance_id ( instance_id : InstanceId ) -> Result < Self , ConvertError > {
258+ if !sys:: is_main_thread ( ) {
259+ return Err ( ConvertError :: with_kind ( ErrorKind :: InvalidThread ) ) ;
260+ }
261+
256262 let ptr = classes:: object_ptr_from_id ( instance_id) ;
257263
258264 // SAFETY: assumes that the returned GDExtensionObjectPtr is convertible to Object* (i.e. C++ upcast doesn't modify the pointer)
@@ -267,10 +273,15 @@ impl<T: GodotClass> Gd<T> {
267273 /// Corresponds to Godot's global function `instance_from_id()`.
268274 ///
269275 /// # Panics
270- /// If no such instance ID is registered, or if the dynamic type of the object behind that instance ID
271- /// is not compatible with `T`.
276+ /// - If no such instance ID is registered, or if the dynamic type of the object behind that instance ID
277+ /// is not compatible with `T`.
278+ /// - If called on a different thread than the main-thread.
272279 #[ doc( alias = "instance_from_id" ) ]
273280 pub fn from_instance_id ( instance_id : InstanceId ) -> Self {
281+ if !sys:: is_main_thread ( ) {
282+ panic ! ( "Gd::instance_from_id is can not be called outside the main-thread" ) ;
283+ }
284+
274285 Self :: try_from_instance_id ( instance_id) . unwrap_or_else ( |err| {
275286 panic ! (
276287 "Instance ID {} does not belong to a valid object of class '{}': {}" ,
0 commit comments