Thread Tools Display Modes
01-25-17, 09:23 AM   #1
sendrock
A Defias Bandit
Join Date: Jan 2017
Posts: 3
[Solved] Help moving a variable outside of an event

Hi everyone,

I'm a total newbie, I never tried to code anything before this addon for me and my friends. After a week of reading/testing and asking about SavedVariable, I had something that worked (by miracle).

The code was pretty ugly coz after testing, sometimes, I forgot to delete things (here is the old code : http://pastebin.com/z28uZRrC )

So, I tried to make it easier to read and now, and now I have this one : http://pastebin.com/QUAA1G8k

Now here is my problem, I wanted to move the line 141 'local mydbname = Init_SAKL_DB()'; to the line 102 (copy/paste). I wanted this, to create the "mydbname" variable when PLAYER_ENTERING_WORLD instead of creating it when the player enter the tooltip.

But when I move it to the line 102, then I have an error "trying to index mydbname a nil value".

I think it's because I use "local mydbname" at line 102, so at line 103 (end) mydbname is deleted. But how can I make it to stay alive ? I can make it global, without declaring the "local thing", but I always read " make your variable local, so you avoid problems with others addon".

If you have any advice on what I'm doing wrong, I'll take it and try to apply it


TLDR : An Error When I move my line 141 to line 102. If someone can tell me what I'm doing wrong, I'll try to fix it coz for now, I'm lost :/

Last edited by sendrock : 01-26-17 at 09:12 AM. Reason: solved
  Reply With Quote
01-25-17, 11:44 AM   #2
Kanegasi
A Molten Giant
 
Kanegasi's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2007
Posts: 666
At the top of your file, use local mydbname with no = and then on line 102, remove "local" like you're declaring a global. Just like the other variables at the top, mydbname is now local to your file, so any changes to it stay local.
  Reply With Quote
01-25-17, 04:49 PM   #3
sendrock
A Defias Bandit
Join Date: Jan 2017
Posts: 3
Hello, first, thanks for answering.

Yes, I ~tried this before, but I had an error that tell me the call of "mydbname" is "nil" when a table was expected.

I tried again, as you said, and I still have an error about nil value for mydbname instead of table. Here is the .lua after the change I'v made : http://pastebin.com/3gdQscaH


At line 146, I still have to make the call "mydbname = Init_SAKL_DB();" (the one I made as a comment) to get no error. If i don't call mydbname at this moment, the value is nil (declared at line 8 I think) but the call at line 106 doesn't seem to stay.
  Reply With Quote
01-25-17, 08:12 PM   #4
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
PLAYER_ENTERING_WORLD should fire after all addons load. That said, don't use that when looking for your saved variables. Use ADDON_LOADED and check to see if it fired for your addon (and not one of the many others you may have installed) before running your code.
http://wow.gamepedia.com/Saving_vari..._game_sessions

Second, what EXACTLY does the error message say? If you have BugGrabber/BugSack installed (which you should), you can copy it right out of the window.
__________________
"You'd be surprised how many people violate this simple principle every day of their lives and try to fit square pegs into round holes, ignoring the clear reality that Things Are As They Are." -Benjamin Hoff, The Tao of Pooh

  Reply With Quote
01-25-17, 08:55 PM   #5
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,879
Also PLAYER_ENTERING_WORLD fires every time you see a loading screen. Do you really want to do the Initialisation every time you take portal, boat, zeplin etc.? As Seerah mentioned you could use ADDON_LOADED and check to see if it's your addon that loaded or PLAYER_LOGIN that fires after all addons have loaded and then only once.

Your if statement in Init_SAKL_DB() (and possibly others) is not going to work.

An if/elesif/else clause only executes the first statement that evaluates to true each time the clause is called. Every statement after that is ignored, so:

Lua Code:
  1. if not SAKL_DB then
  2.           SAKL_DB = {}
  3.     elseif not SAKL_DB[playerRealm] then
  4.         SAKL_DB[playerRealm] = { [playerName] = {
  5.                                     ["class"] = classIndex;
  6.                                     ["amount"] = colorBetter.. amount;
  7.                                     ["level"] = UnitLevel("player");
  8.                                     ["name"] = colorBetter.. playerName;
  9.                                 };
  10.         };
  11. --etc.
  12.     end

The first time the function is called, "SAKL_DB = {}" gets executed because SAKL_DB does not exists (You've entered the world for the first time).

The second time the function is called (after going through a portal etc.), "SAKL_DB[playerRealm] = { [playerName] = {..." gets executed because SAKL_DB now exists from call 1.

"elseif amount == 25 then" and "elseif amount == 0 then" will not be executed until a character is level 110 and the function is called 3 times (portals). Until 110, "elseif UnitLevel("player") < 110 then" will be executed on the third call (portal).

You probably want something more like (it could be done slightly differently and reduced as there is no need to overwrite the entries that do not change from the default):
Lua Code:
  1. if not SAKL_DB then -- Create the default
  2.     SAKL_DB = {
  3.         [playerRealm] = {
  4.             [playerName] = {
  5.                 ["class"] = classIndex;
  6.                 ["amount"] = colorBetter.. amount;
  7.                 ["level"] = UnitLevel("player");
  8.                 ["name"] = colorBetter.. playerName;
  9.             };
  10.         )
  11. end
  12.  
  13. if UnitLevel("player") < 110 then -- Set to < 110 setting
  14.         SAKL_DB[playerRealm][playerName] = {
  15.         ["class"] = classIndex;
  16.         ["amount"] = colorBetter.. "----";
  17.         ["level"] = UnitLevel("player");
  18.         ["name"] = colorBetter.. playerName.. " (Not 110)";
  19.         };
  20. elseif amount == 25 then -- Set to 110+ with amount = 25 setting
  21.         SAKL_DB[playerRealm][playerName] = {
  22.         ["class"] = classIndex;
  23.         ["amount"] = colorBetter.. "25 (Max)";
  24.         ["level"] = UnitLevel("player");
  25.         ["name"] = colorBetter.. playerName;
  26.         };
  27. elseif amount == 0 then -- Set to 110+ with amount = 0 setting
  28.         SAKL_DB[playerRealm][playerName] = {
  29.         ["class"] = classIndex;
  30.         ["amount"] = colorBetter.. "0";
  31.         ["level"] = UnitLevel("player");
  32.         ["name"] = colorBetter.. playerName;
  33.         };
  34. end
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 01-25-17 at 11:53 PM.
  Reply With Quote
01-26-17, 09:11 AM   #6
sendrock
A Defias Bandit
Join Date: Jan 2017
Posts: 3
Your if statement in Init_SAKL_DB() (and possibly others) is not going to work.
Yeah, that was the problem i think, coz I had the same error with ADDON_LOADED.

So I started again from a fresh block for this function and I did it slowly, step by step, and now it seems to work fine even if I don't call "mydbname" at line ~140. Now I'll try to make the block reduced as you said.

Thanks Fizzlemizz,Kanegasi and Seerah for helping, have a nice day.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » [Solved] Help moving a variable outside of an event


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