2012-09-01 49 views
0

我對lua很新,這是我第一個使用它的程序。該代碼使用Lua文件爲我正在開發的遊戲創建菜單。Lua.GetFunction返回空

此行在MainMenu屏幕上創建一個按鈕,它發送要按下該按鈕時調用的函數的名稱。

的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(); 
    } 
} 

,直到按鈕被點擊並嘗試調用相關功能 它輕視Funtion.Call一個空引用異常這一切工作正常();在ButtonPress類下。我已經使用了斷點,並發現問題是

LuaFunction f = lua.GetFunction(functionName); 

返回空。

注:我也試過

LuaFunction f = lua.GetFunction("NewGame"); 

具有相同的結果。

謝謝閱讀,希望任何人都可以幫助指出我做錯了什麼。

+0

在Lua中,功能沒有得到,如果他們的代碼是無效的定義。你確定你的NewGame代碼是正確的嗎? –

+0

是的,謝謝,我只記得你發佈的內容,並解決了發生的其他問題:) – Dusty

回答

1

我有一個想法,並嘗試改變Lua文件的順序。我想我在函數被讀取之前調用了按鈕的創建?我不確定,但這似乎已修復它。

的Lua:

function NewGame() 
--CODE TO START NEW GAME 
end 

CreateButton("mainmenu", "NewGame", "New Game", 50, 120, 150, 32)