1. #1
    The Patient
    15+ Year Old Account
    Join Date
    Apr 2008
    Location
    United Kingdom
    Posts
    319

    LUA - Arrays detection (on a variable returned as string)

    Hello,

    I'm stuck with something I can't seem to resolve after trying to figure it out for countless hours.

    Code:
    local _SPD = CreateFrame("Frame");
    _SPD:RegisterEvent("COMBAT_LOG_EVENT_UNFILTERED");
    _SPD:SetScript("OnEvent", function(self, event, ...)
    	
    	local timestamp, typea, 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, ...)
    				
    				if type(spellName) == "table" then
    					print("It exists")
    					else
    					print(type(spellName))
    					spellName = {}
    					print(type(spellName))
    					print("Created"
    					end
    					
    				end 
    			end 
    		end
    	end
    end);
    Basically my aim is to create a table on each different spell I cast, for example if I use Riptide it should create a table Riptide = {}, the table does create when I cast Riptide but upon recast it just creates the table again. I can't create the table outside the local scope either as I don't want to manually create tables but rather have the AddOn 'learn' them through casting spells.

    A print(spellName) before and after spellName = {} shows that before the creation it returns the spell name, Riptide, and after the creation the table number (which changes each time Riptide is cast). I believe this has to do with the the string being converted into a table but on the next run being a string again which is why it creates another table instead of detecting the old table on the same name.

    I'm at a loss on how to get this working, thanks in advance.
    Last edited by suprep; 2013-01-31 at 02:09 AM. Reason: Forgot to add a line of code.

  2. #2
    Quote Originally Posted by suprep View Post
    Hello,

    I'm stuck with something I can't seem to resolve after trying to figure it out for countless hours.

    Code:
    local _SPD = CreateFrame("Frame");
    _SPD:RegisterEvent("COMBAT_LOG_EVENT_UNFILTERED");
    _SPD:SetScript("OnEvent", function(self, event, ...)
    	
    	local timestamp, typea, 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
    				
    				if type(spellName) == "table" then
    				print("It exists")
    				else
    				print(type(spellName))
    				spellName = {}
    				print(type(spellName))
    				print("Created")
    				end
    
    				end 
    			end 
    		end
    	end
    end);
    Basically my aim is to create a table on each different spell I cast, for example if I use Riptide it should create a table Riptide = {}, the table does create when I cast Riptide but upon recast it just creates the table again. I can't create the table outside the local scope either as I don't want to manually create tables but rather have the AddOn 'learn' them through casting spells.

    A print(spellName) before and after spellName = {} shows that before the creation it returns the spell name, Riptide, and after the creation the table number (which changes each time Riptide is cast). I believe this has to do with the the string being converted into a table but on the next run being a string again which is why it creates another table instead of detecting the old table on the same name.

    I'm at a loss on how to get this working, thanks in advance.

    local spellsCasted = {}
    at the top of your code, outside of any functions

    then everytime you want to create a table

    if not spellsCasted[spellName] then spellsCasted[spellName] = {} end
    then for example to cycle through all the spells you've created tables for

    for spellName, spellTable in pairs(spellsCasted) do
    print(spellName, spellTable)
    end
    And to take it one step further to add a variable lets say the time at which you last casted the spell you would do this:

    spellsCasted["Riptide"].lastCasted = GetTime()
    Last edited by Elv; 2013-01-31 at 02:13 AM.

  3. #3
    The Patient
    15+ Year Old Account
    Join Date
    Apr 2008
    Location
    United Kingdom
    Posts
    319
    You, sir, are a genius!

    Thanks a lot, that solved the issue.

  4. #4
    FYI your code is seriously horrid. Whilst that usually wouldn't matter (most events fire infrequently), you need to consider that the combat log event fires so often that it can overtake polling (OnUpdate), so performance needs to be called into question.

    Here is a simple approach which assumes the only event you want to register is the combat log:
    Code:
    local myTable = {}
    local _SPD = CreateFrame("Frame")
    _SPD:RegisterEvent("COMBAT_LOG_EVENT_UNFILTERED")
    _SPD:SetScript("OnEvent", function(_, _, _, event, _, _, sourceName, _, _, _, _, _, _, _, spellName)
    	if event == "SPELL_AURA_APPLIED" and UnitIsUnit(sourceName, "player") then
    		if not myTable[spellName] then
    			print("It exists")
    		else
    			myTable[spellName] = {}
    			print("Created")
    		end
    	end 
    end)

    Here is an approach that assumes you want to register multiple events to the given frame:
    Code:
    local myTable = {}
    local _SPD = CreateFrame("Frame")
    _SPD:RegisterEvent("COMBAT_LOG_EVENT_UNFILTERED")
    _SPD:SetScript("OnEvent", function(frame, event, ...)
    	frame[event](frame, event, ...)
    end)
    
    function _SPD:COMBAT_LOG_EVENT_UNFILTERED(_, _, _, event, _, _, sourceName, _, _, _, _, _, _, _, spellName)
    	if event == "SPELL_AURA_APPLIED" and UnitIsUnit(sourceName, "player") then
    		if not myTable[spellName] then
    			print("It exists")
    		else
    			myTable[spellName] = {}
    			print("Created")
    		end
    	end 
    end
    https://github.com/funkydude - https://github.com/BigWigsMods
    Author of BadBoy, BasicChatMods, BigWigs, BFAInvasionTimer, SexyMap, and more...

  5. #5
    The Patient
    15+ Year Old Account
    Join Date
    Apr 2008
    Location
    United Kingdom
    Posts
    319
    Ah, I wasn't aware of that as I've started picking up LUA last week, so I'm quite inexperienced. Thanks!

    PS: I assume you meant if myTable[spellName] then instead of if not myTable[spellName] then

  6. #6
    Quote Originally Posted by suprep View Post
    PS: I assume you meant if myTable[spellName] then instead of if not myTable[spellName] then
    Woops! Indeed
    https://github.com/funkydude - https://github.com/BigWigsMods
    Author of BadBoy, BasicChatMods, BigWigs, BFAInvasionTimer, SexyMap, and more...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •