• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

C++ TFS 8.6 Summon getMaster crashing

peha

New Member
Joined
Apr 21, 2020
Messages
12
Reaction score
0
Hello, I'm trying to changing name of summon depending on his master name. In tfs0.3.6pl1 similar code worked, but now everytime I'm trying to master->getName() it's crashing server. It's downgraded tfs to 8.6 and I've no clue how to do this.

C++:
void ProtocolGame::AddCreature(NetworkMessage& msg, const Creature* creature, bool known, uint32_t remove)
{
    const Player* otherPlayer = creature->getPlayer();
    if (!known) {
        msg.add<uint16_t>(0x61);
        msg.add<uint32_t>(remove);
        msg.add<uint32_t>(creature->getID());
        if (!(creature->getName() == "Clone"))
            msg.addString(creature->getName());
        else {
            const Creature* master = creature->getMaster();
            msg.addString(master->getName());
        }
    }
    else {
        msg.add<uint16_t>(0x62);
        msg.add<uint32_t>(creature->getID());
    }
 
C++:
const Creature* master = creature->getMaster();
msg.addString(master->getName());
if master is nullptr you have the crash. Check if master exists.
 
C++:
const Creature* master = creature->getMaster();
msg.addString(master->getName());
if master is nullptr you have the crash. Check if master exists.
In this part of code master never exists, but in spell after summoning when I’m printing master name it exist. So it’s weird.
Post automatically merged:

Okay so I figured it out. For future guys with problems.

Go to luascript.cpp:
find:
C++:
int LuaScriptInterface::luaGameCreateMonster(lua_State* L)

and change whole function to:
C++:
int LuaScriptInterface::luaGameCreateMonster(lua_State* L)
{
    // Game.createMonster(monsterName, position[, extended = false[, force = false]])
    Monster* monster = Monster::createMonster(getString(L, 1));
    if (!monster) {
        lua_pushnil(L);
        return 1;
    }

    Creature* creature = getCreature(L, 2);
    if (creature)
        monster->setMaster(creature);

    const Position& position = getPosition(L, 3);
    bool extended = getBoolean(L, 4, false);
    bool force = getBoolean(L, 5, false);
    if (g_events->eventMonsterOnSpawn(monster, position, false, true) || force) {
        if (g_game.placeCreature(monster, position, extended, force)) {
            pushUserdata<Monster>(L, monster);
            setMetatable(L, -1, "Monster");
        } else {
            delete monster;
            lua_pushnil(L);
        }
    } else {
        delete monster;
        lua_pushnil(L);
    }
    return 1;
}

In compat.lua find function doSummonCreature and change it for this:
Lua:
function doSummonCreature(name, creature, pos, ...)
    local m = Game.createMonster(name, creature, pos, ...) return m and m:getId() or false
end

Now you are not using
Lua:
local summon = Game.createMonster(monsterName, position, true)
but this
Lua:
local summon = Game.createMonster(monsterName, creature, position, true)

if u want place monster without master use it like this:
Lua:
local summon = Game.createMonster(monsterName, nullptr, position, true)
 
Last edited:
Back
Top Bottom