Skip to content

Commit 2341a4a

Browse files
ZughyZughy
and
Zughy
authoredOct 13, 2020
Add ObjectRef:get_children() (#10480)
Co-authored-by: Zughy <4279489-marco_a@users.noreply.gitlab.com>
1 parent 521a042 commit 2341a4a

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed
 

‎doc/lua_api.txt

+2
Original file line numberDiff line numberDiff line change
@@ -6206,6 +6206,8 @@ object you are working with still exists.
62066206
should appear in first person.
62076207
* `get_attach()`: returns parent, bone, position, rotation, forced_visible,
62086208
or nil if it isn't attached.
6209+
* `get_children()`: returns a list of ObjectRefs that are attached to the
6210+
object.
62096211
* `set_detach()`
62106212
* `set_bone_position(bone, position, rotation)`
62116213
* `bone`: string

‎src/script/lua_api/l_object.cpp

+22
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,27 @@ int ObjectRef::l_get_attach(lua_State *L)
738738
return 5;
739739
}
740740

741+
// get_children(self)
742+
int ObjectRef::l_get_children(lua_State *L)
743+
{
744+
GET_ENV_PTR;
745+
746+
ObjectRef *ref = checkobject(L, 1);
747+
ServerActiveObject *sao = getobject(ref);
748+
if (sao == nullptr)
749+
return 0;
750+
751+
const std::unordered_set<int> child_ids = sao->getAttachmentChildIds();
752+
int i = 0;
753+
lua_createtable(L, child_ids.size(), 0);
754+
for (const int id : child_ids) {
755+
ServerActiveObject *child = env->getActiveObject(id);
756+
getScriptApiBase(L)->objectrefGetOrCreate(L, child);
757+
lua_rawseti(L, -2, ++i);
758+
}
759+
return 1;
760+
}
761+
741762
// set_detach(self)
742763
int ObjectRef::l_set_detach(lua_State *L)
743764
{
@@ -2337,6 +2358,7 @@ luaL_Reg ObjectRef::methods[] = {
23372358
luamethod(ObjectRef, get_bone_position),
23382359
luamethod(ObjectRef, set_attach),
23392360
luamethod(ObjectRef, get_attach),
2361+
luamethod(ObjectRef, get_children),
23402362
luamethod(ObjectRef, set_detach),
23412363
luamethod(ObjectRef, set_properties),
23422364
luamethod(ObjectRef, get_properties),

‎src/script/lua_api/l_object.h

+3
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,9 @@ class ObjectRef : public ModApiBase {
143143
// get_attach(self)
144144
static int l_get_attach(lua_State *L);
145145

146+
// get_children(self)
147+
static int l_get_children(lua_State *L);
148+
146149
// set_detach(self)
147150
static int l_set_detach(lua_State *L);
148151

0 commit comments

Comments
 (0)
Please sign in to comment.