table index is nil
Hi, I have been trying to fix this addon and my knowledge of lua is limited at best. It still works, but throws an error first time it's called then is fine. It started throwing the error in pre-patch shadowlands. And thank you in advance for help to fix this.
This is the lua error ... Quote:
TOC file - The only thing I've changed in here is the Interface number Code:
## Interface: 90001 Code:
BattleDex = {}; |
Try moving OnReady from ADDON_LOADED to PLAYER_LOGIN. Your SavedVariables will have been loaded by the game when this event fires and it only fires once.
Don't forget to remove your old SV .lua file(s) before trying. Lua Code:
|
SavedVars should be ready by the time ADDON_LOADED fires. It's part of the addon loading process. The error isn't complaining about the table itself being nil, there's a different error for that. The problem is when you use nil as a key.
The code only works if the opponent has 3 battle pets. If they don't have a full team, this error occurs. One or more empty spots causes species in BattleDex.RecordPet() (along with most of the other arguments) to be nil. This is what the error means by "table index is nil". Again, if this was a load issue with the SavedVar, this would be "attempt to index nil" instead. It's a common mistake to confuse these two. |
Thank you for your help.
I changed to your code, still get the error, slightly different, but same error, when starting the first battle, and it doesn't record the first battle, but does record later battles. I removed the SV before I went in game. I have a full team, but the wild pets don't always, testing in Darnassus where pets are single, low level. I'm fairly sure it was throwing the error on 3 wild pets, full team, too, but will go test the theory. ... Theory tested, and No, no error when starting with wild pets that have a full team with changed lua file. Also tested my original lua file and no it doesn't error with the other team being full either. Both versions of lua recorded the first battle. |
That's the spot it's complaining about. Line 69 in function BattleDex.RecordPet(). The only line there that would generate that error (there are other operations like it, but it would throw a different error generating the second key) is this.
Code:
_G.BattleDexDB.pets[species] = _G.BattleDexDB.pets[species] or {}; PS: The theory was about the enemy team having less than 3 pets. The only other explanation is if BattleDex.RecordBattle() is running before pet data is available. |
So is there a way I can get it to realize that when the pet doesn't exist it's 0, zero, not nil? or that it's ok that the pet doesn't exist? and just record the existing pet/s
a couple of lines down it tells it to either record/add 0 or 1 I'm presuming that's not enough? Code:
_G.BattleDexDB.pets[species][key] = _G.BattleDexDB.pets[species][key] or 0; |
Option 1: Add a safeguard to RecordPet() so that it doesn't try to execute when information is missing.
Lua Code:
Option 2: Change RecordBattle() so it is smart enough to know you sometimes have fewer than three pets. I think you can do it using C_PetBattles.GetNumPets() but its probably also doable with a simple if (s2) then ... end Lua Code:
|
Awesome, I went with option 2. It errored on login, but putting 'then' on line 22 fixed that, and it's now recording the low level pets without error.
Thank you very much everyone for your help :D |
I'd honestly convert it to a loop.
Lua Code:
|
All times are GMT -6. The time now is 10:45 AM. |
vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI