Reputation: 423
I'm very new to lua this is my first program to use it at all. The code uses a Lua File to create a menu for a game I'm working on.
This line creates a button in the MainMenu screen, it sends the name of the function to call when the button is pressed.
Lua:
CreateButton( "mainmenu", "NewGame", "New Game", 50, 120, 150, 32)
function NewGame()
--CODE TO START NEW GAME
end
C#:
public class LuaWrapper
{
public void Initialize()
{
lua = new Lua();
lua.RegisterFunction("CreateButton", this,
this.GetType().GetMethod("CreateButton"));
lua.DoFile("Data/Menus/Main.lua");
}
public void CreateButton(string window, string functionName, string text,
float posx, float posy, float width, float height)
{
ButtonControl button = new ButtonControl();
button.Bounds = new UniRectangle(posx, posy, width, height);
button.Text = text;
LuaFunction f = lua.GetFunction(functionName); // <-- RETURNS NULL ?
ButtonPressEvent pressEvent = new ButtonPressEvent(f);
button.Pressed += new EventHandler(pressEvent.button_Pressed);
WindowDictionary[window].Children.Add(button);
}
}
class ButtonPressEvent
{
LuaFunction function;
public ButtonPress(LuaFunction function)
{
this.function = function;
}
public void button_Pressed(object sender, EventArgs e)
{
function.Call();
}
}
It all works fine until the button is clicked and it tries to call the associated function It throws a Null Reference Exception on Funtion.Call(); Under the ButtonPress Class. I have used break points and found out that the problem is
LuaFunction f = lua.GetFunction(functionName);
returns Null.
Note: I've also tried
LuaFunction f = lua.GetFunction("NewGame");
With the same results.
Thanks Reading, hope any one can help point out what I've done wrong.
Upvotes: 0
Views: 860
Reputation: 423
I had a thought and tried changing the order of the Lua File. I guess I was calling the creation of the button before The function had been read ? I'm not sure but this seems to have fixed it.
Lua:
function NewGame()
--CODE TO START NEW GAME
end
CreateButton( "mainmenu", "NewGame", "New Game", 50, 120, 150, 32)
Upvotes: 1