0
\$\begingroup\$

My question is very simple. I'm using MoonSharp to add Lua scripting to my game. One use case involves calling a static artificial intelligence function from a static class I've implemented in C#. I know you can easily call static functions using MoonSharp, but can those functions be called if the class is static?

I haven't been able to find an answer so far through Google or on this site. My own experimentation with code hasn't been successful either. There are pretty simple workarounds for the problem I'm asking about (like making the AI class not static), but I'd prefer to avoid them.

Thank you :)

\$\endgroup\$
1
\$\begingroup\$

One soulution could be creating a static callback function that in turn calls the function on the staic c# class

so for exmple

private staic void AiCallback(int anArgument)
{
   AiClass.DoAi(anArgument);
}

private static void RegisterAiFunction(Script aLuaScript)
{
   aluaScript.Globals["DoAi"] = (func<void, int>)AiCallback;
}

private static void DoLua(string someLuaCode)
{
   Script script = new Script();
   script.DoString(someLuacode);
}

where AiClass would be your static class

\$\endgroup\$
  • \$\begingroup\$ Thanks for the response :) Still not my ideal solution, but that'll work fine as a wrapper for the static class. \$\endgroup\$ – Grimelios Sep 26 '16 at 21:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.