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:
|
First thing that pops up for me is the line:
Lua Code:
Is it possible that what you meant to write is Lua Code:
[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:
|
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 On a side note, you're creating your mounts table in the least efficient way possible. Instead of this: Code:
local mounts = {} Code:
local mounts = { |
Thank you guys. Helpful as usual. :)
|
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. |
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 :p |
Post your entire code, please. Polling with an OnUpdate script is never the best way (or even a good way) to find out when some data becomes available. :(
|
Not polling, just waiting a few seconds. It seems to work consistently. I know it's horrible, I just haven't found any event that can actually tell me when certain data (e.g. my gear, my raid roster) is available, and it wouldn't surprise me if the same applies to mounts.
|
Lua Code:
|
To work on clients other than English, use the CreatureID instead of the CreatureName. Also, there are two Mammoths, one per faction. Change the following code accordingly:
Lua Code:
|
Based on the default UI's code, I would guess you should watch for COMPANION_LEARNED and COMPANION_UNLEARNED events to detect when the list of known mounts changes:
Code:
function MountJournal_OnLoad(self) |
myrroddin, I followed your suggestion but you were watching creatureID when you defined the creatureSpellID so I switched that up.
Phanx, it looks like COMPANION_UPDATE did the trick to load them up when I first log in. Thanks. :D Lua Code:
|
Ah, I did not know I was looking up the creatureSpellID when I hit WowHead, sorry! One more suggestion: move the variable definition outside the for loop. Right now you are (re)defining the variables on every loop, creating unnecessary garbage collection and a great deal of inefficiency.
Lua Code:
Lua Code:
Lua Code:
|
Quote:
Lua Code:
|
Semlar, that is what I thought. It would return true if var was equal to 1 or 2 or 3. You shouldn't have to keep checking if var, if var, if var... That seems overly verbose.
The reason I asked is because with two choices, it works fine. Lua Code:
|
I think maybe I wasn't clear.
You can't do "if var == 1 or 2 or 3", it's not the same as "if var == 1 or var == 2 or var == 3", it will always return true because "if 2" will always evaluate to true. |
Quote:
However, redefining 3 number values whenever COMPANION_UPDATE fires is hardly "a great deal of inefficiency", especially for the purposes of a beginner still trying to figure out the basic logic. |
Quote:
Code:
if var == 1 or 2 or 3 then Code:
if var == 1 then Code:
if var == 1 then Code:
if var == 1 or var == 2 or var == 3 then |
Actually he's right. The code he quoted was his correction. I had the mammoths defined *inside* the loop, meaning for each mount I kept defining the mammoths, not for each COMPANION_UPDATE. I've fixed that. Also the COMPANION_UPDATE should only ever run once when logging in... and that same code only ever gets run again on PLAYER_ENTERING_WORLD so really it shouldn't be happening all that often.
Btw, just want to say thank you all for the help. There aren't any real good tutorials for lua/WoW addons so having you guys here is very helpful. Hopefully eventually I'll start thinking of these things so you guys don't have to tell me. >< |
Niketa, glad you have it working!
Semlar and Phanx, thank you for the clarifications! I was looking for a syntactic shortcut where none exists. Strangely, I have has to validate if more than one var == something, but thus far in any code I've written have I had to validate one var against multiple values. Odd that, but now I know. |
All times are GMT -6. The time now is 07:33 AM. |
vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI