Variable function calling.

Monkey Forums/Monkey Programming/Variable function calling.

Rhodesy(Posted 2012) [#1]
Does anyone know of a way of calling a function by name stored in a string? ie, "myFunction"() ?

Or even something like CallFunction("string") ?

I've a suspicion it's not possible, but wanted to clarify.


Rhodesy(Posted 2012) [#2]
I think I might have answered my own question while looking into this more, I believe I can get to where I want using the reflection mod, with FunctionInfo / ModuleInfo, looks like there is a useful "Invoke" method.


Samah(Posted 2012) [#3]
[monkeycode]Import reflection

Function Main:Int()
GetFunction("Test", []).Invoke([])
Return 0
End

Function Test:Void()
Print "test"
End
[/monkeycode]
Try that. You'll need to know what the argument types are, and pass those in when you invoke it. Alternatively you could get a list of ALL functions and iterate through until you find the one you want (which lets you see all overloads).