ESOUI

ESOUI (https://www.esoui.com/forums/index.php)
-   Lua/XML Help (https://www.esoui.com/forums/forumdisplay.php?f=175)
-   -   Strings as function calls (https://www.esoui.com/forums/showthread.php?t=1271)

skyraker 04/29/14 06:46 PM

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?

Fathis Ules 04/29/14 08:02 PM

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

skyraker 04/29/14 08:36 PM

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)

skyraker 04/29/14 08:56 PM

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.

Seerah 04/29/14 09:57 PM

Why aren't you just doing
Lua Code:
  1. buttonname:SetHandler("OnClicked", toggleFunction)

Fathis Ules 04/30/14 03:58 AM

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

skyraker 04/30/14 06:33 AM

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.

Harven 04/30/14 07:05 AM

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"

Iyanga 04/30/14 09:47 AM

Quote:

Originally Posted by skyraker (Post 6397)
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.)

skyraker 04/30/14 11:55 AM

Quote:

Originally Posted by Harven (Post 6462)
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

Quote:

Originally Posted by Iyanga (Post 6473)
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.

Garkin 04/30/14 12:13 PM

Quote:

Originally Posted by skyraker (Post 6397)
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:
Quote:

Originally Posted by Seerah (Post 6415)
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.

Seerah 04/30/14 12:30 PM

Quote:

Originally Posted by skyraker (Post 6492)
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.

Iyanga 04/30/14 02:08 PM

Quote:

Originally Posted by skyraker (Post 6457)
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


All times are GMT -6. The time now is 06:39 AM.

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