int Instance::lua_index(lua_State* L){ Instance* inst = checkInstance(L, 1); if(inst != NULL){ const char* name = luaL_checkstring(L, 2); lua_getmetatable(L, 1);//-3 lua_getfield(L, -1, "__propertygetters");//-2 lua_getfield(L, -1, name);//-1 if(lua_iscfunction(L, -1)){ lua_remove(L, -2); lua_remove(L, -2); lua_pushvalue(L, 1); lua_call(L, 1, 1); return 1; }else{ lua_pop(L, 2); //Check methods lua_getfield(L, -1, "__methods");//-2 lua_getfield(L, -1, name);//-1 if(lua_iscfunction(L, -1)){ lua_remove(L, -2); lua_remove(L, -3); return 1; }else{ lua_pop(L, 2); //Check events lua_getfield(L, -1, "__events");//-2 lua_getfield(L, -1, name);//-1 if(lua_iscfunction(L, -1)){ lua_remove(L, -2); lua_remove(L, -3); lua_pushvalue(L, 1); lua_call(L, 1, 1); return 1; }else{ lua_pop(L, 3); Instance* kiddie = inst->FindFirstChild(name, false); if(kiddie){ return kiddie->wrap_lua(L); } return luaL_error(L, "attempt to index '%s' (a nil value)", name); } } } } return 0; }
int Instance::lua_FindFirstChild(lua_State* L){ Instance* inst = checkInstance(L, 1); if(inst){ const char* kidName = luaL_checkstring(L, 2); bool recursive = false; if(!lua_isnoneornil(L, 3)){ if(lua_isboolean(L, 3)){ recursive = lua_toboolean(L, 3); }else{ luaL_typerror(L, 3, "boolean"); } } Instance* foundStuff = inst->FindFirstChild(kidName, recursive); if(foundStuff != NULL){ return foundStuff->wrap_lua(L); } lua_pushnil(L); return 1; } return luaL_error(L, COLONERR, "FindFirstChild"); }
Instance* Instance::FindFirstChild(std::string name, bool recursive){ for(std::vector<Instance*>::size_type i = 0; i != children.size(); i++){ Instance* kid = children[i]; if(kid != NULL){ if(kid->Name == name){ return kid; } } } if(recursive){ for(std::vector<Instance*>::size_type i = 0; i != children.size(); i++){ Instance* kid = children[i]; if(kid != NULL){ Instance* myFind = kid->FindFirstChild(name, recursive); if(myFind != NULL){ return myFind; } } } } return NULL; }