diff --git a/ag.cc b/ag.cc index 8aab2f7..39013fc 100644 --- a/ag.cc +++ b/ag.cc @@ -88,9 +88,34 @@ namespace ag int argc = lua_gettop(L); if (argc == 2 && lua_isnumber(L, 1) && lua_isnumber(L, 2)) { - g_engine->pickObjects(lua_tointeger(L, 1), lua_tointeger(L, 2)); + lua_newtable(L); + int next_index = 1; + refptr< vector > objects = + g_engine->pickObjects(lua_tointeger(L, 1), lua_tointeger(L, 2)); + for (vector::const_iterator it = objects->begin(); + it != objects->end(); + it++) + { + lua_getfield(L, LUA_GLOBALSINDEX, "ag"); /* pushes 1 */ + lua_getfield(L, -1, "_objects"); /* pushes 1 */ + lua_pushinteger(L, *it); /* pushes 1 */ + lua_gettable(L, -2); /* replace top */ + lua_remove(L, -2); /* remove 1 */ + lua_remove(L, -1); /* remove 1 */ + if (!lua_isnil(L, -1)) + { + lua_pushinteger(L, next_index++); /* push key */ + lua_pushvalue(L, -2); /* push value */ + lua_settable(L, argc + 1); /* pop 2 */ + } + lua_pop(L, 1); /* pop 1 */ + } } - return 0; + else + { + lua_pushnil(L); + } + return 1; } static void print_val(lua_State * L, int index) @@ -270,6 +295,14 @@ namespace ag lua_setfield(L, -2, "getAABB"); lua_pushcfunction(L, object::getSize); lua_setfield(L, -2, "getSize"); + + /* ag._objects[id] = obj */ + lua_getfield(L, LUA_GLOBALSINDEX, "ag"); /* pushes 1 */ + lua_getfield(L, -1, "_objects"); /* pushes 1 */ + lua_pushinteger(L, id); /* pushes 1 */ + lua_pushvalue(L, 1); /* pushes 1 */ + lua_settable(L, -3); /* pops 2 */ + lua_pop(L, 2); /* pops 2 */ } static void createLuaAMotor(lua_State * L, int id) @@ -1093,6 +1126,14 @@ namespace ag { int id = lua_tointeger(L, -1); g_engine->removeObject(id); + + /* ag._objects[id] = nil */ + lua_getfield(L, LUA_GLOBALSINDEX, "ag"); /* pushes 1 */ + lua_getfield(L, -1, "_objects"); /* pushes 1 */ + lua_pushinteger(L, id); /* pushes 1 */ + lua_pushnil(L); /* pushes 1 */ + lua_settable(L, -3); /* pops 2 */ + lua_pop(L, 2); /* pops 2 */ } lua_pop(L, 1); clearLuaTable(L, 1); diff --git a/ag.lua b/ag.lua index 5c2443c..7c8d359 100644 --- a/ag.lua +++ b/ag.lua @@ -1,5 +1,6 @@ next = ag.next type = ag.type +ag._objects = {} pairs = function(table) return next, table, nil