Thread Tools Display Modes
05/23/14, 10:29 AM   #1
circonian
AddOn Author - Click to view addons
Join Date: May 2014
Posts: 613
Post Index Nill and Square Brackets

I have two questions if anyone can help me out. Thanks!

First Question:
Can anyone tell me why one of these works and the other does not?
TargetHPBar[1] turns out nil for ZO_TargetUnitFramereticleover, but it works for ZO_PlayerAttributeHealth
Lua Code:
  1. HPBar =       {ZO_PlayerAttributeHealth,      true, false, DEFAULT_ALPHA,   69,   0, 300, DEFAULT_SCALE},
  2. TargetHPBar = {ZO_TargetUnitFramereticleover, true, false, DEFAULT_ALPHA,    0, 150, 300, DEFAULT_SCALE},

I'm trying to do this:
Lua Code:
  1. ConfigurableUI.BarSetup(TargetHPBar)
  2.  
  3. function ConfigurableUI.BarSetup(_BarTable)
  4.     _Control, _Unlocked, _Hidden, _Alpha, _Posx, _Posy, _Width, _Scale = unpack(_BarTable)
  5.     _Control:SetMovable(_Unlocked)
  6.        ...
  7. end

Second Question:
Looking at other addons I can't figure out what the difference is between some of the assignments I see like:
Lua Code:
  1. -- I see some like this...I get this one.
  2. DAMAGE_COLOR         = { 1, 1, 1, 1 }
  3. -- But I also see some with square brackets around them
  4. [DAMAGE_TYPE_NONE]      = { 1, 1, 1, 1 }
  5. -- Some also have single quotes in them
  6. ['ZO_ActionBar1'] = {1, myaddon.stuff}
  7. -- Some also have double quotes in them
  8. ["enabled"] = true,
Whats the difference in those ? Why do some have square brackets, some have ['...'] and some have ["..."]?

Thanks
  Reply With Quote
05/23/14, 12:09 PM   #2
Edda
 
Edda's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 60
"blabla" and 'blabla' is exactly the same thing. "..." and '...' are used to delimit a string. In PHP they are slightly different ("..." lets you insert variables on the fly like : "my max health is $MaxHP"), not sure about Lua but afaik they are stricly the same.

The difference between DAMAGE_COLOR and [DAMAGE_COLOR] is that [] are used to define a table index so it's likely that [DAMAGE_COLOR] = stuff is part of a table, where DAMAGE_COLOR = stuff is just a variable defined with name DAMAGE_COLOR

Example without table :

Code:
DAMAGE_COLOR = "red"
HEAL_COLOR = "green"
BUFF_COLOR = "yellow"
Example with table :

Lua Code:
  1. local Colors = {} -- create an empty table
  2. Colors.DAMAGE_COLOR = "red"
  3. Colors.HEAL_COLOR = "green"
  4. Colors.BUFF_COLOR = "yellow"

Or

Lua Code:
  1. local Colors = {} -- create an empty table
  2. Colors["DAMAGE_COLOR"] = "red"
  3. Colors["HEAL_COLOR"] = "green"
  4. Colors["BUFF_COLOR"] = "yellow"

Which is the same thing.

Then you can access your colors with either i.e. Colors.DAMAGE_COLOR or Colors["DAMAGE_COLOR"].

Now for [DAMAGE_TYPE_NONE] without quotes this is different because the thing will return you the value with index of the DAMAGE_TYPE_NONE value.

Example :

Lua Code:
  1. local Colors = {"red", "green", "blue"}
  2. local MY_SUPER_INDEX = 2
  3. print(Colors[MY_SUPER_INDEX]) -- "green"
  4.  
  5. MY_SUPER_INDEX = 4
  6. print(Colors[MY_SUPER_INDEX]) -- nil

To sum up : ".." or '..' is for string. [] is for table indexes. ["MyIndex"] is for value with index "MyIndex" and [MyIndex] is for value with index *content of MyIndex* - if that makes sense because with [MyIndex] the MyIndex is not considered an index but a variable.

Another silly example :

Lua Code:
  1. local index1 = "color1"
  2. local index2 = "color2"
  3. local index3 = "color3"
  4.  
  5. local Colors = {[index1] = "red", [index2] = "green", [index3] = "blue"}
  6.  
  7. print(Colors["color3"]) -- "blue"
  8. print(Colors.color3) -- "blue"
  9. print(Colors.index3) -- nil

About your first question : I think TargetHPBar[1] turns out nil because ZO_TargetUnitFramereticleover IS nil. Did you check for a typo ? Lua is case-sensitive.

Last edited by Edda : 05/23/14 at 12:36 PM.
  Reply With Quote

ESOUI » Developer Discussions » Lua/XML Help » Index Nill and Square Brackets


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