diff --git a/Content/Documentation/ScriptingAPI-Documentation.md b/Content/Documentation/ScriptingAPI-Documentation.md index 495dab8fa..b922e1043 100644 --- a/Content/Documentation/ScriptingAPI-Documentation.md +++ b/Content/Documentation/ScriptingAPI-Documentation.md @@ -629,6 +629,7 @@ The scene holds components. Entity handles can be used to retrieve associated co - UpdateHierarchy() -- updates the full scene hierarchy system. Useful if you modified for example a parent transform and children immediately need up to date result in the script - CreateEntity() : int entity -- creates an empty entity and returns it +- FindAllEntities() : table[entities] -- returns a table with all the entities present in the given scene - Entity_FindByName(string value, opt Entity ancestor = INVALID_ENTITY) : int entity -- returns an entity ID if it exists, and INVALID_ENTITY otherwise. You can specify an ancestor entity if you only want to find entities that are descendants of ancestor entity - Entity_Remove(Entity entity, bool recursive = true, bool keep_sorted = false) -- removes an entity and deletes all its components if it exists. If recursive is specified, then all children will be removed as well (enabled by default). If keep_sorted is specified, then component order will be kept (disabled by default, slower) - Entity_Duplicate(Entity entity) : int entity -- duplicates all of an entity's components and creates a new entity with them. Returns the clone entity handle diff --git a/WickedEngine/wiScene_BindLua.cpp b/WickedEngine/wiScene_BindLua.cpp index f4b693663..85c6b2817 100644 --- a/WickedEngine/wiScene_BindLua.cpp +++ b/WickedEngine/wiScene_BindLua.cpp @@ -512,6 +512,7 @@ Luna::FunctionType Scene_BindLua::methods[] = { lunamethod(Scene_BindLua, Merge), lunamethod(Scene_BindLua, UpdateHierarchy), lunamethod(Scene_BindLua, Intersects), + lunamethod(Scene_BindLua, FindAllEntities), lunamethod(Scene_BindLua, Entity_FindByName), lunamethod(Scene_BindLua, Entity_Remove), lunamethod(Scene_BindLua, Entity_Duplicate), @@ -701,6 +702,26 @@ int Scene_BindLua::Merge(lua_State* L) return 0; } +int Scene_BindLua::FindAllEntities(lua_State* L) +{ + wi::unordered_set listOfAllEntities; + scene->FindAllEntities(listOfAllEntities); + + int idx = 1; // lua indexes start at 1 + + lua_createtable(L, listOfAllEntities.size(), 0); // fixed size table + int entt_table = lua_gettop(L); + for (wi::ecs::Entity entity : listOfAllEntities) + { + wi::lua::SSetLongLong(L, entity); + lua_rawseti(L, entt_table, lua_Integer(idx)); + ++idx; + } + // our table should be already on the stack + return 1; +} + + int Scene_BindLua::Entity_FindByName(lua_State* L) { int argc = wi::lua::SGetArgCount(L); diff --git a/WickedEngine/wiScene_BindLua.h b/WickedEngine/wiScene_BindLua.h index 3a3c7f522..ed377cc86 100644 --- a/WickedEngine/wiScene_BindLua.h +++ b/WickedEngine/wiScene_BindLua.h @@ -39,6 +39,7 @@ namespace wi::lua::scene int Intersects(lua_State* L); + int FindAllEntities(lua_State* L); int Entity_FindByName(lua_State* L); int Entity_Remove(lua_State* L); int Entity_Duplicate(lua_State* L);