Thread Tools Display Modes
07-31-13, 01:53 AM   #1
Niketa
A Wyrmkin Dreamwalker
 
Niketa's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2013
Posts: 54
Saving values to different tables in a loop.

I'm trying to go through each mount a player has and add that to a table "flying", "ground" or "aquatic" within the table "mounts". With the code I currently have it, it doesn't add them to a table.

However, if I take out the bit of code to add to ground or aquatic (the elseifs) and have it just add to the flying table it works just fine (and I don't think it's just the flying in particular, but rather I can only add to one sub-table rather than multiple ones).

Is there an error in my code or is it just not possible with this approach?


Lua Code:
  1. local mounts = {}
  2.     mounts.flying = {}
  3.     mounts.ground = {}
  4.     mounts.aquatic = {}
  5.  
  6. local amount = GetNumCompanions("mount")
  7.  
  8. local f, g, a = 1
  9.  
  10. for i = 1, amount do
  11.     local creatureID, creatureName, creatureSpellID, icon, issummoned, mountType = GetCompanionInfo("mount",i)
  12.    
  13.     if mountType == 7 or mountType == 15 or mountType == 23 or mountType == 31 then
  14.         mounts.flying[f] = {creatureID, creatureName, creatureSpellID, icon, issummoned, mountType}
  15.         --print(mounts.flying[f][2])
  16.         f = f + 1
  17.     elseif mountType == 29 then
  18.         mounts.ground[g] = {creatureID, creatureName, creatureSpellID, icon, issummoned, mountType}
  19.         --print(mounts.ground[g][2])
  20.         g = g + 1
  21.     elseif mountType == 12 then
  22.         mounts.aquatic[a] = {creatureID, creatureName, creatureSpellID, icon, issummoned, mountType}
  23.         --print(mounts.aquatic[a][2])
  24.         a = a + 1
  25.     end
  26. end
  Reply With Quote
07-31-13, 02:19 AM   #2
Malsomnus
A Cobalt Mageweaver
AddOn Author - Click to view addons
Join Date: Apr 2013
Posts: 203
First thing that pops up for me is the line:
Lua Code:
  1. local f, g, a = 1

Is it possible that what you meant to write is
Lua Code:
  1. local f, g, a = 1, 1, 1
?

[Edit]
It really looks like lines 18, 20, 22 and 24 should complain about indexing/ arithmetic with nil values. Do you get these errors?
Anyway instead of managing indexes you can just use:
Lua Code:
  1. table.insert (mounts.flying, {creatureID, creatureName, creatureSpellID, icon, issummoned, mountType})
__________________
SanityCheck - If you've ever said the words "Sorry, I forgot" then you need this add-on.

Remember, every time you post a comment on an add-on, a kitten gets its wings!

Last edited by Malsomnus : 07-31-13 at 02:25 AM.
  Reply With Quote
07-31-13, 03:02 AM   #3
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
tinsert is definitely the way to go here. Keeping track of the indices yourself is just masochistic.

Also, the mountType returned by GetCompanionInfo is a bitfield, not a decimal value, so rather than trying to account for all possible combinations, you should just use bit operators to check the specific flags you're interested in:

Code:
if bit.band(mountType, 0x8) ~= 0 then
    -- aquatic
elseif bit.band(mountType, 0x2) ~= 0 then
    -- flying
elseif bit.band(mountType, 0x1) ~= 0 then
   -- ground
end
The order is fairly important, as mounts can be flagged as flying and ground, for example; the above prioritizes any flight-capable mount as a flying mount, so it will only consider mounts to be ground mounts if they can't fly.

On a side note, you're creating your mounts table in the least efficient way possible. Instead of this:

Code:
local mounts = {}
    mounts.flying = {}
    mounts.ground = {}
    mounts.aquatic = {}
...do this:
Code:
local mounts = {
    flying = {},
    ground = {},
    aquatic = {}
}
Works out exactly the same, and doesn't cost all the extra lookups.
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.

Last edited by Phanx : 07-31-13 at 04:46 PM.
  Reply With Quote
07-31-13, 04:12 PM   #4
Niketa
A Wyrmkin Dreamwalker
 
Niketa's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2013
Posts: 54
Thank you guys. Helpful as usual.
  Reply With Quote
07-31-13, 11:49 PM   #5
Niketa
A Wyrmkin Dreamwalker
 
Niketa's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2013
Posts: 54
When I first log on the tables don't automatically populate and I have to /reload to get them to populate.

I've tried leaving the code outside of any event, tried PLAYER_LOGIN, PLAYER_ENTERING_WORLD and PLAYER_ALIVE.
  Reply With Quote
08-01-13, 02:05 AM   #6
Malsomnus
A Cobalt Mageweaver
AddOn Author - Click to view addons
Join Date: Apr 2013
Posts: 203
Ah, I hate data that isn't available on PLAYER_ENTERING_WORLD. I've found myself several times putting such code in a function that gets called from OnUpdate after it counts a few seconds.
I'd love to hear if anybody knows a correct way of doing that
__________________
SanityCheck - If you've ever said the words "Sorry, I forgot" then you need this add-on.

Remember, every time you post a comment on an add-on, a kitten gets its wings!
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Saving values to different tables in a loop.


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off