upvalue functions and default key values of an array - WoWInterface
Thread Tools Display Modes
Prev Previous Post   Next Post Next
04-06-20, 06:33 AM   #1
A Frostmaul Preserver
gmarco's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2009
Posts: 290
upvalue functions and default key values of an array


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 = ...
  4. -- defaults
  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, ...)
  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

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