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

Lua Function creature:canChangeDirection(bool)

mackerel

Well-Known Member
Joined
Apr 26, 2017
Messages
398
Solutions
18
Reaction score
72
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()
 
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;
    }

?
 
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:
Back
Top