From b3972e65057f415b467416c323e7e9d9bb491097 Mon Sep 17 00:00:00 2001 From: Jovan Gerodetti Date: Wed, 1 Oct 2025 00:43:59 +0200 Subject: [PATCH 1/2] Migrate to godot-rust 0.4 --- Cargo.lock | 32 +++++++------- Cargo.toml | 6 +-- derive/src/enums.rs | 2 +- derive/src/impl_attribute.rs | 14 +++---- derive/src/lib.rs | 4 +- rust-script/src/editor_ui_hacks.rs | 7 ++-- rust-script/src/interface.rs | 27 ++++++------ rust-script/src/interface/export.rs | 2 +- rust-script/src/interface/on_editor.rs | 5 ++- rust-script/src/interface/signals.rs | 9 ++-- rust-script/src/runtime/downgrade_self.rs | 2 +- rust-script/src/runtime/metadata.rs | 12 +++--- rust-script/src/runtime/mod.rs | 20 ++++----- rust-script/src/runtime/resource_loader.rs | 5 ++- rust-script/src/runtime/rust_script.rs | 42 +++++++++++-------- .../src/runtime/rust_script_instance.rs | 11 ++--- .../src/runtime/rust_script_language.rs | 4 +- rust-script/src/static_script_registry.rs | 36 ++++++++-------- 18 files changed, 124 insertions(+), 116 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1d66a66..2976fda 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -72,9 +72,9 @@ checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" [[package]] name = "gdextension-api" -version = "0.2.2" +version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2ec0a03c8f9c91e3d8eb7ca56dea81c7248c03826dd3f545f33cd22ef275d4d1" +checksum = "e25d88dabe9fdb2e064cb545312178ec4025eefb62d9a3bbce1769088e2c381d" [[package]] name = "getrandom" @@ -95,9 +95,9 @@ checksum = "50a99dbe56b72736564cfa4b85bf9a33079f16ae8b74983ab06af3b1a3696b11" [[package]] name = "godot" -version = "0.3.2" +version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e5c4b80a6697ab47cbe03beaa519b4f40fc408ee43b853842224d5d4bdeb85ad" +checksum = "90b20e19a25e45460c4fd2b11b519beea3e9bcda929c1566f8d17a369b41dd82" dependencies = [ "godot-core", "godot-macros", @@ -105,24 +105,24 @@ dependencies = [ [[package]] name = "godot-bindings" -version = "0.3.2" +version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e3ef8e3a3873c1c442b66da8ad939d12ab3ba2bb8c929bc9d45ae4d46047dbd4" +checksum = "508d56d01018c16b67a906bda8bcd9ade7f265990e4be7c2dffecbfdebcc3992" dependencies = [ "gdextension-api", ] [[package]] name = "godot-cell" -version = "0.3.2" +version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2e792bd37ca9b374ab70293bf1b62c17083048938773e9fe527fa844ba411c27" +checksum = "c1794fbec9934ef375d717d02ec142d9e0c7d7482b24d7da463264b92bc14eaf" [[package]] name = "godot-codegen" -version = "0.3.2" +version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d1af2c1cf73ea183c4d6b3344259da3de41bf76df17227fa4c3fa309eecceb0d" +checksum = "791e05ae1859028c3a910aaed83e4910ab7701ce32fa663d2dc7cd957287cdf5" dependencies = [ "godot-bindings", "heck", @@ -134,9 +134,9 @@ dependencies = [ [[package]] name = "godot-core" -version = "0.3.2" +version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "24fb24e8689e236c80ad7580f87491c90a7f5f6fba8ea6f41863993d5b2b5b8f" +checksum = "9b1717ffba78bd2655c9ee1073752ac90d969b8a614800c469f00fc3ddc02439" dependencies = [ "glam", "godot-bindings", @@ -147,9 +147,9 @@ dependencies = [ [[package]] name = "godot-ffi" -version = "0.3.2" +version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d6f6f4fbcd1be7bae4c4e781c727317e68879afa1a6e33cd412160bc17e42827" +checksum = "419e46ba92fba076da67f35ad96faaa830c1de4e8175db2bb4251aeb58b14b08" dependencies = [ "godot-bindings", "godot-codegen", @@ -159,9 +159,9 @@ dependencies = [ [[package]] name = "godot-macros" -version = "0.3.2" +version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1b32a375c5a9852de4201751dbc650a39609c8f618c0a2c156958fd9fe6148e4" +checksum = "a464e26854e63825d36655759c24556c970a8777535466ebf4ecc7f8e87a4638" dependencies = [ "godot-bindings", "proc-macro2", diff --git a/Cargo.toml b/Cargo.toml index 3cfbebd..4cc9c9d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,9 +10,9 @@ version = "0.1.0" edition = "2021" [workspace.dependencies] -godot = { version = "0.3.2", features = ["experimental-threads"] } -godot-cell = "0.3" -godot-bindings = "0.3" +godot = { version = "0.4.0", features = ["experimental-threads"] } +godot-cell = "0.4" +godot-bindings = "0.4" itertools = "0.10" rand = "0.8" darling = { version = "0.20" } diff --git a/derive/src/enums.rs b/derive/src/enums.rs index c3b2c08..7158c59 100644 --- a/derive/src/enums.rs +++ b/derive/src/enums.rs @@ -93,7 +93,7 @@ pub fn script_enum_derive(input: proc_macro::TokenStream) -> proc_macro::TokenSt } impl #godot_types::meta::ToGodot for #enum_ident { - type ToVia<'a> = Self::Via; + type Pass = ::godot::meta::ByValue; fn to_godot(&self) -> Self::Via { #enum_from_self::from(self) diff --git a/derive/src/impl_attribute.rs b/derive/src/impl_attribute.rs index f3e2005..65229d4 100644 --- a/derive/src/impl_attribute.rs +++ b/derive/src/impl_attribute.rs @@ -25,7 +25,7 @@ pub fn godot_script_impl( let godot_types = godot_types(); let string_name_ty = string_name_ty(); let variant_ty = variant_ty(); - let call_error_ty = quote!(#godot_types::sys::GDExtensionCallErrorType); + let call_error_ty = quote!(#godot_types::meta::error::CallErrorType); let property_hints = property_hints(); let current_type = &body.self_ty; @@ -73,7 +73,7 @@ pub fn godot_script_impl( ::godot_rust_script::private_export::RustScriptPropDesc { name: stringify!(#arg_name), ty: #arg_type, - class_name: <<#arg_rust_type as #godot_types::meta::GodotConvert>::Via as #godot_types::meta::GodotType>::class_name(), + class_name: <<#arg_rust_type as #godot_types::meta::GodotConvert>::Via as #godot_types::meta::GodotType>::class_id(), exported: false, hint: #property_hints::NONE, hint_string: String::new(), @@ -84,10 +84,10 @@ pub fn godot_script_impl( quote_spanned! { arg.span() => #godot_types::prelude::FromGodot::try_from_variant( - args.get(#index).ok_or(#godot_types::sys::GDEXTENSION_CALL_ERROR_TOO_FEW_ARGUMENTS)? + args.get(#index).ok_or(#call_error_ty::TooFewArguments)? ).map_err(|err| { #godot_types::global::godot_error!("failed to convert variant for argument {} of {}: {}", stringify!(#arg_name), #fn_name_str, err); - #godot_types::sys::GDEXTENSION_CALL_ERROR_INVALID_ARGUMENT + #call_error_ty::InvalidArgument })?, } ) @@ -104,7 +104,7 @@ pub fn godot_script_impl( fnc.span() => #fn_name_str => { if args.len() > #arg_count { - return Err(#godot_types::sys::GDEXTENSION_CALL_ERROR_TOO_MANY_ARGUMENTS); + return Err(#call_error_ty::TooManyArguments); } Ok(#godot_types::prelude::ToGodot::to_variant(&self.#fn_name(#args))) @@ -134,7 +134,7 @@ pub fn godot_script_impl( return_type: ::godot_rust_script::private_export::RustScriptPropDesc { name: #fn_name_str, ty: #fn_return_ty, - class_name: <<#fn_return_ty_rust as #godot_types::meta::GodotConvert>::Via as #godot_types::meta::GodotType>::class_name(), + class_name: <<#fn_return_ty_rust as #godot_types::meta::GodotConvert>::Via as #godot_types::meta::GodotType>::class_id(), exported: false, hint: #property_hints::NONE, hint_string: String::new(), @@ -164,7 +164,7 @@ pub fn godot_script_impl( match name.to_string().as_str() { #method_dispatch - _ => Err(#godot_types::sys::GDEXTENSION_CALL_ERROR_INVALID_METHOD), + _ => Err(#call_error_ty::InvalidMethod), } } } diff --git a/derive/src/lib.rs b/derive/src/lib.rs index c4a60cb..bda6aac 100644 --- a/derive/src/lib.rs +++ b/derive/src/lib.rs @@ -28,7 +28,7 @@ pub fn derive(input: proc_macro::TokenStream) -> proc_macro::TokenStream { let godot_types = godot_types(); let variant_ty = variant_ty(); let string_name_ty = string_name_ty(); - let call_error_ty = quote!(#godot_types::sys::GDExtensionCallErrorType); + let call_error_ty = quote!(#godot_types::meta::error::CallErrorType); let base_class = opts .base @@ -409,7 +409,7 @@ fn derive_field_metadata( ::godot_rust_script::private_export::RustScriptPropDesc { name: #name, ty: #ty, - class_name: <<#rust_ty as #godot_types::meta::GodotConvert>::Via as #godot_types::meta::GodotType>::class_name(), + class_name: <<#rust_ty as #godot_types::meta::GodotConvert>::Via as #godot_types::meta::GodotType>::class_id(), exported: #is_exported, hint: #hint, hint_string: #hint_string, diff --git a/rust-script/src/editor_ui_hacks.rs b/rust-script/src/editor_ui_hacks.rs index b6afb2d..64d380e 100644 --- a/rust-script/src/editor_ui_hacks.rs +++ b/rust-script/src/editor_ui_hacks.rs @@ -6,7 +6,8 @@ use godot::classes::{EditorInterface, Engine}; use godot::global::godot_warn; -use godot::meta::ToGodot; +use godot::meta::{ByValue, ToGodot}; +use godot::obj::Singleton as _; use godot::prelude::GodotConvert; #[derive(Clone, Copy)] @@ -29,9 +30,9 @@ impl GodotConvert for EditorToasterSeverity { } impl ToGodot for EditorToasterSeverity { - type ToVia<'v> = Self::Via; + type Pass = ByValue; - fn to_godot(&self) -> Self::ToVia<'static> { + fn to_godot(&self) -> Self::Via { (*self).into() } } diff --git a/rust-script/src/interface.rs b/rust-script/src/interface.rs index 48829ac..e2f0bed 100644 --- a/rust-script/src/interface.rs +++ b/rust-script/src/interface.rs @@ -12,11 +12,13 @@ use std::marker::PhantomData; use std::ops::{Deref, DerefMut}; use std::{collections::HashMap, fmt::Debug}; -use godot::meta::{FromGodot, GodotConvert, ToGodot}; +use godot::meta::error::CallErrorType; +use godot::meta::{ByValue, FromGodot, GodotConvert, ToGodot}; use godot::obj::Inherits; use godot::prelude::{ConvertError, Gd, Object, StringName, Variant}; pub use crate::runtime::Context; +use crate::runtime::RustScript; pub use export::GodotScriptExport; pub use on_editor::OnEditor; @@ -35,7 +37,7 @@ pub trait GodotScript: Debug + GodotScriptImpl { method: StringName, args: &[&Variant], context: Context<'_, Self>, - ) -> Result; + ) -> Result; fn to_string(&self) -> String; fn property_state(&self) -> HashMap; @@ -51,7 +53,7 @@ pub trait GodotScriptImpl { name: StringName, args: &[&Variant], context: Context, - ) -> Result; + ) -> Result; } #[derive(Debug)] @@ -72,7 +74,8 @@ impl RsRef { let script = owner .upcast_ref::() .get_script() - .try_to::>>(); + .map(|script| script.try_cast::()) + .transpose(); let Ok(script) = script else { return Some(GodotScriptCastError::NotRustScript); @@ -127,20 +130,16 @@ where } impl ToGodot for RsRef { - type ToVia<'v> - = Gd - where - Self: 'v; + type Pass = ByValue; - fn to_godot(&self) -> Self::ToVia<'_> { + fn to_godot(&self) -> Self::Via { self.deref().clone() } } -impl<'v, T: GodotScript> ::godot::prelude::Var for RsRef +impl ::godot::prelude::Var for RsRef where - Self: GodotConvert::ToVia<'v>>, - Self: 'v, + Self: GodotConvert, { fn get_property(&self) -> Self::Via { ::to_godot(self) @@ -193,7 +192,7 @@ impl + Inherits> CastToScript fo self.try_to_script().unwrap_or_else(|err| { panic!( "`{}` was assumed to have rust script `{}`, but this was not the case at runtime!\nError: {}", - B::class_name(), + B::class_id(), T::CLASS_NAME, err, ); @@ -204,7 +203,7 @@ impl + Inherits> CastToScript fo self.try_into_script().unwrap_or_else(|err| { panic!( "`{}` was assumed to have rust script `{}`, but this was not the case at runtime!\nError: {}", - B::class_name(), + B::class_id(), T::CLASS_NAME, err ); diff --git a/rust-script/src/interface/export.rs b/rust-script/src/interface/export.rs index 51755e5..243aa99 100644 --- a/rust-script/src/interface/export.rs +++ b/rust-script/src/interface/export.rs @@ -35,7 +35,7 @@ impl GodotScriptExport for Gd { return custom; } - T::class_name().to_string() + T::class_id().to_string() } fn hint(custom: Option) -> PropertyHint { diff --git a/rust-script/src/interface/on_editor.rs b/rust-script/src/interface/on_editor.rs index d6cc2a3..e3b98ce 100644 --- a/rust-script/src/interface/on_editor.rs +++ b/rust-script/src/interface/on_editor.rs @@ -43,13 +43,14 @@ where impl godot::prelude::Var for OnEditor where - for<'v> T: ToGodot = ::Via> + FromGodot + 'v, + for<'v> T: ToGodot + FromGodot + 'v, Self: GodotConvert>, + T::Via: Clone, { fn get_property(&self) -> Self::Via { match self.value { ValueState::Invalid => None, - ValueState::Valid(ref value) => Some(value.to_godot()), + ValueState::Valid(ref value) => Some(value.to_godot_owned()), } } diff --git a/rust-script/src/interface/signals.rs b/rust-script/src/interface/signals.rs index f2e8195..b76ab56 100644 --- a/rust-script/src/interface/signals.rs +++ b/rust-script/src/interface/signals.rs @@ -11,7 +11,7 @@ use godot::builtin::{ }; use godot::classes::Object; use godot::global::{Error, PropertyHint}; -use godot::meta::{GodotConvert, GodotType, ToGodot}; +use godot::meta::{ByValue, GodotConvert, GodotType, ToGodot}; use godot::obj::{Gd, GodotClass}; use crate::static_script_registry::RustScriptPropDesc; @@ -112,7 +112,7 @@ macro_rules! signal_argument_desc { RustScriptPropDesc { name: $name, ty: <<<$type as GodotConvert>::Via as GodotType>::Ffi as godot::sys::GodotFfi>::VARIANT_TYPE.variant_as_nil(), - class_name: <<$type as GodotConvert>::Via as GodotType>::class_name(), + class_name: <<$type as GodotConvert>::Via as GodotType>::class_id(), exported: false, hint: PropertyHint::NONE, hint_string: String::new(), @@ -213,10 +213,7 @@ impl GodotConvert for ScriptSignal { } impl ToGodot for ScriptSignal { - type ToVia<'v> - = Self::Via - where - Self: 'v; + type Pass = ByValue; fn to_godot(&self) -> Self::Via { godot::builtin::Signal::from_object_signal(&self.host, self.name) diff --git a/rust-script/src/runtime/downgrade_self.rs b/rust-script/src/runtime/downgrade_self.rs index 235fdd0..4610803 100644 --- a/rust-script/src/runtime/downgrade_self.rs +++ b/rust-script/src/runtime/downgrade_self.rs @@ -18,7 +18,7 @@ where { fn downgrade_gd) -> R, R>(&mut self, closure: F) -> R { let mut_base = self.base_mut(); - let self_gd = mut_base.to_godot().cast(); + let self_gd = mut_base.to_godot_owned().cast(); closure(self_gd) } diff --git a/rust-script/src/runtime/metadata.rs b/rust-script/src/runtime/metadata.rs index fb3bb6a..7c4768d 100644 --- a/rust-script/src/runtime/metadata.rs +++ b/rust-script/src/runtime/metadata.rs @@ -7,7 +7,7 @@ use std::borrow::Cow; use std::ops::Deref; -use godot::meta::{ClassName, MethodInfo, PropertyInfo}; +use godot::meta::{ClassId, MethodInfo, PropertyInfo}; use godot::obj::{EngineBitfield, EngineEnum}; use godot::prelude::{Array, Dictionary}; use godot::sys::VariantType; @@ -23,7 +23,7 @@ impl ToDictionary for PropertyInfo { let mut dict = Dictionary::new(); dict.set("name", self.property_name.clone()); - dict.set("class_name", self.class_name.to_string_name()); + dict.set("class_name", self.class_id.to_string_name()); dict.set("type", self.variant_type.ord()); dict.set("hint", self.hint_info.hint.ord()); dict.set("hint_string", self.hint_info.hint_string.clone()); @@ -94,7 +94,7 @@ fn variant_type_to_str(var_type: VariantType) -> &'static str { } } -fn prop_doc_type(prop_type: VariantType, class_name: ClassName) -> Cow<'static, str> { +fn prop_doc_type(prop_type: VariantType, class_name: ClassId) -> Cow<'static, str> { match prop_type { VariantType::OBJECT => class_name.to_cow_str(), _ => variant_type_to_str(prop_type).into(), @@ -117,7 +117,7 @@ impl ToMethodDoc for MethodInfo { dict.set("name", self.method_name.clone()); dict.set( "return_type", - prop_doc_type(self.return_type.variant_type, self.return_type.class_name).as_ref(), + prop_doc_type(self.return_type.variant_type, self.return_type.class_id).as_ref(), ); dict.set("is_deprecated", false); dict.set("is_experimental", false); @@ -194,7 +194,7 @@ impl ToArgumentDoc for PropertyInfo { dict.set("name", self.property_name.clone()); dict.set( "type", - prop_doc_type(self.variant_type, self.class_name).as_ref(), + prop_doc_type(self.variant_type, self.class_id).as_ref(), ); }) } @@ -218,7 +218,7 @@ impl ToPropertyDoc for PropertyInfo { dict.set("name", self.property_name.clone()); dict.set( "type", - prop_doc_type(self.variant_type, self.class_name).as_ref(), + prop_doc_type(self.variant_type, self.class_id).as_ref(), ); dict.set("is_deprecated", false); dict.set("is_experimental", false); diff --git a/rust-script/src/runtime/mod.rs b/rust-script/src/runtime/mod.rs index c0241dc..89ebb0d 100644 --- a/rust-script/src/runtime/mod.rs +++ b/rust-script/src/runtime/mod.rs @@ -21,7 +21,7 @@ use godot::classes::{ ScriptLanguage, }; use godot::global::godot_warn; -use godot::obj::{GodotClass, Inherits}; +use godot::obj::{GodotClass, Inherits, Singleton as _}; use godot::prelude::{godot_print, Gd}; use godot::register::GodotClass; use once_cell::sync::Lazy; @@ -81,17 +81,17 @@ impl RustScriptExtensionLayer { load_rust_scripts(lib_init_fn); engine.register_script_language(&lang); - engine.register_singleton(&RustScriptLanguage::class_name().to_string_name(), &lang); + engine.register_singleton(&RustScriptLanguage::class_id().to_string_name(), &lang); ResourceSaver::singleton().add_resource_format_saver(&res_saver); engine.register_singleton( - &RustScriptResourceSaver::class_name().to_string_name(), + &RustScriptResourceSaver::class_id().to_string_name(), &RefCountedSingleton::new(&res_saver), ); ResourceLoader::singleton().add_resource_format_loader(&res_loader); engine.register_singleton( - &RustScriptResourceLoader::class_name().to_string_name(), + &RustScriptResourceLoader::class_id().to_string_name(), &RefCountedSingleton::new(&res_loader), ); @@ -103,16 +103,16 @@ impl RustScriptExtensionLayer { let mut engine = Engine::singleton(); if let Some(lang) = engine - .get_singleton(&RustScriptLanguage::class_name().to_string_name()) + .get_singleton(&RustScriptLanguage::class_id().to_string_name()) .map(Gd::cast::) { engine.unregister_script_language(&lang); - engine.unregister_singleton(&RustScriptLanguage::class_name().to_string_name()); + engine.unregister_singleton(&RustScriptLanguage::class_id().to_string_name()); lang.free(); } if let Some(res_loader_singleton) = engine - .get_singleton(&RustScriptResourceLoader::class_name().to_string_name()) + .get_singleton(&RustScriptResourceLoader::class_id().to_string_name()) .map(Gd::cast::) { let res_loader = res_loader_singleton.bind().get(); @@ -126,12 +126,12 @@ impl RustScriptExtensionLayer { ResourceLoader::singleton() .remove_resource_format_loader(&res_loader.cast::()); - engine.unregister_singleton(&RustScriptResourceLoader::class_name().to_string_name()); + engine.unregister_singleton(&RustScriptResourceLoader::class_id().to_string_name()); res_loader_singleton.free(); } if let Some(res_saver_singleton) = engine - .get_singleton(&RustScriptResourceSaver::class_name().to_string_name()) + .get_singleton(&RustScriptResourceSaver::class_id().to_string_name()) .map(Gd::cast::) { let res_saver = res_saver_singleton.bind().get(); @@ -145,7 +145,7 @@ impl RustScriptExtensionLayer { ResourceSaver::singleton() .remove_resource_format_saver(&res_saver.clone().cast::()); - engine.unregister_singleton(&RustScriptResourceSaver::class_name().to_string_name()); + engine.unregister_singleton(&RustScriptResourceSaver::class_id().to_string_name()); res_saver_singleton.free(); } diff --git a/rust-script/src/runtime/resource_loader.rs b/rust-script/src/runtime/resource_loader.rs index 3de84d4..f0e8f9b 100644 --- a/rust-script/src/runtime/resource_loader.rs +++ b/rust-script/src/runtime/resource_loader.rs @@ -6,7 +6,7 @@ use godot::classes::{ClassDb, IResourceFormatLoader, IScriptLanguageExtension, Script}; use godot::global::godot_print; -use godot::obj::Base; +use godot::obj::{Base, Singleton as _}; use godot::prelude::{ godot_api, GString, Gd, GodotClass, PackedStringArray, StringName, ToGodot, Variant, }; @@ -45,7 +45,8 @@ impl IResourceFormatLoader for RustScriptResourceLoader { } fn handles_type(&self, type_: StringName) -> bool { - type_ == StringName::from("Script") || type_ == self.script_lang().bind().get_type().into() + type_ == StringName::from("Script") + || type_ == (&self.script_lang().bind().get_type()).into() } fn get_resource_type(&self, path: GString) -> GString { diff --git a/rust-script/src/runtime/rust_script.rs b/rust-script/src/runtime/rust_script.rs index 728e296..00500a6 100644 --- a/rust-script/src/runtime/rust_script.rs +++ b/rust-script/src/runtime/rust_script.rs @@ -13,7 +13,7 @@ use godot::classes::{ use godot::global::{godot_error, godot_print, godot_warn, PropertyUsageFlags}; use godot::meta::{MethodInfo, PropertyInfo, ToGodot}; use godot::obj::script::create_script_instance; -use godot::obj::{EngineBitfield, InstanceId, WithBaseField}; +use godot::obj::{EngineBitfield, InstanceId, Singleton as _, WithBaseField}; use godot::prelude::{ godot_api, Array, Base, Callable, Dictionary, GString, Gd, GodotClass, StringName, Variant, VariantArray, @@ -52,10 +52,10 @@ pub(crate) struct RustScript { impl RustScript { pub fn new(class_name: String) -> Gd { let mut inst: Gd = ClassDb::singleton() - .instantiate(&::class_name().to_string_name()) + .instantiate(&::class_id().to_string_name()) .to(); - inst.bind_mut().class_name = GString::from(class_name); + inst.bind_mut().class_name = GString::from(&class_name); inst } @@ -115,7 +115,11 @@ impl RustScript { ), }; - if let Err(err) = base.get_script().try_to::>() { + if let Err(err) = base + .get_script() + .map(|script| script.try_cast::()) + .transpose() + { godot_warn!("expected new script to be previously assigned RustScript, but it wasn't!"); godot_warn!("{}", err); @@ -150,7 +154,7 @@ impl IScriptExtension for RustScript { } fn get_global_name(&self) -> StringName { - self.get_class_name().into() + (&self.get_class_name()).into() } fn get_source_code(&self) -> GString { @@ -191,14 +195,12 @@ impl IScriptExtension for RustScript { let callbale_args = VariantArray::from(&[for_object.to_variant()]); - for_object - .connect_ex( - "script_changed", - &Callable::from_object_method(&self.to_gd(), "init_script_instance") - .bindv(&callbale_args), - ) - .flags(ConnectFlags::ONE_SHOT.ord() as u32) - .done(); + for_object.connect_flags( + "script_changed", + &Callable::from_object_method(&self.to_gd(), "init_script_instance") + .bindv(&callbale_args), + ConnectFlags::ONE_SHOT, + ); create_script_instance(instance, for_object) } @@ -426,11 +428,11 @@ impl IScriptExtension for RustScript { }; // clear script to destroy script instance. - object.set_script(&Variant::nil()); + object.set_script(Option::<&Gd