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:
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? |
why dont you just use
Lua Code:
toggleFunction could be local and your handler will still be called |
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:
|
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. |
Why aren't you just doing
Lua Code:
|
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 =)
|
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. |
So the toggleFunction is something like this? :
Lua Code:
|
Quote:
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.) |
Quote:
Quote:
|
Quote:
Quote:
I you really want to use function(), I believe it should have defined argument: Lua Code:
Lua Code:
|
Quote:
Do what Iyanga said. /edit: though I would probably make the table keys be your button names. |
Quote:
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