From 30eff9a766fe9f31bb79bb07a6fdb88205288b1d Mon Sep 17 00:00:00 2001 From: xiaojin Date: Wed, 4 Aug 2021 16:18:42 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=B3=20chore(=E5=B7=A5=E5=85=B7):=20?= =?UTF-8?q?=E6=9B=B4=E6=96=B0=20lua=20ecs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- framework/lualib-src/lua-ecs/luaecs.c | 5 +++- framework/lualib-src/lua-ecs/test10.lua | 39 +++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 framework/lualib-src/lua-ecs/test10.lua diff --git a/framework/lualib-src/lua-ecs/luaecs.c b/framework/lualib-src/lua-ecs/luaecs.c index 6f32c81..19356bc 100644 --- a/framework/lualib-src/lua-ecs/luaecs.c +++ b/framework/lualib-src/lua-ecs/luaecs.c @@ -162,6 +162,9 @@ add_component_id_(lua_State *L, int world_index, struct entity_world *w, int cid } ++pool->n; pool->id[index] = eid; + if (index > 0 && eid <= pool->id[index-1]) { + luaL_error(L, "Add component %d fail", cid); + } return index; } @@ -1119,7 +1122,7 @@ update_iter(lua_State *L, int world_index, int lua_index, struct group_iter *ite luaL_error(L, "Missing lua table for %d", k->id); } lua_insert(L, -2); - lua_rawseti(L, -2, index); + lua_rawseti(L, -2, index + 1); } else { void *buffer = entity_add_sibling_(iter->world, mainkey, idx, k->id, NULL, L, world_index); write_component_object(L, k->field_n, f, buffer); diff --git a/framework/lualib-src/lua-ecs/test10.lua b/framework/lualib-src/lua-ecs/test10.lua new file mode 100644 index 0000000..f6aa67a --- /dev/null +++ b/framework/lualib-src/lua-ecs/test10.lua @@ -0,0 +1,39 @@ +local ecs = require "ecs" + +local w = ecs.world() + +w:register { + name = "key", + type = "int", +} + +w:register { + name = "temp", + type = "lua", +} + +for i = 1, 10 do + w:new { key = i } +end + +local function add_temp(i) + for v in w:select "key:in temp?temp" do + if v.key == i then + v.temp = "Temp" + end + end +end + +local function print_entity() + for v in w:select "key:in temp:in" do + print(v.key, v.temp) + end +end + +add_temp(5) +print_entity() + +add_temp(6) +print_entity() + +assert(pcall(add_temp, 4) == false) \ No newline at end of file