Lua.GetFunction返回Null [英] Lua.GetFunction Returns Null

查看:381
本文介绍了Lua.GetFunction返回Null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对lua非常陌生,这是我第一个使用它的程序.该代码使用Lua文件为我正在开发的游戏创建菜单.

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.

此行在MainMenu屏幕中创建一个按钮,当按钮被按下时,它将发送要调用的函数的名称.

This line creates a button in the MainMenu screen, it sends the name of the function to call when the button is pressed.

卢阿:

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()上引发Null引用异常.在ButtonPress类下.我使用了断点,发现问题是

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

返回Null.

注意:我也尝试过

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

具有相同的结果.

感谢阅读,希望任何人都可以帮助指出我做错了什么.

Thanks Reading, hope any one can help point out what I've done wrong.

推荐答案

我有一个想法,尝试更改Lua文件的顺序.我想我在读取函数之前就在调用按钮的创建吗?我不确定,但这似乎已经解决了.

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.

卢阿:

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

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

这篇关于Lua.GetFunction返回Null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆