@@ -11,9 +11,9 @@ use godot::{
11
11
builtin:: meta:: { MethodInfo , PropertyInfo , ToGodot } ,
12
12
engine:: {
13
13
create_script_instance, notify:: ObjectNotification , ClassDb , Engine , IScriptExtension ,
14
- Script , ScriptExtension , ScriptLanguage , WeakRef ,
14
+ Script , ScriptExtension , ScriptInstance , ScriptLanguage , WeakRef ,
15
15
} ,
16
- log:: { godot_print, godot_warn} ,
16
+ log:: { godot_error , godot_print, godot_warn} ,
17
17
obj:: { InstanceId , UserClass } ,
18
18
prelude:: {
19
19
godot_api, Array , Base , Dictionary , GString , Gd , GodotClass , Object , StringName , Variant ,
@@ -123,6 +123,31 @@ impl RustScript {
123
123
. map ( |gd_ref| godot:: engine:: utilities:: weakref ( gd_ref. to_variant ( ) ) . to ( ) )
124
124
. collect ( ) ;
125
125
}
126
+
127
+ fn init_script_instance ( instance : & mut RustScriptInstance ) {
128
+ match instance. call ( StringName :: from ( "_init" ) , & [ ] ) {
129
+ Ok ( _) => ( ) ,
130
+ Err ( err) => {
131
+ use godot:: sys:: * ;
132
+
133
+ if !matches ! (
134
+ err,
135
+ GDEXTENSION_CALL_OK | GDEXTENSION_CALL_ERROR_INVALID_METHOD
136
+ ) {
137
+ let error_code = match err {
138
+ GDEXTENSION_CALL_ERROR_INSTANCE_IS_NULL => "INSTANCE_IS_NULL" ,
139
+ GDEXTENSION_CALL_ERROR_INVALID_ARGUMENT => "INVALID_ARGUMENT" ,
140
+ GDEXTENSION_CALL_ERROR_METHOD_NOT_CONST => "METHOD_NOT_CONST" ,
141
+ GDEXTENSION_CALL_ERROR_TOO_FEW_ARGUMENTS => "TOO_FEW_ARGUMENTS" ,
142
+ GDEXTENSION_CALL_ERROR_TOO_MANY_ARGUMENTS => "TOO_MANY_ARGUMENTS" ,
143
+ _ => "UNKNOWN" ,
144
+ } ;
145
+
146
+ godot_error ! ( "failed to call rust script _init fn: {}" , error_code) ;
147
+ }
148
+ }
149
+ } ;
150
+ }
126
151
}
127
152
128
153
#[ godot_api]
@@ -178,8 +203,10 @@ impl IScriptExtension for RustScript {
178
203
. push ( godot:: engine:: utilities:: weakref ( for_object. to_variant ( ) ) . to ( ) ) ;
179
204
180
205
let data = self . create_remote_instance ( for_object. clone ( ) ) ;
181
- let instance = RustScriptInstance :: new ( data, for_object, self . base . deref ( ) . clone ( ) . cast ( ) ) ;
206
+ let mut instance =
207
+ RustScriptInstance :: new ( data, for_object, self . base . deref ( ) . clone ( ) . cast ( ) ) ;
182
208
209
+ Self :: init_script_instance ( & mut instance) ;
183
210
create_script_instance ( instance)
184
211
}
185
212
0 commit comments