Lua Function creature:canChangeDirection(bool)

Joined
Apr 26, 2017
Messages
295
Best answers
9
Likes
21
#1
Explanation: You can make any creature stop changing directions, usually via CTRL+ARROW KEY.

My first C++ function, tested only with players and TFS 1.0

1. In game.cpp find:

C++:
bool Game::internalCreatureTurn(Creature* creature, Direction dir) {
    if (creature->getDirection() == dir) {
        return false;
    }
and replace with:

C++:
bool Game::internalCreatureTurn(Creature* creature, Direction dir) {
    if (creature->getDirection() == dir || creature->canChangeDir()) {
        return false;
    }
2. in creature.h after:

C++:
virtual bool isInGhostMode() const {
    return false;
}
add the following:

C++:
bool directionChange;
bool canChangeDir() const {
    return directionChange;
}
void canChangeDirection(bool directionChange) {
    this->directionChange = directionChange;
}
3. In creature.cpp after:

C++:
hiddenHealth = false;
add the following:

C++:
directionChange = false;
4. In luascript.h after:

C++:
static int32_t luaCreatureSetNoMove(lua_State* L);
add the following:

C++:
static int32_t luaCreatureCanChangeDirection(lua_State* L);
5. In luascript.cpp after:

C++:
registerMethod("Creature", "getPathTo", LuaScriptInterface::luaCreatureGetPathTo);
add the following:

C++:
registerMethod("Creature", "canChangeDirection", LuaScriptInterface::luaCreatureCanChangeDirection);
after:

C++:
int32_t LuaScriptInterface::luaCreatureGetPathTo(lua_State* L)
{
    [......]
}
add the following:

C++:
int32_t LuaScriptInterface::luaCreatureCanChangeDirection(lua_State* L)
{
    // creature:canChangeDirection(bool)
    Creature* creature = getUserdata<Creature>(L, 1);
    if (!creature) {
        lua_pushnil(L);
        return 1;
    }
   
    bool directionChange = getBoolean(L, 2);

    creature->canChangeDirection(directionChange);

    pushBoolean(L, true);
    return 1;
}
Most things learned from this thread xd: Lua Function - [TFS 1.2] creature:setNoMove(bool) & creature:canMove()
 

gudan garam

Active Member
Joined
Feb 13, 2011
Messages
276
Best answers
12
Likes
84
Location
Tibialand
#2
Shouldn't this:
C++:
bool Game::internalCreatureTurn(Creature* creature, Direction dir) {
    if (creature->getDirection() == dir || creature->canChangeDir()) {
        return false;
    }
be:

C++:
bool Game::internalCreatureTurn(Creature* creature, Direction dir) {
    if (creature->getDirection() == dir || !creature->canChangeDir()) {
        return false;
    }
?
 
Joined
Apr 26, 2017
Messages
295
Best answers
9
Likes
21
#3
Shouldn't this:
C++:
bool Game::internalCreatureTurn(Creature* creature, Direction dir) {
    if (creature->getDirection() == dir || creature->canChangeDir()) {
        return false;
    }
be:

C++:
bool Game::internalCreatureTurn(Creature* creature, Direction dir) {
    if (creature->getDirection() == dir || !creature->canChangeDir()) {
        return false;
    }
?
If you only change this part, it will freeze your character when you first login

and currently you use it like that creature:canChangeDirection(true) - will freeze your character which is wrong because 1 is not equal to 0. Well the functionality is there, but if you want to fix this issue then change the previous code from first post to this:

1. game.cpp

C++:
bool Game::internalCreatureTurn(Creature* creature, Direction dir) {
    if (creature->getDirection() == dir || !creature->canChangeDir()) {
        return false;
    }
2. creature.cpp

C++:
directionChange = true;
 
Last edited by a moderator:
Top