diff --git a/src/runner/instance.rs b/src/runner/instance.rs index f8103a4..448adc2 100644 --- a/src/runner/instance.rs +++ b/src/runner/instance.rs @@ -29,6 +29,14 @@ fn dom(lua:&mlua::Lua,mut f:impl FnMut(&mut WeakDom)->mlua::Result)->mlua: f(&mut *lua.app_data_mut::().ok_or(mlua::Error::runtime("DataModel missing"))?) } +fn coerce_float(value:&mlua::Value)->Option{ + match value{ + &mlua::Value::Integer(i)=>Some(i as f32), + &mlua::Value::Number(f)=>Some(f as f32), + _=>None, + } +} + impl mlua::UserData for Instance{ fn add_fields<'lua,F:mlua::UserDataFields<'lua,Self>>(fields:&mut F){ fields.add_field_method_get("Parent",|lua,this|{ @@ -46,6 +54,24 @@ impl mlua::UserData for Instance{ } fn add_methods<'lua,M:mlua::UserDataMethods<'lua,Self>>(methods:&mut M){ + methods.add_method("GetChildren",|lua,this,_:()| + dom(lua,|dom|{ + let instance=this.get(dom)?; + let children:Vec<_>=instance + .children() + .iter() + .copied() + .map(Instance::new) + .collect(); + Ok(children) + }) + ); + methods.add_method("IsA",|lua,this,classname:mlua::String| + dom(lua,|dom|{ + let instance=this.get(dom)?; + Ok(crate::context::class_is_a(instance.class.as_str(),classname.to_str()?)) + }) + ); methods.add_meta_function(mlua::MetaMethod::NewIndex,|lua,(this,index,value):(Self,mlua::String,mlua::Value)| dom(lua,|dom|{ let instance=this.get_mut(dom)?; @@ -57,11 +83,11 @@ impl mlua::UserData for Instance{ let typed_value:Vector3=value.as_userdata().ok_or(mlua::Error::runtime("Expected Userdata"))?.take()?; instance.properties.insert(index_str.to_owned(),rbx_types::Variant::Vector3(typed_value.into())); }, - rbx_types::Variant::Float64(_)=>{ - let typed_value:f64=value.as_f64().ok_or(mlua::Error::runtime("Expected f64"))?; - instance.properties.insert(index_str.to_owned(),rbx_types::Variant::Float64(typed_value)); + rbx_types::Variant::Float32(_)=>{ + let typed_value:f32=coerce_float(&value).ok_or(mlua::Error::runtime("Expected f32"))?; + instance.properties.insert(index_str.to_owned(),rbx_types::Variant::Float32(typed_value)); }, - _=>unimplemented!(), + other=>println!("Unimplemented property type: {other:?}"), } Ok(()) })