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

TFS 1.X+ Player Event OnStepTile

Itutorial

Excellent OT User
Joined
Dec 23, 2014
Messages
2,307
Solutions
68
Reaction score
982
I am getting this error: PANIC: unprotected error in call to Lua API (attempt to index a nil value)
Im sure its something simple..

Events.cpp
C++:
} else if (methodName == "onStepTile") {
                info.playerOnStepTile = event;

C++:
bool Events::eventPlayerOnStepTile(Player* player, const Position& fromPosition, const Position& toPosition)
{
    // Player:onStepTile(fromTile, toTile)
    if (info.playerOnStepTile == -1) {
        return true;
    }

    if (!scriptInterface.reserveScriptEnv()) {
        std::cout << "[Error - Events::eventPlayerOnStepTile] Call stack overflow" << std::endl;
        return false;
    }

    ScriptEnvironment* env = scriptInterface.getScriptEnv();
    env->setScriptId(info.playerOnStepTile, &scriptInterface);

    lua_State* L = scriptInterface.getLuaState();
    scriptInterface.pushFunction(info.playerOnStepTile);

    LuaScriptInterface::pushUserdata<Player>(L, player);
    LuaScriptInterface::setMetatable(L, -1, "Player");

    LuaScriptInterface::pushPosition(L, fromPosition);
    LuaScriptInterface::pushPosition(L, toPosition);

    return scriptInterface.callFunction(3);
}

Events.h
C++:
int32_t playerOnStepTile = -1;

C++:
bool eventPlayerOnStepTile(Player* player, const Position& fromPosition, const Position& toPosition);

Player.cpp
C++:
void Player::onWalk(Direction& dir)
{
    const Position& fromPos = getPosition();
    const Position& toPos = getNextPosition(dir, fromPos);
  
    if (!g_events->eventPlayerOnStepTile(this, fromPos, toPos)) {
        return;
    }

    Creature::onWalk(dir);
    setNextActionTask(nullptr);
    setNextAction(OTSYS_TIME() + getStepDuration(dir));
}


events.lua
Lua:
function Player:onStepTile(fromPosition, toPosition)
    if hasEventCallback(EVENT_CALLBACK_ONSTEPTILE) then
        return EventCallback(EVENT_CALLBACK_ONSTEPTILE, self, fromPosition, toPosition)
    end
    return true
end

events.xml
XML:
<event class="Player" method="onStepTile" enabled="1" />
 
It appears he beat me to it, thought of this last night when I was going to sleep 😄

you are only missing it from the lib that makes the event callbacks in data/scripts/lib/event_callbacks.lua

can see easy in his example if you click on that file.

ec.onStepTile = {}

is what you are missing if you don't feel like leaving the page :D
 
Why does this file exist. I added onStepTile the same as the other callbacks are in it and nothing changed. I am also using TFS 1.4.1


Need register on event_callbacks
See my impl
C++:
bool Events::eventPlayerOnStepTile(Player* player, const Position& fromPosition, const Position& toTile)
Need to change toTile to toPosition

C++:
// Player:onStepTile(fromPosition, toPosition)

@Ramon Bernardo You should change onStepTile to onWalk in the main repo. Also add everything under onTurn to keep some structure.


and change player.cpp
C++:
const Position& fromPos = getPosition();
    const Position& toPos = getNextPosition(dir, fromPos);
    if (!g_events->eventPlayerOnStepTile(this, fromPos, toPos)) {
        sendCancelWalk();
        return;
    }
 
Last edited:
Back
Top