WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   Would it be possible to get value of element during table construction? (https://www.wowinterface.com/forums/showthread.php?t=54079)

Layback_ 07-31-16 05:16 AM

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.

Resike 07-31-16 06:34 AM

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.

Banknorris 07-31-16 06:48 AM

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.

Layback_ 07-31-16 07:47 AM

Quote:

Originally Posted by Resike (Post 317244)
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.

Quote:

Originally Posted by Banknorris (Post 317246)
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?

Resike 07-31-16 10:10 AM

Quote:

Originally Posted by Layback_ (Post 317248)
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.

Layback_ 07-31-16 04:48 PM

Quote:

Originally Posted by Resike (Post 317252)
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 :D

I'll have a go with it!

Thank you

SDPhantom 08-01-16 02:44 AM

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);


Resike 08-01-16 03:55 AM

Quote:

Originally Posted by SDPhantom (Post 317272)
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.


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

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI