Quantcast
Would it be possible to get value of element during table construction? - WoWInterface
Thread Tools Display Modes
07-31-16, 05:16 AM   #1
Layback_
An Onyxian Warder
Join Date: Feb 2016
Posts: 358
Would it be possible to get value of element during table construction?

Hi again all,

The title says everything.

I was trying to create a configuration table for my castbar and just got a question regarding table construction.

Let's say I have a simple table like this:

Lua Code:
  1. G["castbar"] = {
  2.     ["player"] = {
  3.         ["width"] = 350,
  4.         ["height"] = 35,
  5.         ["point"] = {"CENTER", UIParent, "CENTER", 250, -200},
  6.         ["iconSize"] = 35,
  7.         ["spacing"] = 2,
  8.     },
  9. }

As you can see, height and iconSize have a same value and this is because I would like the icon to be the same size as the castbar's height.

Of course, I could do something like

Lua Code:
  1. G["castbar"] = {
  2.     ["player"] = {
  3.         ["width"] = 350,
  4.         ["height"] = 35,
  5.         ["point"] = {"CENTER", UIParent, "CENTER", 250, -200},
  6.         ["spacing"] = 2,
  7.     },
  8. }
  9. G["castbar"]["player"]["iconSize"] = G["castbar"]["player"]["height"];

and this will definitely work without any issues.

However, as I add target's castbar, targettarget's castbar, party1's castbar and so on, this would look ugly and would be complex for me to manage with.

e.g:
Lua Code:
  1. G["castbar"] = {
  2.     ["player"] = {
  3.         ["width"] = 350,
  4.         ["height"] = 35,
  5.         ["point"] = {"CENTER", UIParent, "CENTER", 250, -200},
  6.         ["spacing"] = 2,
  7.     },
  8.     ["target"] = {
  9.         ["width"] = G["unitframe"]["target"]["width"],
  10.         ["height"] = 20,
  11.         ["point"] = {"TOP", UIParent, "BOTTOM", 0, 200},
  12.         ["iconSize"] = 20,
  13.     },
  14. }
  15. G["castbar"]["player"]["iconSize"] = G["castbar"]["player"]["height"];
  16. G["castbar"]["target"]["spacing"] = G["castbar"]["player"]["spacing"];
  17. .
  18. .
  19. .

I also tried something like the following and this doesn't seem to be working. I'm guessing that this is because the table G["castbar"] does not exist yet, since it is on the way of its construction.

Lua Code:
  1. G["castbar"] = {
  2.     ["player"] = {
  3.         ["width"] = 350,
  4.         ["height"] = 35,
  5.         ["point"] = {"CENTER", UIParent, "CENTER", 250, -200},
  6.         ["iconSize"] = G["castbar"]["player"]["height"],
  7.         ["spacing"] = 2,
  8.     },
  9. }

To gain some idea, I have gone through the official references, but could have not found anything close to this...

Would there be any possible approach to achieve something similar to the above?

Thank you.

Last edited by Layback_ : 07-31-16 at 05:43 AM.
  Reply With Quote
07-31-16, 06:34 AM   #2
Resike
A Pyroguard Emberseer
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 1,289
Why don't you just construct your icon like this?

Lua Code:
  1. local height = castbar:GetHeight()
  2. icon:SetSize(height, height)

Then you can ditch the variable too.
  Reply With Quote
07-31-16, 06:48 AM   #3
Banknorris
A Chromatic Dragonspawn
 
Banknorris's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2014
Posts: 153
Working on the previous solution, maybe:
Lua Code:
  1. G["castbar"] = {
  2.     ["player"] = {
  3.         ["width"] = 350,
  4.         ["height"] = 35,
  5.         ["point"] = {"CENTER", UIParent, "CENTER", 250, -200},
  6.         ["iconSize"] = "height",
  7.         ["spacing"] = 2,
  8.     },
  9. }

then when using the configuration do something like:

Lua Code:
  1. local height = G["castbar"]["player"]["height"]
  2. local iconSize = G["castbar"]["player"]["iconSize"]
  3. if iconSize=="height" then
  4.         iconSize = height
  5. end
  6. castbar:SetHeight(height)
  7. icon:SetSize(iconSize,iconSize)
If in the future you decide to set iconSize to lets say 50, you just need to change the configuration value from "height" to 50, the code the uses the configuration will still work.
__________________
"In this world nothing can be said to be certain, except that fractional reserve banking is a Ponzi scheme and that you won't believe it." - Mandrill

Last edited by Banknorris : 07-31-16 at 06:59 AM.
  Reply With Quote
07-31-16, 07:47 AM   #4
Layback_
An Onyxian Warder
Join Date: Feb 2016
Posts: 358
Originally Posted by Resike View Post
Why don't you just construct your icon like this?

Lua Code:
  1. local height = castbar:GetHeight()
  2. icon:SetSize(height, height)

Then you can ditch the variable too.
That's actually a good point!

But, if possible, I would like to keep them as a two separate variables.

For now, height and iconSize could be the same values, but for some cases I could give some variations on them.

Originally Posted by Banknorris View Post
Working on the previous solution, maybe:
Lua Code:
  1. G["castbar"] = {
  2.     ["player"] = {
  3.         ["width"] = 350,
  4.         ["height"] = 35,
  5.         ["point"] = {"CENTER", UIParent, "CENTER", 250, -200},
  6.         ["iconSize"] = "height",
  7.         ["spacing"] = 2,
  8.     },
  9. }

then when using the configuration do something like:

Lua Code:
  1. local height = G["castbar"]["player"]["height"]
  2. local iconSize = G["castbar"]["player"]["iconSize"]
  3. if iconSize=="height" then
  4.         iconSize = height
  5. end
  6. castbar:SetHeight(height)
  7. icon:SetSize(iconSize,iconSize)
If in the future you decide to set iconSize to lets say 50, you just need to change the configuration value from "height" to 50, the code the uses the configuration will still work.
This could be the possible solution!

So... there is no such a straight-forward way to achieve this, right?
  Reply With Quote
07-31-16, 10:10 AM   #5
Resike
A Pyroguard Emberseer
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 1,289
Originally Posted by Layback_ View Post
That's actually a good point!

But, if possible, I would like to keep them as a two separate variables.

For now, height and iconSize could be the same values, but for some cases I could give some variations on them.
Then you could just addon another boolean variable called "sameSize", and if it's true then you use the castbar's height, else the icon height.
  Reply With Quote
07-31-16, 04:48 PM   #6
Layback_
An Onyxian Warder
Join Date: Feb 2016
Posts: 358
Originally Posted by Resike View Post
Then you could just addon another boolean variable called "sameSize", and if it's true then you use the castbar's height, else the icon height.
Sounds good for me

I'll have a go with it!

Thank you
  Reply With Quote
08-01-16, 02:44 AM   #7
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 1,929
Why not have it default to height if iconSize is absent (nil)?
You can then use a fallback assignment like the following:
Code:
local height = G["castbar"]["player"]["height"];
local size = G["castbar"]["player"]["iconSize"] or height;
castbar:SetHeight(height);
icon:SetSize(size,size);
__________________
ESOUI AddOns | WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)

Last edited by SDPhantom : 08-01-16 at 02:47 AM.
  Reply With Quote
08-01-16, 03:55 AM   #8
Resike
A Pyroguard Emberseer
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 1,289
Originally Posted by SDPhantom View Post
Why not have it default to height if iconSize is absent (nil)?
You can then use a fallback assignment like the following:
Code:
local height = G["castbar"]["player"]["height"];
local size = G["castbar"]["player"]["iconSize"] or height;
castbar:SetHeight(height);
icon:SetSize(size,size);
That problem with that you lose the set vaule every time you "uncheck" same size.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Would it be possible to get value of element during table construction?

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