Thread Tools Display Modes
10-23-10, 01:47 AM   #1
Grimsin
A Molten Giant
 
Grimsin's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2006
Posts: 990
Using a function in a table as a variable?

Is it possible to use locals or functions in a table of variables? so lets say you have this...

mytable = {
["scale"] = 1,
}

would it be possible to do something like this

local scale

function addon:FigureScale()
do some stuff to get the scale
scale = result
end

mytable = {
["scale"] = scale,
}

or

mytable = {
["scale"] = addon:FigureScale(),
}

??? or some other method that would give me the same functionality?
__________________
"Are we there yet?"

GrimUI
[SIGPIC][/SIGPIC]
  Reply With Quote
10-23-10, 02:01 AM   #2
Luzzifus
A Warpwood Thunder Caller
 
Luzzifus's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2007
Posts: 94
Code:
mytable = {
    ["scale"] = 1,
}

function addon:FigureScale()
     result = do some stuff to get the scale
     mytable.scale = result
end
or

Code:
function addon:FigureScale()
     result = do some stuff to get the scale
     return result
end

mytable = {
    ["scale"] = addon:FigureScale(),
}
Note that you have to set the values to mytable.scale again if your scale changes. Moreover this, mytable.scale is short for mytable["scale"]. Both work.
  Reply With Quote
10-23-10, 02:26 AM   #3
Grimsin
A Molten Giant
 
Grimsin's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2006
Posts: 990
Originally Posted by Luzzifus View Post
Code:
mytable = {
    ["scale"] = 1,
}

function addon:FigureScale()
     result = do some stuff to get the scale
     mytable.scale = result
end
or

Code:
function addon:FigureScale()
     result = do some stuff to get the scale
     return result
end

mytable = {
    ["scale"] = addon:FigureScale(),
}
Note that you have to set the values to mytable.scale again if your scale changes. Moreover this, mytable.scale is short for mytable["scale"]. Both work.
Hmm im going to have to play with this... i think the second method will work as long as the original code comes back a long and changes the scale = function in the table to an actual number later and does not permanently store the function.

The reason this is tricky is because what im doing is, i have made a default layout for Macaroon that fits GrimUI. What goes on is GrimUI will overwrite the Macaroon default setup table the first time they are loaded together. Then its designed so that anytime after the first time Macaroon controls all its own settings. The reason i need the added scale and possibly some other functions is to adjust the default overwrite based on a few things like the players resolution, i may get daring and attempt to make it load different layouts according to class but thats a ways down the line. i think the second method you listed would do the trick assuming Macaroons comes a long and overwrites that function in the table after the first load. Im to tired for that tonight though will check it out in the morning. Thanks!!
__________________
"Are we there yet?"

GrimUI
[SIGPIC][/SIGPIC]
  Reply With Quote
10-23-10, 10:55 AM   #4
Xubera
A Cobalt Mageweaver
 
Xubera's Avatar
AddOn Author - Click to view addons
Join Date: May 2009
Posts: 207
you can also make table variables functions

Code:
local function calcScale()
    --do some stuff to find scale
    return result
end


myTable = {
    ["scale"] = calcScale
}

print(myTable.scale()) --prints result
__________________
Chat Consolidate is the solution to any out of control trade chat. Ignore lines, throttle chat, consolidate posts!Follow the link to find out how!

▲ ▲ WoWInterface wont let me triforce >.>
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Using a function in a table as a variable?


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