そこで私は、SFML.Net を使用してグラフィックスなどを処理し、NLua をゲームのスクリプト作成に使用する単純なゲーム エンジンに取り組んできました。そのため、Lua スクリプトを実行し、いくつかのオブジェクトやメソッドなどを Lua 側に追加することになっている BaseGame クラスにこのメソッドがあります。例外をキャプチャするための 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 ファイルの内容を以下に示します。
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# メソッドは「成功!」を出力せず、「実行中」のみを出力し、何も起こらない理由がわかりません。例外も出力されません。ここで何が起こっているので、どうすれば修正できますか?