From 0148476648fc9034b20a198deadcb3d58c3f9b2c Mon Sep 17 00:00:00 2001 From: Quaternions Date: Fri, 4 Oct 2024 18:43:40 -0700 Subject: [PATCH] Instance FindFirstChild + WaitForChild (no difference in implementation) --- src/runner/instance.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/runner/instance.rs b/src/runner/instance.rs index 3e79efd..b219885 100644 --- a/src/runner/instance.rs +++ b/src/runner/instance.rs @@ -158,6 +158,29 @@ impl Instance{ Ok(children) }) ); + let ffc=|lua,this:&T,(name,search_descendants):(mlua::String,mlua::Value)|{ + let name_str=name.to_str()?; + let search_descendants=match search_descendants{ + mlua::Value::Nil=>false, + mlua::Value::Boolean(b)=>b, + _=>Err(mlua::Error::runtime("Invalid argument #3 bool expected"))?, + }; + dom(lua,|dom|{ + let instance=this.get(dom)?; + let child=match search_descendants{ + true=>dom.descendants_of(this.referent()).find(|inst|inst.name==name_str), + false=>instance.children().iter().filter_map(|&r| + dom.get_by_ref(r) + ).find(|inst|inst.name==name_str), + }; + match child{ + Some(instance)=>Instance::new(instance.referent()).into_lua(lua), + None=>mlua::Value::Nil.into_lua(lua), + } + }) + }; + methods.add_method("FindFirstChild",ffc); + methods.add_method("WaitForChild",ffc); methods.add_method("GetDescendants",|lua,this,_:()| dom(lua,|dom|{ let children:Vec<_>=dom