44 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
55 */
66
7- use std:: { cell:: RefCell , collections:: HashSet , ffi:: c_void, ops :: Deref } ;
7+ use std:: { cell:: RefCell , collections:: HashSet , ffi:: c_void} ;
88
99use abi_stable:: std_types:: RBox ;
1010use godot:: {
@@ -14,7 +14,7 @@ use godot::{
1414 Script , ScriptExtension , ScriptInstance , ScriptLanguage , WeakRef ,
1515 } ,
1616 log:: { godot_error, godot_print, godot_warn} ,
17- obj:: { InstanceId , UserClass } ,
17+ obj:: { InstanceId , WithBaseField } ,
1818 prelude:: {
1919 godot_api, Array , Base , Dictionary , GString , Gd , GodotClass , Object , StringName , Variant ,
2020 VariantArray ,
@@ -25,6 +25,7 @@ use RemoteGodotScript_trait::RemoteGodotScript_TO;
2525use crate :: { apply:: Apply , script_registry:: RemoteGodotScript_trait } ;
2626
2727use super :: {
28+ downgrade_self:: DowngradeSelf ,
2829 metadata:: { Documented , ToDictionary , ToMethodDoc , ToPropertyDoc } ,
2930 rust_script_instance:: { RustScriptInstance , RustScriptPlaceholder } ,
3031 rust_script_language:: RustScriptLanguage ,
@@ -47,6 +48,7 @@ pub(super) struct RustScript {
4748 owner_ids : Array < i64 > ,
4849
4950 owners : RefCell < Vec < Gd < WeakRef > > > ,
51+ #[ base]
5052 base : Base < ScriptExtension > ,
5153}
5254
@@ -174,7 +176,7 @@ impl IScriptExtension for RustScript {
174176 }
175177
176178 fn get_language ( & self ) -> Option < Gd < ScriptLanguage > > {
177- Some ( RustScriptLanguage :: alloc_gd ( ) . upcast ( ) )
179+ RustScriptLanguage :: singleton ( ) . map ( Gd :: upcast )
178180 }
179181
180182 fn can_instantiate ( & self ) -> bool {
@@ -203,8 +205,7 @@ impl IScriptExtension for RustScript {
203205 . push ( godot:: engine:: utilities:: weakref ( for_object. to_variant ( ) ) . to ( ) ) ;
204206
205207 let data = self . create_remote_instance ( for_object. clone ( ) ) ;
206- let mut instance =
207- RustScriptInstance :: new ( data, for_object, self . base . deref ( ) . clone ( ) . cast ( ) ) ;
208+ let mut instance = RustScriptInstance :: new ( data, for_object, self . to_gd ( ) ) ;
208209
209210 Self :: init_script_instance ( & mut instance) ;
210211 create_script_instance ( instance)
@@ -215,7 +216,7 @@ impl IScriptExtension for RustScript {
215216 . borrow_mut ( )
216217 . push ( godot:: engine:: utilities:: weakref ( for_object. to_variant ( ) ) . to ( ) ) ;
217218
218- let placeholder = RustScriptPlaceholder :: new ( self . base . deref ( ) . clone ( ) . cast ( ) ) ;
219+ let placeholder = RustScriptPlaceholder :: new ( self . to_gd ( ) ) ;
219220
220221 create_script_instance ( placeholder)
221222 }
@@ -338,7 +339,7 @@ impl IScriptExtension for RustScript {
338339 dict. set ( GString :: from ( "is_deprecated" ) , false ) ;
339340 dict. set ( GString :: from ( "is_experimental" ) , false ) ;
340341 dict. set ( GString :: from ( "is_script_doc" ) , true ) ;
341- dict. set ( GString :: from ( "script_path" ) , self . base . get_path ( ) ) ;
342+ dict. set ( GString :: from ( "script_path" ) , self . base ( ) . get_path ( ) ) ;
342343 } ) ;
343344
344345 Array :: from ( & [ class_doc] )
@@ -350,7 +351,9 @@ impl IScriptExtension for RustScript {
350351
351352 // godot script reload hook
352353 fn reload ( & mut self , _keep_state : bool ) -> godot:: engine:: global:: Error {
353- self . owners . borrow ( ) . iter ( ) . for_each ( |owner| {
354+ let owners = self . owners . borrow ( ) . clone ( ) ;
355+
356+ owners. iter ( ) . for_each ( |owner| {
354357 let mut object: Gd < Object > = match owner. get_ref ( ) . try_to ( ) {
355358 Ok ( owner) => owner,
356359 Err ( err) => {
@@ -359,14 +362,13 @@ impl IScriptExtension for RustScript {
359362 }
360363 } ;
361364
362- let script = object. get_script ( ) ;
363-
364365 // clear script to destroy script instance.
365366 object. set_script ( Variant :: nil ( ) ) ;
366367
367- // re-assign script to create new instance.
368- // call is defered because this will call back into can_instantiate.
369- object. call_deferred ( StringName :: from ( "set_script" ) , & [ script] ) ;
368+ self . downgrade_gd ( |self_gd| {
369+ // re-assign script to create new instance.
370+ object. set_script ( self_gd. to_variant ( ) ) ;
371+ } )
370372 } ) ;
371373
372374 godot:: engine:: global:: Error :: OK
0 commit comments