helloburp
|
  |
| Joined: 26 Aug 2011 |
| Total Posts: 14376 |
|
|
| 27 Jun 2017 04:42 AM |
| like say I have a user input thing in which players enter the name of a function. Implying this is sent to a script with functions of the same name defined, how could I fire a function with the name of the string without using if statements? |
|
|
| Report Abuse |
|
|
|
| 27 Jun 2017 04:44 AM |
Use a table.
local functions = {test = function() print("Test") end}
chat = "test" functions[chat]()
|
|
|
| Report Abuse |
|
|
helloburp
|
  |
| Joined: 26 Aug 2011 |
| Total Posts: 14376 |
|
|
| 27 Jun 2017 04:46 AM |
| I'd like to do it without any external things, only the string and the plugin. Like even if I made a new function and didn't enter it into some table or define it in any other way, I could still run it with just the string being parsed. |
|
|
| Report Abuse |
|
|
helloburp
|
  |
| Joined: 26 Aug 2011 |
| Total Posts: 14376 |
|
|
| 27 Jun 2017 04:47 AM |
| Although if I need to use a table I can live with it, lol |
|
|
| Report Abuse |
|
|
|
| 27 Jun 2017 04:48 AM |
So you aren't using it to call a function, you're using it to create a function?
I (And everyone else) highly recommend against it. It's not a good practice, and should never ever be necessary. Ever.
It is called 'loadstring' and shouldn't be used, it can only be used in a Script and only if LoadstringEnabled is on.
|
|
|
| Report Abuse |
|
|
|
| 27 Jun 2017 04:49 AM |
For example
chat = "print('Hello, world!')" newFunction = loadstring(chat) newFunction()
|
|
|
| Report Abuse |
|
|
helloburp
|
  |
| Joined: 26 Aug 2011 |
| Total Posts: 14376 |
|
|
| 27 Jun 2017 04:53 AM |
No, I am implying that there are already functions and that a string that is the same as the function's name would call it
function potato()
end
input = "potato"
--call function that matches input, so call potato
--???
--profit
but tables seem to make sense for this, if there is a better way then let me know. And I am aware of the dangers of the class that must not be named |
|
|
| Report Abuse |
|
|
|
| 27 Jun 2017 04:57 AM |
There is one other way then.
If you don't use local variables and functions, you can use getfenv.
function potato()
end
-- All your functions above this line fenv = getfenv() -- 'fenv' stands for function environment -- fenv holds all of the global variables in your code.
input = "potato" f = fenv[input] -- get the function if type(f) == "function" then -- So we don't try to call something that isn't a function f() end
Basically it's the same thing, but the table is generated automatically (Or whatever, the table you use is the script's global environment. Ykwim)
|
|
|
| Report Abuse |
|
|
|
| 27 Jun 2017 04:58 AM |
function exe(str,...) if getfenv()[str] then return getfenv()[str](...) end end
exe'warn' |
|
|
| Report Abuse |
|
|
helloburp
|
  |
| Joined: 26 Aug 2011 |
| Total Posts: 14376 |
|
|
| 27 Jun 2017 04:59 AM |
| Awesome, that's much cleaner and exactly what I was after. Thanks |
|
|
| Report Abuse |
|
|
|
| 27 Jun 2017 05:03 AM |
If you're making a plugin you can also use this nifty doodad.
local pluginFunctions = {} function pluginFunctions.selectRandom() -- example function local children = game.Selection:Get()[1]:GetChildren() game.Selection:Set(children[math.random(#children)]) end
function _G.initialize() for i, v in pairs(pluginFunctions) do getfenv(2)[i] = v end end
Now, when you call '_G.initialize' from the command bar, all of the functions in pluginFunctions become default functions in the command bar:
_G.initialize() selectRandom() -- ololol no error
|
|
|
| Report Abuse |
|
|
Exeplex
|
  |
| Joined: 29 May 2008 |
| Total Posts: 1081 |
|
|
| 27 Jun 2017 05:15 AM |
I use a protected call and hand it whatever is being chatted.
local function RunFunction(FunctionToRun) local Success = true; local Func = nil; Success = pcall(function() Func = FunctionToRun() end); if Success then print("Function ran successfully!"); else print("Function has encountered an error."); end return Success; end
RunFunction(function() print("Hey") end);
|
|
|
| Report Abuse |
|
|
Exeplex
|
  |
| Joined: 29 May 2008 |
| Total Posts: 1081 |
|
|
| 27 Jun 2017 05:17 AM |
| But this isn't fully related to the post, sorry! |
|
|
| Report Abuse |
|
|