Skip to content

Commit

Permalink
Implement script init callback (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
TitanNano authored Jan 5, 2024
1 parent 06d6292 commit 447f892
Showing 1 changed file with 30 additions and 3 deletions.
33 changes: 30 additions & 3 deletions rust-script/src/runtime/rust_script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ use godot::{
builtin::meta::{MethodInfo, PropertyInfo, ToGodot},
engine::{
create_script_instance, notify::ObjectNotification, ClassDb, Engine, IScriptExtension,
Script, ScriptExtension, ScriptLanguage, WeakRef,
Script, ScriptExtension, ScriptInstance, ScriptLanguage, WeakRef,
},
log::{godot_print, godot_warn},
log::{godot_error, godot_print, godot_warn},
obj::{InstanceId, UserClass},
prelude::{
godot_api, Array, Base, Dictionary, GString, Gd, GodotClass, Object, StringName, Variant,
Expand Down Expand Up @@ -123,6 +123,31 @@ impl RustScript {
.map(|gd_ref| godot::engine::utilities::weakref(gd_ref.to_variant()).to())
.collect();
}

fn init_script_instance(instance: &mut RustScriptInstance) {
match instance.call(StringName::from("_init"), &[]) {
Ok(_) => (),
Err(err) => {
use godot::sys::*;

if !matches!(
err,
GDEXTENSION_CALL_OK | GDEXTENSION_CALL_ERROR_INVALID_METHOD
) {
let error_code = match err {
GDEXTENSION_CALL_ERROR_INSTANCE_IS_NULL => "INSTANCE_IS_NULL",
GDEXTENSION_CALL_ERROR_INVALID_ARGUMENT => "INVALID_ARGUMENT",
GDEXTENSION_CALL_ERROR_METHOD_NOT_CONST => "METHOD_NOT_CONST",
GDEXTENSION_CALL_ERROR_TOO_FEW_ARGUMENTS => "TOO_FEW_ARGUMENTS",
GDEXTENSION_CALL_ERROR_TOO_MANY_ARGUMENTS => "TOO_MANY_ARGUMENTS",
_ => "UNKNOWN",
};

godot_error!("failed to call rust script _init fn: {}", error_code);
}
}
};
}
}

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

let data = self.create_remote_instance(for_object.clone());
let instance = RustScriptInstance::new(data, for_object, self.base.deref().clone().cast());
let mut instance =
RustScriptInstance::new(data, for_object, self.base.deref().clone().cast());

Self::init_script_instance(&mut instance);
create_script_instance(instance)
}

Expand Down

0 comments on commit 447f892

Please sign in to comment.