• 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+ When does execute event actually end?

Exeus

Advanced OT User
Joined
Oct 8, 2012
Messages
859
Reaction score
198
Hi!

I'm using TFS 1.2 and I'm kind of changing TFS' death but I found some part that I struggle to understand

Here's the code:


Code:
for (CreatureEvent* deathEvent : getCreatureEvents(CREATURE_EVENT_DEATH)) {
    printf("started executin onDeath script \n");
    deathEvent->executeOnDeath(this, corpse, lastHitCreature, mostDamageCreature, lastHitUnjustified, mostDamageUnjustified);
    printf("ended executin onDeath script \n");
}

also playerdeath.lua which prints

exiting playerdeath.lua

Code:
function onDeath(player, corpse, killer, mostDamageKiller, unjustified, mostDamageUnjustified)
    (...)
    print("exiting playerdeath.lua");
    return false;
end


Now I'm struggling to understand one thing

I'm killing character A and everything so far prints fine

started executin onDeath script
exiting playerdeath.lua

but now nothing happens until I logout from character

so, after logout message appears:

ended executin onDeath script


but why? what's blocking it?

thanks in advance!
 
Solution
This literally doesn't make sense, if it was blocking execution your server would 100% be freezing as well. My suspicion is that the output buffer isn't being flushed because of you using printf for some reason, use std::cout instead and try again.
I'm unable to reproduce this, even if prepareDeath runs first it's unrelated to this specific loop, this loop should not be blocking (if it is, your entire game server would freeze as well).

When I do the same (adding a print before & after executeOnDeath) I get this:
Code:
begin death
executing onDeath <- my script
exit death
. has logged out.
It doesn't block exit death print, it does it immediately before i even log out & drops the corpse as well.
 
Sorry that code is full of "ass debugging prints" and looks shitty but I've been trying to remove as much as I could just in order to find what's wrong

I also after posting this thread moved a little up that "executeOnDeath", but It shouldnt make difference

I made onDeath virtual in creature.h -> virtual void onDeath();
+ added void onDeath() final; to Player.h

Take a look at console on video:

ending playerdeath.lua appears and then the rest, so ended executin onDeath script appears after relog, but should instantly?


Code:
D has logged out.
D has logged in.

Player onDeath
dropping corpse
weird if in dropCorpse 1
creature dropCorpse
true
started executin onDeath script
> Broadcasted message: "C[44] just died to: D [69], D".
ending playerdeath.lua

// RELOG //
________________________________
// RELOG //

ended executin onDeath script
before death entry
player death

C has logged out.
C has logged in.


Code execution from the top to bottom

Code:
void Player::onDeath()
{
    printf("Player onDeath\n");
    CalculateSomeBullshitAndDropCorpse();
    Creature* lastHitCreature = g_game.getCreatureByID(lastHitCreatureId);

    //scripting event - onDeath
    for (CreatureEvent* deathEvent : getCreatureEvents(CREATURE_EVENT_DEATH))
    {
        printf("started executin onDeath script \n");
        deathEvent->executeOnDeath(this, nullptr, lastHitCreature, 0, 0, 0);
        printf("ended executin onDeath script \n");
    }

    printf("before death entry\n");
    death(lastHitCreature);
}

Code:
void Player::CalculateSomeBullshitAndDropCorpse()
{
    Creature* lastHitCreature = g_game.getCreatureByID(lastHitCreatureId);
    printf("dropping corpse\n");
    bool droppedCorpse = dropCorpse(lastHitCreature, lastHitCreature, 0, 0);
    printf(droppedCorpse == true ? "true\n" : "false\n");
}


Code:
bool Player::dropCorpse(Creature* lastHitCreature, Creature* mostDamageCreature, bool lastHitUnjustified, bool mostDamageUnjustified)
{
    if (getZone() != ZONE_PVP || !Player::lastHitIsPlayer(lastHitCreature))
    {
        printf("weird if in dropCorpse 1\n");
        return Creature::dropCorpse(lastHitCreature, mostDamageCreature, lastHitUnjustified, mostDamageUnjustified);
    }

    printf("weird else in dropCorpse 1\n");
    setDropLoot(true);
    return false;
}


Code:
bool Creature::dropCorpse(Creature* lastHitCreature, Creature* mostDamageCreature, bool lastHitUnjustified, bool mostDamageUnjustified)
{
    printf("creature dropCorpse\n");
    return true;
}

Code:
function onDeath(player, corpse, killer, mostDamageKiller, unjustified, mostDamageUnjustified)
    (...)
    print("ending playerdeath.lua");
    return true;
end
 
Last edited:
This literally doesn't make sense, if it was blocking execution your server would 100% be freezing as well. My suspicion is that the output buffer isn't being flushed because of you using printf for some reason, use std::cout instead and try again.
 
Solution
Unfortunely changing those printf to std::cout << didnt change anything :/

I feel as if there was some wait for lua to return something?

What actually happens when lua script is being executed? C++ code just goes ahead?

Because if yes, then it'd mean that there's no scenerio where C++ code needs to be done after lua did something e.g drop loot(rs)?

But on the other hand there are database calls in lua, so it could be blocking whole engine, so also not nicest thing

is there some await thing?


Post automatically merged:

@Delusion

HOLY FUCK I FOUND IT XD!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Background:

My friend wanted to be able to swap OTS map "on fly" - he uploaded map.otbm via www site form and it replaced OTS map and restarted it

Because we're lazy asses and wanted to get our "Development Infrastructure™" ASAP - web app was just replacing file and killing tfs process, as simple as that.

Then, there was console session on tmux that had running THIS SCRIPT

Code:
while true;
do
    ./tfs > logs.txt | tail -f logs.txt
    sleep 1;
done

So, when tfs was killed (and its map replaced with new one) - it started it up again, instantly.

it worked fine and allowed him to change map without any access to server

and for some reason it casued this problem <WTF>

I'm now switching between using this script and normal ./tfs and here's this difference

Using loop:

1577924413384.png

Using just ./tfs:

the difference is subtle - no logout needed

1577924493954.png
 
Last edited:
Unfortunely changing those printf to std::cout << didnt change anything :/

I feel as if there was some wait for lua to return something?

What actually happens when lua script is being executed? C++ code just goes ahead?

Because if yes, then it'd mean that there's no scenerio where C++ code needs to be done after lua did something e.g drop loot(rs)?

But on the other hand there are database calls in lua, so it could be blocking whole engine, so also not nicest thing

is there some await thing?
No, database tasks are run on a separate thread, Lua calls are not (why do you think while true loops freeze your server?). There's no way you'd be able to move around in-game if it was waiting for executeOnDeath to return. That's why this makes absolutely no sense unless it's getting called elsewhere somehow with some other edit you've made.

Edit: Ah, so it was an output buffer issue, just not from sources.
 
Back
Top