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.

+3


source to share


2 answers


Your ad for checking the table is incorrect. Use it like this:



if type(array) == "table" then
  print("it exists")
else
  array = {}
end

      

+2


source


Try the following:



local spellID, spellName = select(12, ...), select(13, ...)
spellName = spellName or {}
spellName.sourceName = spellName.sourceName or {}
table.insert(spellName.sourceName, {id = spellID, stamp = timestamp })

      

+1


source







All Articles