From 40655cdf449259999468d83923b290470ed8ff17 Mon Sep 17 00:00:00 2001 From: Quaternions Date: Thu, 3 Oct 2024 18:55:50 -0700 Subject: [PATCH] get referent from any class --- src/runner/instance.rs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/runner/instance.rs b/src/runner/instance.rs index 666a8db..0dad951 100644 --- a/src/runner/instance.rs +++ b/src/runner/instance.rs @@ -29,6 +29,11 @@ fn get_full_name(dom:&rbx_dom_weak::WeakDom,instance:&rbx_dom_weak::Instance)->S full_name } +//workaround until I have an enum of classes +struct Dereferent(Ref); +impl mlua::UserData for Dereferent{} +type_from_lua_userdata!(Dereferent); + trait Referent{ fn referent(&self)->Ref; fn get<'a>(&self,dom:&'a WeakDom)->mlua::Result<&'a rbx_dom_weak::Instance>{ @@ -61,6 +66,9 @@ macro_rules! class_composition{ ($class:ident,($($superclass:ident),*))=>{ impl mlua::UserData for $class{ fn add_fields<'lua,F:mlua::UserDataFields<'lua,Self>>(fields:&mut F){ + fields.add_field_method_get("Referent",|_,this|{ + Ok(Dereferent(this.referent())) + }); $( $superclass::composition_add_fields(fields); )* @@ -86,9 +94,10 @@ impl Instance{ Ok(Instance::new(instance.parent())) }) }); - fields.add_field_method_set("Parent",|lua,this,val:Self|{ + fields.add_field_method_set("Parent",|lua,this,val:mlua::AnyUserData|{ + let Dereferent(referent)=mlua::AnyUserDataExt::get(&val,"Referent")?; dom(lua,|dom|{ - dom.transfer_within(this.referent(),val.referent); + dom.transfer_within(this.referent(),referent); Ok(()) }) });