lua错误结束c#函数和父函数执行

问题描述 投票:0回答:1

当我尝试执行错误的lua代码时,它不仅会崩溃,而且会完全结束启动lua脚本的函数执行。

阅读以下代码的一些注意事项:prs()是Debug.Log()。 prs(“脚本完成”);永远不会被称为。

这段代码的问题

// script.Globals [“getturnnumber”] =(Func)getturnnumber;是错误的路线。然后错误消息是因为它找不到该函数:

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()

我的问题是:为什么函数和父函数会停止执行?一个小小的旁注:这发生在任何错误我不仅如此。

作为我插入测试的例子;为什么是prs(“脚本完成”);不叫

这就是我的意思:

    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();
    }

和执行的lua脚本:

function waves()
if getturnnumber()>0 then
    if getturnnumber() % 2 == 0 then
        printerror("Lua success");
    end
end

结束

c# lua moonsharp
1个回答
0
投票

最后在这里会派上用场。正如@shingo所说你重新抛出异常。即使有一个抛出,finally子句也会执行,即使根本没有捕获。

finally
{
    prs("script  finished");
}
© www.soinside.com 2019 - 2024. All rights reserved.