Thread Tools Display Modes
04-06-20, 06:33 AM   #1
gmarco
An Onyxian Warder
 
gmarco's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2009
Posts: 362
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.
__________________
This is Unix-Land. In quiet nights, you can hear the Windows machines reboot.

Last edited by gmarco : 04-06-20 at 06:36 AM.
  Reply With Quote
04-06-20, 09:14 AM   #2
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,871
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.
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 04-06-20 at 09:51 AM.
  Reply With Quote
04-06-20, 11:12 AM   #3
gmarco
An Onyxian Warder
 
gmarco's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2009
Posts: 362
Originally Posted by Fizzlemizz View Post
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

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.
__________________
This is Unix-Land. In quiet nights, you can hear the Windows machines reboot.
  Reply With Quote
04-06-20, 11:22 AM   #4
gmarco
An Onyxian Warder
 
gmarco's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2009
Posts: 362
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
__________________
This is Unix-Land. In quiet nights, you can hear the Windows machines reboot.
  Reply With Quote
04-06-20, 11:55 AM   #5
gmarco
An Onyxian Warder
 
gmarco's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2009
Posts: 362
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.
__________________
This is Unix-Land. In quiet nights, you can hear the Windows machines reboot.
  Reply With Quote
04-06-20, 12:10 PM   #6
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,871
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.
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 04-06-20 at 12:12 PM.
  Reply With Quote
04-06-20, 12:52 PM   #7
gmarco
An Onyxian Warder
 
gmarco's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2009
Posts: 362
Originally Posted by Fizzlemizz View Post
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.
__________________
This is Unix-Land. In quiet nights, you can hear the Windows machines reboot.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » upvalue functions and default key values of an array

Thread Tools
Display Modes

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