Lua与CSharp间的通信

    技术2025-04-07  26

    准备工作      

    C#中使用Lua脚本,需要用到LuainterfaceLuainterfaceLuaC#封装,它是一个开源的项目,SVN地址为:http://luainterface.googlecode.com/svn/trunk

    C#代码中调用Lua函数

    Lua代码(保存为路径c:/test.lua),定义了函数add

    function add(num1,num2)

        return num1+num2;

    end;

    C#代码,调用在test.lua中定义的函数add

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    using LuaInterface;

     

    namespace CSharpCallLuaFunction

    {

        class Program

        {

            static void Main(string[] args)

            {

                Lua luaVM = new Lua();

                luaVM.DoFile(@"c:/test.lua");

                LuaFunction luaFunc = luaVM.GetFunction("add");

                if (luaFunc != null)

                {

                    object[] objArr = luaFunc.Call(2, 2);

                    Console.WriteLine(int.Parse(objArr[0].ToString()));

                }

            }

        }

    }

    运行后的输出结果为:

    4

    总结下,C#中调用Lua脚本的一般步骤是:

    1. 生成Lua对象。

    Lua luaVM = new Lua();

    2. 运行脚本。

    luaVM.DoFile(@"c:/test.lua");

    3. 获得脚本中定义的函数。

    LuaFunction luaFunc = luaVM.GetFunction("add");

    4. 调用函数。

    object[] objArr = luaFunc.Call(2, 2);

    要注意一点,从Lua脚本获得函数,要检验得到的不为null再调用。

    Lua中调用C#的函数

    C#代码,定义了方法GetName

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

     

    namespace CSharpCallLuaFunction

    {

        public class FuncLibrary

        {

            public string GetName()

            {

                return "yangjun";

            }

        }

    }

    C#代码,在Lua脚本中注册方法GetName

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    using LuaInterface;

     

    namespace CSharpCallLuaFunction

    {

        class Program

        {

            static void Main(string[] args)

            {

                FuncLibrary lib = new FuncLibrary();

                Type tThis = lib.GetType();

                Lua luaVM = new Lua();

                luaVM.RegisterFunction("Name", lib, tThis.GetMethod("GetName"));

                luaVM.DoFile(@"c:/test.lua");

            }

        }

    }

    C#代码向Lua脚本注册方法使用:

    luaVM.RegisterFunction("Name", lib, tThis.GetMethod("GetName"));

    方法RegisterFunction的原型为:

    ///

    /// Lua脚本注册函数

    ///

    /// Lua脚本中使用的函数名称

    /// 注册方法的对象实例

    /// 调用target.GetType().GetMethod("Method")获得

    /// 失败返回null

    public LuaFunction RegisterFunction(string path, object target, MethodBase function)

    用于测试的Lua脚本(保存为c:/test.lua):

    print(Name());

    运行后输出的结果为:

    yangjun

    总结下,Lua脚本调用C#定义方法,步骤如下:

    1. 生成类的实例

    FuncLibrary lib = new FuncLibrary();

    2. 获得实例的类型信

    Type tThis = lib.GetType();

    3. 生成Lua对象

    Lua luaVM = new Lua();

    4. 注册方法

    luaVM.RegisterFunction("Name", lib, tThis.GetMethod("GetName"));

    5. 运行脚本

    luaVM.DoFile(@"c:/test.lua");

     

    最新回复(0)