@@ -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 ,
@@ -211,9 +211,15 @@ where
211211impl < T : GodotClass > Gd < T > {
212212 /// Looks up the given instance ID and returns the associated object, if possible.
213213 ///
214- /// If no such instance ID is registered, or if the dynamic type of the object behind that instance ID
215- /// is not compatible with `T`, then `None` is returned.
214+ /// # Errors
215+ /// - If no such instance ID is registered, or if the dynamic type of the object behind that instance ID
216+ /// is not compatible with `T`, then `Err(...)` is returned.
217+ /// - If called outside the main-thread, then `Err(...)` is returned as well.
216218 pub fn try_from_instance_id ( instance_id : InstanceId ) -> Result < Self , ConvertError > {
219+ if !sys:: is_main_thread ( ) {
220+ return Err ( ConvertError :: with_kind ( ErrorKind :: InvalidThread ) ) ;
221+ }
222+
217223 let ptr = classes:: object_ptr_from_id ( instance_id) ;
218224
219225 // SAFETY: assumes that the returned GDExtensionObjectPtr is convertible to Object* (i.e. C++ upcast doesn't modify the pointer)
@@ -228,10 +234,15 @@ impl<T: GodotClass> Gd<T> {
228234 /// Corresponds to Godot's global function `instance_from_id()`.
229235 ///
230236 /// # Panics
231- /// If no such instance ID is registered, or if the dynamic type of the object behind that instance ID
232- /// is not compatible with `T`.
237+ /// - If no such instance ID is registered, or if the dynamic type of the object behind that instance ID
238+ /// is not compatible with `T`.
239+ /// - If called on a different thread than the main-thread.
233240 #[ doc( alias = "instance_from_id" ) ]
234241 pub fn from_instance_id ( instance_id : InstanceId ) -> Self {
242+ if !sys:: is_main_thread ( ) {
243+ panic ! ( "Gd::instance_from_id is can not be called outside the main-thread" ) ;
244+ }
245+
235246 Self :: try_from_instance_id ( instance_id) . unwrap_or_else ( |err| {
236247 panic ! (
237248 "Instance ID {} does not belong to a valid object of class '{}': {}" ,
0 commit comments