Thread Tools Display Modes
04/29/14, 06:46 PM   #1
skyraker
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 154
Strings as function calls

I'm trying to convert the use of a string as a function call from the global namespace to calling local functions.

Lua Code:
  1. buttonname:SetHandler( "OnClicked" , function() _G[toggleFunction](buttonname) end)

How do I make this same call of toggleFunction without those functions being global? For example, toggleFunction ends up being ToggleLocation, but I have local function ToggleLocation?
  Reply With Quote
04/29/14, 08:02 PM   #2
Fathis Ules
Thief Guild Master
 
Fathis Ules's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 42
why dont you just use

Lua Code:
  1. buttonname:SetHandler( "OnClicked" , function() toggleFunction(buttonname) end)

toggleFunction could be local and your handler will still be called
  Reply With Quote
04/29/14, 08:36 PM   #3
skyraker
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 154
It still sees it as a string and not a function call.

I tried loadstring, but that didn't work either. Or I am using it wrong.

Lua Code:
  1. buttonname:SetHandler( "OnClicked" , function() loadstring(toggleFunction)(buttonname) end)
  Reply With Quote
04/29/14, 08:56 PM   #4
skyraker
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 154
Guess I should add more info to head off any questions.

1) When I use loadstring, instead of it complaining about using a string it complains about it being a null value
2) Yes, this function comes after the local declarations for all the toggle functions.
  Reply With Quote
04/29/14, 09:57 PM   #5
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
AddOn Author - Click to view addons
Join Date: Feb 2014
Posts: 648
Why aren't you just doing
Lua Code:
  1. buttonname:SetHandler("OnClicked", toggleFunction)
  Reply With Quote
04/30/14, 03:58 AM   #6
Fathis Ules
Thief Guild Master
 
Fathis Ules's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 42
seems you are trying to apply a method you have seen in another language but I doubt you will have chances with Lua, usually you write handler like seerah or i showed you, nothing much, forget strings =)
  Reply With Quote
04/30/14, 06:33 AM   #7
skyraker
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 154
The problem is that toggleFunction takes a value from a list and appends it to "Toggle". So then I get "ToggleLocation" on one iteration, "ToggleFPS" on another, and so on. That string was then being used for setting the button handlers to call functions with those names.

It works fine, if the functions are in or descended from the global namespace, but it hates it when they are local functions. I've seen plenty of examples online of how to accomplish it using loadstring, but I've gone through just about every method of using it and cannot get it to work.

For now, I'll just put the functions back into the global namespace. I'd rather have a working product right now until I can get it right.
  Reply With Quote
04/30/14, 07:05 AM   #8
Harven
 
Harven's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 135
So the toggleFunction is something like this? :
Lua Code:
  1. toggleFunction = "local arg = (select(1,...)) if arg == something then return 'ToggleLocation' elseif arg == somethingelse then return 'ToggleFPS' else return 'ToggleSomething' end"
  Reply With Quote
04/30/14, 09:47 AM   #9
Iyanga
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 183
Originally Posted by skyraker View Post
I'm trying to convert the use of a string as a function call from the global namespace to calling local functions.

Lua Code:
  1. buttonname:SetHandler( "OnClicked" , function() _G[toggleFunction](buttonname) end)

How do I make this same call of toggleFunction without those functions being global? For example, toggleFunction ends up being ToggleLocation, but I have local function ToggleLocation?

You don't. You need to use a table. Sorry.


local MyLocalFuncTable = {
["toggleFunction"] = ToggleLocation
}


MyLocalFuncTable[buttonname]()

When buttonname equals the string "toggleFunction", then ToggleLocation will be called.

(I didn't quite get what your locals and globals where, so I might have mixed up their names.)

Last edited by Iyanga : 04/30/14 at 09:50 AM.
  Reply With Quote
04/30/14, 11:55 AM   #10
skyraker
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 154
Originally Posted by Harven View Post
So the toggleFunction is something like this? :
Lua Code:
  1. toggleFunction = "local arg = (select(1,...)) if arg == something then return 'ToggleLocation' elseif arg == somethingelse then return 'ToggleFPS' else return 'ToggleSomething' end"
No. It is simply toggleFunction = "Toggle"..lblName

Originally Posted by Iyanga View Post
You don't. You need to use a table. Sorry.


local MyLocalFuncTable = {
["toggleFunction"] = ToggleLocation
}


MyLocalFuncTable[buttonname]()

When buttonname equals the string "toggleFunction", then ToggleLocation will be called.

(I didn't quite get what your locals and globals where, so I might have mixed up their names.)
Yeah, in the end I think I will need to go to some sort of global table if I want to keep the functions local. Not quite in this way if what I am reading is correct. I would still like to dynamically change what comes after Toggle on each iteration if I can help it because it keeps the code concise.
  Reply With Quote
04/30/14, 12:13 PM   #11
Garkin
 
Garkin's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 832
Originally Posted by skyraker View Post
I'm trying to convert the use of a string as a function call from the global namespace to calling local functions.

Lua Code:
  1. buttonname:SetHandler( "OnClicked" , function() _G[toggleFunction](buttonname) end)

How do I make this same call of toggleFunction without those functions being global? For example, toggleFunction ends up being ToggleLocation, but I have local function ToggleLocation?
I really wonder why do you not use Seerah's suggestion:
Originally Posted by Seerah View Post
Why aren't you just doing
Lua Code:
  1. buttonname:SetHandler("OnClicked", toggleFunction)
I'm not a programmer, but as far as I know handler will receive "self" (that is what you call "buttonname") as a default argument.

I you really want to use function(), I believe it should have defined argument:
Lua Code:
  1. buttonname:SetHandler( "OnClicked" , function(buttonname) toggleFunction(buttonname) end)
or because default argument is "self":
Lua Code:
  1. buttonname:SetHandler( "OnClicked" , function() toggleFunction(self) end)
Correct me if I am wrong.
  Reply With Quote
04/30/14, 12:30 PM   #12
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
AddOn Author - Click to view addons
Join Date: Feb 2014
Posts: 648
Originally Posted by skyraker View Post
No. It is simply toggleFunction = "Toggle"..lblName



Yeah, in the end I think I will need to go to some sort of global table if I want to keep the functions local. Not quite in this way if what I am reading is correct. I would still like to dynamically change what comes after Toggle on each iteration if I can help it because it keeps the code concise.
If this is the case, and your function is local (which I think you said above), then you cannot do it this way. You can't grab a local out of the global table (_G) because it's not global - you made it local.

Do what Iyanga said.
/edit: though I would probably make the table keys be your button names.
  Reply With Quote
04/30/14, 02:08 PM   #13
Iyanga
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 183
Originally Posted by skyraker View Post
I've seen plenty of examples online of how to accomplish it using loadstring, but I've gone through just about every method of using it and cannot get it to work.
That's unlikely, because it's not possible. loadstring is always executed in the global scope. You can neither reference local variables nor local functions inside the string of a loadstring() (unless you declare them within the loadstring itself).

i = 0
local i = 5
f = loadstring("i = i + 1")
f()
i = i - 1

The result will be:
i = 1
local i = 4
  Reply With Quote

ESOUI » Developer Discussions » Lua/XML Help » Strings as function calls

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