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