0

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

kristianp
  • 5,496
  • 37
  • 56
Codani
  • 13
  • 5

1 Answers1

0

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");
}
kristianp
  • 5,496
  • 37
  • 56
  • Hey, i rewrote the question a little to make the question more understandable: The point is not that that line is not executed it is that the whole callstack stops there and the parent function is not executed aswell. – Codani Mar 13 '19 at 06:23
  • You've changed the code in the question too. Did you retest it? – kristianp Mar 13 '19 at 06:29
  • Yes i did test it before i changed the code and i did retest it now: Special: start script ScriptRuntimeException: attempt to call a nil value After this nothing more comes... also i like to note originally i had no try catch block and it had the same issue so i added the try catch. – Codani Mar 14 '19 at 06:35
  • ScriptRuntimeException isn't caught in your function, because you're not catching it and not catching Exception. The exception will continue to be thrown up to calling functions on the stack, until it is caught. – kristianp Mar 14 '19 at 11:45
  • Can you edit the question to include the error message too please? – kristianp Mar 14 '19 at 11:46
  • I added some more informations about the error which i found after some try and error. However the code execution stop does happen with every error i encountered yet (i checked it after asking) so there is little to see with that error and i think it maybe the default behaviour of moonsharp however i am not really sure. – Codani Mar 14 '19 at 13:16