diff --git a/src/game/game.cpp b/src/game/game.cpp index 6f659e92916..97d0b066b9b 100644 --- a/src/game/game.cpp +++ b/src/game/game.cpp @@ -10066,6 +10066,19 @@ void Game::removeMonster(const std::shared_ptr &monster) { } } +void Game::updateMonster(const std::shared_ptr &monster, const std::string &oldName, uint32_t oldId) { + if (!monster) { + return; + } + + const auto &newName = monster->getLowerName(); + monstersNameIndex.erase(oldName); + monstersNameIndex[newName] = monsters.size() - 1; + + monstersIdIndex.erase(oldId); + monstersIdIndex[monster->getID()] = monsters.size() - 1; +} + std::shared_ptr Game::getGuild(uint32_t id, bool allowOffline /* = flase */) const { auto it = guilds.find(id); if (it == guilds.end()) { diff --git a/src/game/game.hpp b/src/game/game.hpp index 9a4b59f8a55..f8e6400630d 100644 --- a/src/game/game.hpp +++ b/src/game/game.hpp @@ -541,6 +541,7 @@ class Game { void addMonster(const std::shared_ptr &monster); void removeMonster(const std::shared_ptr &monster); + void updateMonster(const std::shared_ptr &monster, const std::string &oldName, uint32_t oldId); std::shared_ptr getGuild(uint32_t id, bool allowOffline = false) const; std::shared_ptr getGuildByName(const std::string &name, bool allowOffline = false) const; diff --git a/src/lua/functions/creatures/monster/monster_functions.cpp b/src/lua/functions/creatures/monster/monster_functions.cpp index 3f5d0c25ea8..e174edf031b 100644 --- a/src/lua/functions/creatures/monster/monster_functions.cpp +++ b/src/lua/functions/creatures/monster/monster_functions.cpp @@ -138,6 +138,12 @@ int MonsterFunctions::luaMonsterSetType(lua_State* L) { g_logger().warn("[Warning - MonsterFunctions::luaMonsterSetType] Unknown event name: {}", scriptName); } } + + // Update created monster + const std::string oldName = monster->getLowerName(); + uint32_t oldId = monster->getID(); + g_game().updateMonster(monster, oldName, oldId); + // Assign new MonsterType monster->mType = mType; monster->nameDescription = asLowerCaseString(mType->nameDescription);