Skip to content

Commit 447f892

Browse files
authored
Implement script init callback (#22)
1 parent 06d6292 commit 447f892

File tree

1 file changed

+30
-3
lines changed

1 file changed

+30
-3
lines changed

rust-script/src/runtime/rust_script.rs

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ use godot::{
1111
builtin::meta::{MethodInfo, PropertyInfo, ToGodot},
1212
engine::{
1313
create_script_instance, notify::ObjectNotification, ClassDb, Engine, IScriptExtension,
14-
Script, ScriptExtension, ScriptLanguage, WeakRef,
14+
Script, ScriptExtension, ScriptInstance, ScriptLanguage, WeakRef,
1515
},
16-
log::{godot_print, godot_warn},
16+
log::{godot_error, godot_print, godot_warn},
1717
obj::{InstanceId, UserClass},
1818
prelude::{
1919
godot_api, Array, Base, Dictionary, GString, Gd, GodotClass, Object, StringName, Variant,
@@ -123,6 +123,31 @@ impl RustScript {
123123
.map(|gd_ref| godot::engine::utilities::weakref(gd_ref.to_variant()).to())
124124
.collect();
125125
}
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+
}
126151
}
127152

128153
#[godot_api]
@@ -178,8 +203,10 @@ impl IScriptExtension for RustScript {
178203
.push(godot::engine::utilities::weakref(for_object.to_variant()).to());
179204

180205
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());
182208

209+
Self::init_script_instance(&mut instance);
183210
create_script_instance(instance)
184211
}
185212

0 commit comments

Comments
 (0)