• 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 TFS 1.0 player:registerEvent("PlayerDeath")

putamerda

New Member
Joined
Jan 2, 2012
Messages
73
Reaction score
0
Hello im just trying to add an creaturescript for when the player dies:
Heres my login.lua
Code:
function onLogin(cid)
        local player = Player(cid)

        local loginStr = "Welcome to " .. configManager.getString(configKeys.SERVER_NAME) .. "!"
        if player:getLastLoginSaved() <= 0 then
                loginStr = loginStr .. " Please choose your outfit."
                player:sendOutfitWindow()
        else
                if loginStr ~= "" then
                        player:sendTextMessage(MESSAGE_STATUS_DEFAULT, loginStr)
                end

                loginStr = string.format("Your last visit was on %s.", os.date("%a %b %d %X %Y", player:getLastLoginSaved()))
        end
        player:registerEvent("PlayerDeath")
        player:sendTextMessage(MESSAGE_STATUS_DEFAULT, loginStr)

       
        return true
end

My creaturescripts.xml:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<creaturescripts>
        <event type="login" name="PlayerLogin" script="login.lua"/>
        <event type="login" name="FirstItems" script="firstitems.lua"/>
        <event type="death" name="PlayerDeath" script="playerdeath.lua"/>
        <event type="extendedopcode" name="ExtendedOpcode" script="extendedopcode.lua"/>
</creaturescripts>

And finally my playerdeath.lua
Code:
local deathListEnabled = true
local maxDeathRecords = 5

function onDeath(cid, corpse, killer, mostDamage, unjustified, mostDamage_unjustified)
        print("TESTE MESSAGE YOUREDEAD")
        doPlayerSendTextMessage(cid, MESSAGE_EVENT_ADVANCE, "You are dead.")
        if not deathListEnabled then
                return
        end

        local byPlayer = 0
        if killer == 0 then
                killerName = "field item"
        else
                if isPlayer(killer) then
                        byPlayer = 1
                else
                        local master = getCreatureMaster(killer)
                        if master ~= killer and isPlayer(master) then
                                killer = master
                                byPlayer = 1
                        end
                end
                killerName = getCreatureName(killer)
        end

        local byPlayerMostDamage = 0
        if mostDamage == 0 then
                mostDamageName = "field item"
        else
                if isPlayer(mostDamage) == TRUE then
                        byPlayerMostDamage = 1
                else
                        local master = getCreatureMaster(mostDamage)
                        if master ~= mostDamage and isPlayer(master) then
                                mostDamage = master
                                byPlayerMostDamage = 1
                        end
                end
                mostDamageName = getCreatureName(mostDamage)
        end

        db.query("INSERT INTO `player_deaths` (`player_id`, `time`, `level`, `killed_by`, `is_player`, `mostdamage_by`, `mostdamage_is_player`, `unjustified`, `mostdamage_unjustified`) VALUES (" .. getPlayerGUID(cid) .. ", " .. os.time() .. ", " .. getPlayerLevel(cid) .. ", " .. db.escapeString(killerName) .. ", " .. byPlayer .. ", " .. db.escapeString(mostDamageName) .. ", " .. byPlayerMostDamage .. ", " .. unjustified .. ", " .. mostDamage_unjustified .. ")")
        local resultId = db.storeQuery("SELECT `player_id` FROM `player_deaths` WHERE `player_id` = " .. getPlayerGUID(cid))

        local deathRecords = 0
        local tmpResultId = resultId
        while tmpResultId ~= false do
                tmpResultId = result.next(resultId)
                deathRecords = deathRecords + 1
        end

        if resultId ~= false then
                result.free(resultId)
        end

        while deathRecords > maxDeathRecords do
                db.query("DELETE FROM `player_deaths` WHERE `player_id` = " .. getPlayerGUID(cid) .. " ORDER BY `time` LIMIT 1")
                deathRecords = deathRecords - 1
        end

        if byPlayer == 1 then
                local targetGuild = getPlayerGuildId(cid)
                if targetGuild ~= 0 then
                        local killerGuild = getPlayerGuildId(killer)
                        if killerGuild ~= 0 and targetGuild ~= killerGuild and isInWar(cid, killer) == TRUE then
                                local warId = false
                                resultId = db.storeQuery("SELECT `id` FROM `guild_wars` WHERE `status` = 1 AND ((`guild1` = " .. killerGuild .. " AND `guild2` = " .. targetGuild .. ") OR (`guild1` = " .. targetGuild .. " AND `guild2` = " .. killerGuild .. "))")
                                if resultId ~= false then
                                        warId = result.getDataInt(resultId, "id")
                                        result.free(resultId)
                                end

                                if warId ~= false then
                                        db.query("INSERT INTO `guildwar_kills` (`killer`, `target`, `killerguild`, `targetguild`, `time`, `warid`) VALUES (" .. db.escapeString(getCreatureName(killer)) .. ", " .. db.escapeString(getCreatureName(cid)) .. ", " .. killerGuild .. ", " .. targetGuild .. ", " .. os.time() .. ", " .. warId .. ")")
                                end
                        end
                end
        end
end





When the players dies, nothing happens, not even the print("TESTE MESSAGE YOUREDEAD")
No console errors

What is happening?
 
Nope, just the basic, this playerdeath.lua is from theforgotten server original source (GitHub)
I was checking creatureevent.cpp: https://github.com/otland/forgotten...6152d530ff864bfc14fcf15/src/creatureevent.cpp

Code:
bool CreatureEvents::registerEvent(Event* event, const pugi::xml_node&)
{
CreatureEvent* creatureEvent = dynamic_cast<CreatureEvent*>(event);
if (!creatureEvent) {
return false;
}

if (creatureEvent->getEventType() == CREATURE_EVENT_NONE) {
std::cout << "Error: [CreatureEvents::registerEvent] Trying to register event without type!" << std::endl;
return false;
}

CreatureEvent* oldEvent = getEventByName(creatureEvent->getName(), false);
if (oldEvent) {
//if there was an event with the same that is not loaded
//(happens when realoading), it is reused
if (!oldEvent->isLoaded() && oldEvent->getEventType() == creatureEvent->getEventType()) {
oldEvent->copyEvent(creatureEvent);
}

return false;
} else {
//if not, register it normally
m_creatureEvents[creatureEvent->getName()] = creatureEvent;
return true;
}
}

registerEvent returns false in some cases... someone can help me?
 
maybe:
registerCreatureEvent(cid, "PlayerDeath")

Here's the OO code from compat.lua:
Code:
function registerCreatureEvent(cid, name) 
    local c = Creature(cid) 
    return c ~= nil and c:registerEvent(name) or false 
end
You might be getting a weird side-effect from not using a Creature object (weird because Player is a subclass, so if this works there's probably a bug in the Lua<->C++ interface)
 
Back
Top