WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   upvalue functions and default key values of an array (https://www.wowinterface.com/forums/showthread.php?t=57912)

gmarco 04-06-20 06:33 AM

upvalue functions and default key values of an array
 
Hi,

I'd like to ask a couple of questions.

1) Is fine to upvalue a function using it's same name ?

Something like:

Lua Code:
  1. local string.format = string.format

I usually wrote something like:

Lua Code:
  1. local string_format = string.format

It obviusly works but in this way I dont have the syntax color of the editor :/ :)
Does it have some dark side effects ?

2) Another question.

Until now I usually initialize (wrongly) my vars in this way.

Lua Code:
  1. -- beginning
  2. local ADDON = ...
  3.  
  4. -- defaults
  5.  
  6. MYADDON_CFG = {
  7.     KEY1 = true,
  8.     KEY2 = "var2",
  9.     KEY3 = 3,
  10. }

Now I have understood better that the vars are overwritten during ADDON_LOADED event so I can write in this way:

Lua Code:
  1. frame:SetScript("OnEvent", function(self, event, arg1, ...)
  2.  
  3.     if event == "ADDON_LOADED" and arg1 == ADDON then
  4.         -- Some defaults I dont want to be nil
  5.         if MYADDON_CFG["KEY1"] == nil then MYADDON_CFG["KEY1"] = true end
  6.         if MYADDON_CFG["KEY2"] == nil then MYADDON_CFG["KEY2"] = "var2" end
  7.         if MYADDON_CFG["KEY3"] == nil then MYADDON_CFG["KEY3"] = 3 end
  8.         frame:UnregisterEvent("ADDON_LOADED")
  9.     end

What I want to ask ... Is there a better way to write values in an array if they are null ? Or the

Lua Code:
  1. if then end

for every key is a good enough solution ?

Thanks so much to everyone and patience.

Fizzlemizz 04-06-20 09:14 AM

1. You can't
local string.format = string.format
directly, you would need a local "string" table to assign the functions to (a bit self defeating):
Code:

local string = {
        format = string.format,
}

but you can lessen your typing depending on preference .

Code:

local format = string.format
2.
Lua Code:
  1. if event == "PLAYER_LOGIN" then
  2.     if not MYADDON_CFG then -- doesn't exist so add defaults
  3.         MYADDON_CFG = {
  4.             KEY1 = true,
  5.             KEY2 = "var2",
  6.             KEY3 = 3,
  7.         }
  8.     end
  9. end

PLAYER_LOGIN only fires once after all addons have loaded including their saved variables. The game knows most things about the character loading that don't requiring a server request at this point.

gmarco 04-06-20 11:12 AM

Quote:

Originally Posted by Fizzlemizz (Post 335563)
1. You can't
local string.format = string.format
directly, you would need a local "string" table to assign the functions to (a bit self defeating):
Code:

local string = {
        format = string.format,
}

but you can lessen your typing depending on preference .

Code:

local format = string.format

Hi Fizzlemizz,


now I understand why I usually never found upvalued with same name.
And in my case that I string.format a couple of vars I think I dont need at all :)

Quote:

2.
Lua Code:
  1. if event == "PLAYER_LOGIN" then
  2.     if not MYADDON_CFG then -- doesn't exist so add defaults
  3.         MYADDON_CFG = {
  4.             KEY1 = true,
  5.             KEY2 = "var2",
  6.             KEY3 = 3,
  7.         }
  8.     end
  9. end

PLAYER_LOGIN only fires once after all addons have loaded including their saved variables. The game knows most things about the character loading that don't requiring a server request at this point.
Thanks for input on PLAYER_LOGIN vs ADDON_LOADED event but here the question is a little bit different :)

I am not asking to use defaults for ALL keys if not exist MYADDON_CFG but I am asking for a different approach to put a default value when happens that some of these keys has nil values (for example a new config).

For this reason I use:

Lua Code:
  1. if MYADDON_CFG["KEY1"] == nil then MYADDON_CFG["KEY1"] = true end

for every keys.

Probably we can use two tables.

MYADDON_CFG and MYADDON_CFG_DEFAULTS and then

Lua Code:
  1. for k in pairs(MYADDON_CFG_DEFAULTS) do
  2.     MYADDON_CFG[k] = MYADDON_CFG[k] or MYADDON_CFG_DEFAULTS[k]
  3. end

I have to check.

Thanks so much for your kind reply. They are always precious.

gmarco 04-06-20 11:22 AM

I think in this way it could be fine to write and it seems to works to :)

Lua Code:
  1. if event == "PLAYER_LOGIN" then
  2.     local GMEXP_DEFAULTS = { LONGNRFMT = false, SCALE = 1, LIST = true, MAXLIST = 12 }
  3.     for k in pairs(GMEXP_DEFAULTS) do
  4.         GMEXP_CFG[k] = GMEXP_CFG[k] or GMEXP_DEFAULTS[k]
  5.     end
  6. end

gmarco 04-06-20 11:55 AM

Uhm I have to rewritten it in this way:

Lua Code:
  1. if event == "PLAYER_LOGIN" then
  2.     local GMEXP_DEFAULTS = { LONGNRFMT = false, SCALE = 1, LIST = true, MAXLIST = 12 }
  3.     for k in pairs(GMEXP_DEFAULTS) do
  4.         if GMEXP_CFG[k] == nil then GMEXP_CFG[k] = GMEXP_DEFAULTS[k] end
  5.     end
  6. end

or the values = false are overwritten by defaults like they (I wrongly supposed) were nil. And it is not what I am looking for.
In this way it works.

Fizzlemizz 04-06-20 12:10 PM

It doesn't check if the GMEXP_CFG exists so on a new install where the game has created the placeholder (GMEXP_CFG = nil) but you haven't assigned a table to it, this will fail.

Lua Code:
  1. if event == "PLAYER_LOGIN" then
  2.     if not GMEXP_CFG then
  3.         GMEXP_CFG = {}
  4.     end
  5.     -- etc.
  6. end
With your code, if a user sets a setting to nil it will be reset on next login which is probably not desired and depends on your configuration setup.

gmarco 04-06-20 12:52 PM

Quote:

Originally Posted by Fizzlemizz (Post 335572)
It doesn't check if the GMEXP_CFG exists so on a new install where the game has created the placeholder (GMEXP_CFG = nil) but you haven't assigned a table to it, this will fail.

Lua Code:
  1. if event == "PLAYER_LOGIN" then
  2.     if not GMEXP_CFG then
  3.         GMEXP_CFG = {}
  4.     end
  5.     -- etc.
  6. end
With your code, if a user sets a setting to nil it will be reset on next login which is probably not desired and depends on your configuration setup.

Ciao Fizzlemizz.

I am doing this in the first part of the addon ...

Lua Code:
  1. local ADDON, namespace = ...
  2. local L = namespace.L
  3.  
  4. GMEXP_NAMES = GMEXP_NAMES or {}
  5. GMEXP_CFG = GMEXP_CFG or {}
  6. -- go on

... because I didn't think I couldn't move them there :)
Now that I know I can I'll move them there.

Thanks so much Fizzlemizz.


All times are GMT -6. The time now is 08:09 AM.

vBulletin © 2020, Jelsoft Enterprises Ltd
© 2004 - 2020 MMOUI