• 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!
  • 2026 staff recruitment is open! Check it out and consider applying!

Lua Check for Registered Event?

mackerel

Well-Known Member
Joined
Apr 26, 2017
Messages
398
Solutions
18
Reaction score
74
Just wondering if there is a way to check whether a player has certain event registered, example:

LUA:
    if Player(cid):registerEvent() == "quest2" then
        print("hello")
    end

I only know these two:

LUA:
Player(cid):registerEvent(name)
Player(cid):unregisterEvent(name)


TFS 1.0

thanks
 
Solution
Maybe this will help you
for example:
LUA:
-- save in global.lua
PLAYER_REGISTER_EVENTS = {}

function Player.getRegisterEvent(self, name)
if not self:isPlayer() then
return false
end
if PLAYER_REGISTER_EVENTS[self:getId()] == nil then
PLAYER_REGISTER_EVENTS[self:getId()] = {}
end
local event = PLAYER_REGISTER_EVENTS[self:getId()][name] or false
return event
end

LUA:
-- register event example
local nameEvent = 'kill_boss'
local playerId = player:getId()
player:registerEvent(nameEvent)
PLAYER_REGISTER_EVENTS[playerId][nameEvent] = true

LUA:
-- use function getRegisterEvent example
function onUse(player, item, fromPos, target, toPos, isHotkey)
if player:getRegisterEvent('kill_boss') then
player:sendTextMessage(MESSAGE_INFO_DESCR, 'yes event...
Just wondering if there is a way to check whether a player has certain event registered, example:

LUA:
    if Player(cid):registerEvent() == "quest2" then
        print("hello")
    end

I only know these two:

LUA:
Player(cid):registerEvent(name)
Player(cid):unregisterEvent(name)


TFS 1.0

thanks
Don't think it works that way. I would suggest just setting a player storage when the event is registered. That way you can just check that.
 
Maybe this will help you
for example:
LUA:
-- save in global.lua
PLAYER_REGISTER_EVENTS = {}

function Player.getRegisterEvent(self, name)
if not self:isPlayer() then
return false
end
if PLAYER_REGISTER_EVENTS[self:getId()] == nil then
PLAYER_REGISTER_EVENTS[self:getId()] = {}
end
local event = PLAYER_REGISTER_EVENTS[self:getId()][name] or false
return event
end

LUA:
-- register event example
local nameEvent = 'kill_boss'
local playerId = player:getId()
player:registerEvent(nameEvent)
PLAYER_REGISTER_EVENTS[playerId][nameEvent] = true

LUA:
-- use function getRegisterEvent example
function onUse(player, item, fromPos, target, toPos, isHotkey)
if player:getRegisterEvent('kill_boss') then
player:sendTextMessage(MESSAGE_INFO_DESCR, 'yes event registered')
end
return true
end
I do not know why you want that function, it's not really necessary
Greetings little friend
 
Last edited:
Solution
Maybe this will help you
for example:
LUA:
-- save in global.lua
PLAYER_REGISTER_EVENTS = {}

function Player.getRegisterEvent(self, name)
if not self:isPlayer() then
return false
end
local event = PLAYER_REGISTER_EVENTS[self:getId()][name] or false
return event
end

LUA:
-- register event example
local nameEvent = 'kill_boss'
local playerId = player:getId()
player:registerEvent(nameEvent)
PLAYER_REGISTER_EVENTS[playerId][nameEvent] = true

LUA:
-- use function getRegisterEvent example
function onUse(player, item, fromPos, target, toPos, isHotkey)
if player:getRegisterEvent('kill_boss') then
player:sendTextMessage(MESSAGE_INFO_DESCR, 'yes event registered')
end
return true
end
I do not know why you want that function, it's not really necessary
Greetings little friend
you could be potentially indexing a nil value if there's no value for PLAYER_REGISTER_EVENTS[self:getId()]
 
you could be potentially indexing a nil value if there's no value for PLAYER_REGISTER_EVENTS[self:getId()]
Of course I know if there is no value that gives us a 'nil'
return nill <- -> no, exist value
return true <- -> yes, existe value
If you know how to use it well, everything works perfect, just like the stamina works
 
Of course I know if there is no value that gives us a 'nil'
return nill <- -> no, exist value
return true <- -> yes, existe value
If you know how to use it well, everything works perfect, just like the stamina works
yes but you would get an error if there were no events registered under the player
if PLAYER_REGISTER_EVENTS[self:getId()] didn't exist and you tried to assign a new index inside that table you'd be indexing a nil value with PLAYER_REGISTER_EVENTS[self:getId()][nameEvent]
any time you use the function before registering the player to the table you would get an error
 
yes but you would get an error if there were no events registered under the player
if PLAYER_REGISTER_EVENTS[self:getId()] didn't exist and you tried to assign a new index inside that table you'd be indexing a nil value with PLAYER_REGISTER_EVENTS[self:getId()][nameEvent]
any time you use the function before registering the player to the table you would get an error
If the table already exists, I can modify and create variables within it, regardless of whether the variables exist or not.
example:

Global.lua
If nextUseStamina == nil then
NextUseStamina = {}
End

Then I can already create variables within that table, anywhere because it is a global variable

Maybe you are thinking about the chances of it failing.
I say that if it works, because on my server I use several global tables in the same way and it works perfectly

Anyway thank you very much for being aware of my code and for analyzing it
 
If the table already exists, I can modify and create variables within it, regardless of whether the variables exist or not.
example:

Global.lua
If nextUseStamina == nil then
NextUseStamina = {}
End

Then I can already create variables within that table, anywhere because it is a global variable

Maybe you are thinking about the chances of it failing.
I say that if it works, because on my server I use several global tables in the same way and it works perfectly

Anyway thank you very much for being aware of my code and for analyzing it
you're not understanding what i'm saying.
if you use player:getRegisterEvent(name) BEFORE you create a new table to save all event names with player id, the index is nil
you cannot give a value to a nil index in a table
for example:
LUA:
local t = {}
t[nil][5] = 10 -- attempt to index a nil value
t[1][6] = 40 -- t[1] is not a table, the [] operator to insert 6 will not work, attempt to index field ? a nil value
in this case, if the player id was 1 in the 2nd case, you would get attempt to index field ?, because you haven't set PLAYER_REGISTER_EVENTS[self:getId()] = {}
it would work the same way as your global tables if you do PLAYER_REGISTER_EVENTS[self:getId()] = {} and THEN insert names into there, but if the value is nil in the first place you'd be indexing an unknown value and attempting to insert a name into nothing
to fix this you would need to make sure to insert a new table whenever the cid isn't a valid index yet
LUA:
PLAYER_REGISTER_EVENTS = {}

function Player.getRegisterEvent(self, name)
    local cid = self:getId()
    if not PLAYER_REGISTER_EVENTS[cid] then
        PLAYER_REGISTER_EVENTS[cid] = {}
    end
    return PLAYER_REGISTER_EVENTS[cid][name]
end
you should probably also do this with the creature class instead of player, since the registerEvent and unregisterEvent both belong to the creature metatable, not player.
 

Similar threads

Back
Top