When i try to execute faulty lua code it does not only crash but completely ends the function execution that started the lua script.
Some notes to read the following code: prs() is Debug.Log(). prs("script finished"); is never called.
The Issue with this code
// script.Globals["getturnnumber"] = (Func<int>)getturnnumber;
is the faulty line. The error message then is because it cannot find the function:
ScriptRuntimeException: attempt to call a nil value
MoonSharp.Interpreter.Execution.VM.Processor.Internal_ExecCall (Int32 argsCount, Int32 instructionPtr, MoonSharp.Interpreter.CallbackFunction handler, MoonSharp.Interpreter.CallbackFunction continuation, Boolean thisCall, System.String debugText, MoonSharp.Interpreter.DynValue unwindHandler) (at Assets/Plugins/MoonSharp/Interpreter/Execution/VM/Processor/Processor_InstructionLoop.cs:763)
MoonSharp.Interpreter.Execution.VM.Processor.Processing_Loop (Int32 instructionPtr) (at Assets/Plugins/MoonSharp/Interpreter/Execution/VM/Processor/Processor_InstructionLoop.cs:115)
UnityEngine.EventSystems.EventSystem:Update()
My question is: Why will the function and the parent functions just stop executing? A little sidenote: This happens with ANY error i had not only this.
As example which i inserted for testing; Why is prs("script finished"); is not called
Here is what i mean:
public void evaltext(string scriptCode) {//this is the Core of the scripting engine it does the actual work.
prs("start script " + scriptfile + " scriptname " + scriptname+ " ## "+ scriptCode);
try
{
Script script = new Script(); //Generating the script instance
UserData.RegisterAssembly(); //registering the asseblys you need to add [MoonSharpUserData] to the data you want to expose more under http://www.moonsharp.org/objects.html
script.Globals["createunit"] = (Func<string, List<string>, objects>)createunit; //exposing a function so it can be called from lua syntax is: Func<parameters, returntype>
script.Globals["printerror"] = (Func<string[], string>)pre;
script.Globals["testadd"] = (Func<int>)debug.testadd;
script.Globals["getglobal"] = (Func<glo>)g.getglobal;
script.Globals["playeraddtech"] = (Func<string, bool>)playeraddtech;
script.Globals["playerhastech"] = (Func<string, bool>)playerhastech;
script.Globals["playeraddtag"] = (Func<string, bool>)playeraddtag;
script.Globals["playerhastag"] = (Func<string, int>)playerhastag;
script.Globals["getobject"] = (Func<int, objects>)g.getobj;
script.Globals["getobjectn"] = (Func<string, objects>)g.getobjn;
script.Globals["getmap"] = (Func<int, maps>)g.getmap;
// script.Globals["getturnnumber"] = (Func<int>)getturnnumber;
script.Globals["newscript"] = (Func<string, string, string, scripting>)newscript;
script.Globals["addresearch"] = (Func<string, string, string>)addresearch;
script.Globals["addtoinventory"] = (Func<string, string, string>)addtoinventory;
script.Globals["addturnevent"] = (Func<string, string, string, string>)addturnevent;
script.Globals["removeturnevent"] = (Func<string, string, string, string>)removeturnevent;
script.Globals["hasturnevent"] = (Func<string, string, string, bool>)hasturnevent;
script.DoString(scriptCode); //The command to load scriptCode as module more under http://www.moonsharp.org/scriptloaders.html
DynValue res = script.Call(script.Globals[scriptname]); //Calling the function inside the code you should define a default value here More about dynvalue http://www.moonsharp.org/dynvalue.html
}
catch (LuaException ex)
{
pre("[LuaException]" + ex.Message);
}
prs("script finished");
//scriptmain(scriptnamei);
//starts();
}
And the executed lua script:
function waves()
if getturnnumber()>0 then
if getturnnumber() % 2 == 0 then
printerror("Lua success");
end
end
end
A finally would come in handy here. as @shingo says you rethrow the exception. finally clause will get executed even if there is a throw, and even if there is no catch at all.
finally
{
prs("script finished");
}