因此,我一直在开发一个简单的游戏引擎,使用SFML.Net作为图形和其他东西,使用NLua编写游戏脚本。因此,我在我的BaseGame -class中有这个方法,它应该运行Lua脚本,并向Lua端添加一些对象和方法等。我有一个try/catch块来捕获任何异常。
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的内容
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#方法没有打印出“成功!”,只打印“正在做的事情”,而且我也不知道为什么什么都没有发生。它也不是一个例外。这是怎么回事我怎么解决这个问题?
发布于 2016-11-10 15:28:12
也就是说,应该修复您的问题(并最终删除):
bool success = true;
try
{
LuaState.DoFile("main.lua");
}
catch (NLua.Exceptions.LuaScriptException e)
{
success = false;
Console.WriteLine(e.ToString());
}https://stackoverflow.com/questions/40530634
复制相似问题