• 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!

Feature setCreatureName & monster:setName for TFS 1.2

unknownRanger

Banned User
Joined
Aug 7, 2016
Messages
4
Reaction score
14
Ever wanted to create your own monsters but just too damn lazy to create a new file?
Well now you can rename any monster in the game and just call it something else... great for those illusionist scripts.. more on that later :)

There is 2 options and both work, you can use either
Code:
setCreatureName(cid, name, description)
or you can use
Code:
monster:setName(name, description)


------------------------------------
monster.h
find this
Code:
class Monster final : public Creature
{
    public:

place this right under it
Code:
        std::string name, nameDescription, altStrDesc;

next find this function
Code:
        const std::string& getName() const final {
            return mType->name;
        }

and replace it with this
Code:
        const std::string& getName() const final {
            if (!name.empty()) {
                return name;
            }
            return mType->name;
        }

next find this function
Code:
        const std::string& getNameDescription() const final {
            return mType->nameDescription;
        }

and replace it with this
Code:
        const std::string& getNameDescription() const final {
            if (nameDescription.empty()) {
                return mType->nameDescription;
            }
            return nameDescription;
        }

next find this function
Code:
        std::string getDescription(int32_t) const final {
            return strDescription + '.';
        }

and replace it with this
Code:
        std::string getDescription(int32_t) const final {
            if (!altStrDesc.empty()) {
                return altStrDesc + '.';
            }
            return strDescription + '.';
        }

Now underneath all that code place these functions
Code:
        void setName(std::string n) {
            name = n;
        }
        void setDescription(std::string desc) {
            nameDescription = desc;
        }
        void setStrDescription(std::string altdesc) {
            altStrDesc = altdesc;
        }
---------------------------------------
luascript.cpp
find this
Code:
    registerMethod("Monster", "searchTarget", LuaScriptInterface::luaMonsterSearchTarget);
and place this right underneath
Code:
    // monster:setName(name, description)
    registerMethod("Monster", "setName", LuaScriptInterface::luaMonsterSetName);

find this
Code:
    lua_register(luaState, "sendGuildChannelMessage", LuaScriptInterface::luaSendGuildChannelMessage);
and place this right underneath
Code:
    //setCreatureName(cid, newName, newDescription)
    lua_register(luaState, "setCreatureName", LuaScriptInterface::luaSetCreatureName);


find this function
Code:
int LuaScriptInterface::luaMonsterSearchTarget(lua_State* L)

and place this right underneath
Code:
int LuaScriptInterface::luaSetCreatureName(lua_State* L)
{
    // setCreatureName(cid, name, description)
    std::string newDesc = getString(L, 3);
    std::string newName = getString(L, 2);

    Creature* creature = getCreature(L, 1);
    if (!creature) {
        reportErrorFunc(getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND));
        pushBoolean(L, false);
        return 1;
    }
    Monster* monster = (Monster*)creature;
    monster->setName(newName);
    monster->setDescription(newDesc);
    monster->setStrDescription(newDesc);
    lua_pushboolean(L, true);
 
    return 1;
}

int LuaScriptInterface::luaMonsterSetName(lua_State* L)
{
    // monster:setName(name, description)
    std::string newDesc = getString(L, 3);
    std::string newName = getString(L, 2);

    Monster* monster = getUserdata<Monster>(L, 1);
    if (!monster) {
        reportErrorFunc(getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND));
        pushBoolean(L, false);
        return 1;
    }
    monster->setName(newName);
    monster->setDescription(newDesc);
    monster->setStrDescription(newDesc);
    lua_pushboolean(L, true);

    return 1;
}
-----------------------------------
luascript.h
You can place these together in the monster section e.g. // Monster
Code:
    // monster:setName(name, description)
    static int luaMonsterSetName(lua_State* L);

    //setCreatureName(cid, newName, newDescription)
    static int luaSetCreatureName(lua_State* L);
 
Last edited:
We need to re-log to change the see the new name of monster.
Anyone have any idea how to solve this?
 
You should probably move your updated methods from monsters.h to monsters.cpp, as they are no longer single getters (get* methods). Just have a look how it's done in case of DepotChest class and DepotChest::getRealParent() (simple getter) compared to DepotChest::getParent() (one if statement).

The crucial thing you are missing here is informing the spectators about this name change, take a look at Monster::convinceCreature. The part starting on line 1950 in master branch:

C++:
    //Notify surrounding about the change
    SpectatorHashSet spectators;
    g_game.map.getSpectators(spectators, getPosition(), true);
    g_game.map.getSpectators(spectators, creature->getPosition(), true);
    for (Creature* spectator : spectators) {
        spectator->onCreatureConvinced(creature, this);
    }

You would need something similar, though I am not even sure if TFS has any means to inform client application about the name change of the creature after is has been "convinced".
 
Can any good soul update this function for the last version of TFS?
I noticed some changes in the files the thread changes. I'm afraid to do shit since I'm a potato in C++.

Also:
You should probably move your updated methods from monsters.h to monsters.cpp, as they are no longer single getters (get* methods). Just have a look how it's done in case of DepotChest class and DepotChest::getRealParent() (simple getter) compared to DepotChest::getParent() (one if statement).

The crucial thing you are missing here is informing the spectators about this name change, take a look at Monster::convinceCreature. The part starting on line 1950 in master branch:

C++:
    //Notify surrounding about the change
    SpectatorHashSet spectators;
    g_game.map.getSpectators(spectators, getPosition(), true);
    g_game.map.getSpectators(spectators, creature->getPosition(), true);
    for (Creature* spectator : spectators) {
        spectator->onCreatureConvinced(creature, this);
    }

You would need something similar, though I am not even sure if TFS has any means to inform client application about the name change of the creature after is has been "convinced".

What about using OTClient? It sounds easier.
 
Can any good soul update this function for the last version of TFS?
I noticed some changes in the files the thread changes. I'm afraid to do shit since I'm a potato in C++.

Also:


What about using OTClient? It sounds easier.
it work on the last tfs ... but you need do some stuff to it work properly... as exemple:
Code:
local monster = Game.createMonster('rat', Position(farawayofanyplayer), true, true)
if monster then
    monster:setName('Cave Rat', 'a cave rat')
end
 
it work on the last tfs ... but you need do some stuff to it work properly... as exemple:
Code:
local monster = Game.createMonster('rat', Position(farawayofanyplayer), true, true)
if monster then
    monster:setName('Cave Rat', 'a cave rat')
end
Oh yeah. I do know you need to change it far away to update the name in the client. Thing is, I found different lines in the last source.

As an example, in the monster.h, the thread asks you to find this code:
Code:
const std::string& getName() const final {
    return mType->name;
}

But in the last version of the source, it is written as:
Code:
const std::string& getName() const override {
    return mType->name;
}

So I'm getting kinda confused here. ;_;
 
Oh yeah. I do know you need to change it far away to update the name in the client. Thing is, I found different lines in the last source.

As an example, in the monster.h, the thread asks you to find this code:
Code:
const std::string& getName() const final {
    return mType->name;
}

But in the last version of the source, it is written as:
Code:
const std::string& getName() const override {
    return mType->name;
}

So I'm getting kinda confused here. ;_;
use the lastest edit and test, if not work you can use the first one..
 
Someone can help in the update name for spectators ?
Bring It back haha
 
Back
Top