moonsharp icon indicating copy to clipboard operation
moonsharp copied to clipboard

[Help] Doubt about using a moon function as a parameter

Open brutalzinn opened this issue 4 years ago • 1 comments

how do I pass a moon function as a parameter :?

I'm trying with

--lua
function ButtonDown ()

print ('tste')

end

core.creator_button ("Abacatao ITEM", ButtonDown, "print ('test')", "Deck")



C#

Environment.Call(LuaExecuteButtonDown); I want to pass this function as a parameter, store it in a variable and execute it later. It's possible?

brutalzinn avatar May 09 '20 14:05 brutalzinn

You can get the function using script.Globals.Get(functionName) where script is your Lua Script instance from MoonSharp. It will return a DynValue. Then just execute it using script.Call(fn). If you want to store a c# method call to execute later there's plenty of ways, perhaps one of the simplest is to store it as an Action:

Action myFunctionCall = () => Environment.Call(LuaExecuteButtonDown);
// ...
myFunction.Invoke();

tmenezes avatar May 24 '20 15:05 tmenezes