Lua - check if array exists
I am trying to figure out if any array exists via an if statement like
if array{} == nil then array = {} else print("it exists") end
The above doesn't work and I can't check if it exists, basically I create an AddOn that scans the log for a specific event and if it is correct it returns the startup_name. I want to create an array with this name spellName, however spellName = {} doesn't work as it just creates a new array (not update the existing one).
local _SPD = CreateFrame("Frame");
_SPD:RegisterEvent("COMBAT_LOG_EVENT_UNFILTERED");
_SPD:SetScript("OnEvent", function(self, event, ...)
local timestamp, type, sourceName = select(1, ...), select(2, ...), select(5, ...)
if (event == "COMBAT_LOG_EVENT_UNFILTERED") then
if select(2,...) == "SPELL_AURA_APPLIED" then
if select(5,...) == UnitName("player") then
local spellID, spellName = select(12, ...), select(13, ...)
spellName = {
sourceName = {
}
}
table.insert(spellName["sourceName"], {id = spellID, stamp = timestamp })
for k,v in pairs ( spellName["sourceName"] ) do
print (k.. ": " ..v["id"].. " at " ..v["stamp"])
end
end
end
end
end);
Basically it's just re-creating the table every time a certain aura is imposed on me (which is the expected behavior)
I banged my head, but I have no idea how to check if spellName (and sourceName) exists and if that doesn't create them again, since in this case the variable already exists because it returns the value to me so I can't check if they are null, as there won't be, I need to somehow check if the table exists for these values ββand if they are not generated.
Thanks in advance.
source to share