0

So I've been working on a simple game engine using SFML.Net for the graphics and what not, and NLua for scripting of games. So I have this method in my BaseGame -class that is supposed to run a Lua script and add some objects and methods etc. to the Lua side. I have a try/catch block for capturing any exceptions.

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

If you're interested, here are the contents of the 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

Anyway, the C# method does NOT print out "Success!", only "Doing stuff", and I have no idea why nothing is happening. It's not printing out an exception either. So what is going on here and how do I fix this?

Ricochet
  • 11
  • 3

1 Answers1

1

That is should fix your problem (and remove finally):

    bool success = true;
    try
    {
        LuaState.DoFile("main.lua");
    }
    catch (NLua.Exceptions.LuaScriptException e)
    {
        success = false;
        Console.WriteLine(e.ToString());
    }
Jeron
  • 36
  • 4