首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >LuaScriptException在NLua中的处理

LuaScriptException在NLua中的处理
EN

Stack Overflow用户
提问于 2016-11-10 14:57:33
回答 1查看 535关注 0票数 0

因此,我一直在开发一个简单的游戏引擎,使用SFML.Net作为图形和其他东西,使用NLua编写游戏脚本。因此,我在我的BaseGame -class中有这个方法,它应该运行Lua脚本,并向Lua端添加一些对象和方法等。我有一个try/catch块来捕获任何异常。

代码语言:javascript
复制
        public bool Start(uint x = 800U, uint y = 600U)
    {
        LuaState = new Lua();
        GameTime = new Time();
        Window = RenderWindow.FromResolution(new Vector2u(x, y));
        Console.WriteLine(Directory.GetCurrentDirectory() + @"\main.lua");
        if (File.Exists("main.lua"))
        {
            Console.WriteLine("Doing stuff");
            //Import assembly and globals
            LuaState.LoadCLRPackage();
            LuaState.DoString(@" import ('Orakel')");
            LuaState["Window"] = Window;
            LuaState["GameTime"] = GameTime;

            //Sandbox the code:
            LuaState.DoString(@"import = function () end");

            //Load the actual Lua file
            bool success = true;
            try
            {
                LuaState.DoFile("main.lua");
            }
            catch (NLua.Exceptions.LuaScriptException e)
            {
                Console.WriteLine(e.ToString());
            }
            finally
            {
                success = false;
            }
            if (!success) { return false; }
            Console.WriteLine("Success!");
        }
        else
        {
            //TODO: Write a native message box or something
            DialogResult res = MessageBox.Show("main.lua not found in working directory!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            if (res == DialogResult.OK)
            {
                return false;
            }
        }
        return true;
    }

如果您感兴趣,下面是main.lua -file的内容

代码语言:javascript
复制
local Drawables = {}


--Runs on game start
function Begin()
    print("hooray")
end

--Runs every frame
function Update(delta)
    if UserInputService.IsKeyPressed(KeyCode.A) then
        print(delta)
    end
end

--Runs every frame
function Draw()

end

function Exit()
    print("exited")
end

无论如何,C#方法没有打印出“成功!”,只打印“正在做的事情”,而且我也不知道为什么什么都没有发生。它也不是一个例外。这是怎么回事我怎么解决这个问题?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-11-10 15:28:12

也就是说,应该修复您的问题(并最终删除):

代码语言:javascript
复制
    bool success = true;
    try
    {
        LuaState.DoFile("main.lua");
    }
    catch (NLua.Exceptions.LuaScriptException e)
    {
        success = false;
        Console.WriteLine(e.ToString());
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40530634

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档