Store tool identifier to index mapping in tools.index

This mimics sim.walls.* rather than elem.*_PT_*. The latter is fine because element identifiers follow the *_PT_* pattern, but tool identifiers don't, so they'd be a pain to tell apart from the rest of the API.
This commit is contained in:
Tamás Bálint Misius 2024-10-04 12:09:05 +02:00
parent c03c2f3fd0
commit b26d057783
No known key found for this signature in database
GPG Key ID: 5B472A12F6ECA9F2

View File

@ -365,6 +365,8 @@ void LuaTools::Open(lua_State *L)
};
lua_newtable(L);
luaL_register(L, NULL, reg);
lua_newtable(L);
lua_setfield(L, -2, "index");
lua_setglobal(L, "tools");
auto &toolList = lsi->gameModel->GetTools();
for (int i = 0; i < int(toolList.size()); ++i)
@ -380,6 +382,7 @@ void LuaTools::Open(lua_State *L)
void LuaTools::SetToolIndex(lua_State *L, ByteString identifier, std::optional<int> index)
{
lua_getglobal(L, "tools");
lua_getfield(L, -1, "index");
tpt_lua_pushByteString(L, identifier);
if (index)
{
@ -390,5 +393,5 @@ void LuaTools::SetToolIndex(lua_State *L, ByteString identifier, std::optional<i
lua_pushnil(L);
}
lua_settable(L, -3);
lua_pop(L, 1);
lua_pop(L, 2);
}