|
Lua的确是一个很有意思的语言,尤其是交互是如此的方便,在系统中潜入一个Lua也不会消耗多少资源,反而可以获得很大的灵活性.的确很有意思.
Lua在C#的移植使用的LuaInterface这个产品
在互联网上也有不少文章介绍如何在.net环境中使用Lua的,不过对于我们新手来说,可能有时候不是很容易看得清楚.
Lua相当于一个解释器,你在这个解释器中注_册了一个函数,即可在一个外部文件中调用其中的函数
最简单而粗暴的写法
using System;
using System.Collections.Generic;
using System.Text;
using LuaInterface;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Program program = new Program();
Lua lua = new Lua();
//Register our C# functions
lua.RegisterFunction("DanSays", program, program.GetType().GetMethod("DanSays"));
lua.RegisterFunction("ThorSays", program, program.GetType().GetMethod("ThorSays"));
lua.RegisterFunction("TestFun", program, program.GetType().GetMethod("TestFun"));
lua.RegisterFunction("ReturnInt", program, program.GetType().GetMethod("ReturnInt"));
lua.DoString("DanSays('Hello'); ThorSays('Hi! Dan')");
lua.DoString("print('100asdf');");
lua.DoFile("scripts/Thursdays.lua");
Console.ReadLine();
Console.ReadLine();
Console.ReadLine();
}
public void DanSays(string s)
{
Console.WriteLine("Dan>" + s);
}
public void ThorSays(string s)
{
Console.WriteLine("Thor>" + s);
}
public void TestFun(Int32 aint)
{
Console.WriteLine("this is lua example, Number:" + aint.ToString());
}
public Int32 ReturnInt()
{
Console.WriteLine("Return Int");
return 110;
}
}
}
这种方式使用手工的方式注_册函数名
值得主意的是,在lua的系统中”注_册用的函数名”并不代表”对应的函数”就是一样的名称,两者是没有必然的联系的.
上面是参照 http://www.godpatterns.com/2006/05/scripting-with-lua-in-c.html 而弄出来的
然后有人就觉得不方便了 于是有了如下文章
http://blog.csdn.net/rcfalcon/article/details/5583095
实际上呢,
这是 www.gamedev.net/page/resources/_/reference/programming/sweet-snippets/using-lua-with-c-r2275
的简化版和弱化版
如果你按照 gamedev中的步骤做下去的话,会突然发现,编译错误.作为新手来说,肯定很头痛吧
01 //注_册函数
02 public static void registerLuaFunctions(Object pTarget)
03 {
04 // Sanity checks
05 if (pLuaVM == null || pLuaFuncs == null)
06 return;
07
08 // Get the target type
09 Type pTrgType = pTarget.GetType();
10
11 // ... and simply iterate through all it's methods
12 foreach (MethodInfo mInfo in pTrgType.GetMethods())
13 {
14 // ... then through all this method's attributes
15 foreach (Attribute attr in Attribute.GetCustomAttributes(mInfo))
16 {
17 // and if they happen to be one of our AttrLuaFunc attributes
18 if (attr.GetType() == typeof(AttrLuaFunc))
19 {//这样的判断较为严谨杜绝了,属性基类的标注的时候,不一样的描述
20 AttrLuaFunc pAttr = (AttrLuaFunc)attr;
21 Hashtable pParams = new Hashtable();
22
23 // Get the desired function name and doc string, along with parameter info
24 String strFName = pAttr.getFuncName();//获取属性中描述的 函数名
25 String strFDoc = pAttr.getFuncDoc();//函数描述
26 String[] pPrmDocs = pAttr.getFuncParams();//参数描述
27
28 // Now get the expected parameters from the MethodInfo object
29 ParameterInfo[] pPrmInfo = mInfo.GetParameters();//参数表
30
31 // If they don't match, someone forgot to add some documentation to the
32 // attribute, complain and go to the next method
33 if (pPrmDocs != null && (pPrmInfo.Length != pPrmDocs.Length))
34 {
35 Console.WriteLine("Function " + mInfo.Name + " (exported as " +
36 strFName + ") argument number mismatch. Declared " +
37 pPrmDocs.Length + " but requires " +
38 pPrmInfo.Length + ".");
39 break;
40 }
41 ArrayList paras = new ArrayList();
42 ArrayList paradocs = new ArrayList();
43 // Build a parameter <-> parameter doc hashtable
44 for (int i = 0; i < pPrmInfo.Length; i++)
45 {
46 pParams.Add(pPrmInfo.Name, pPrmDocs);
47 paras.Add(pPrmInfo.Name);
48 paradocs.Add(pPrmDocs);
49 }
50
51
52 // Get a new function descriptor from this information
53 //LuaFuncDescriptor pDesc = new LuaFuncDescriptor(strFName, strFDoc, pParams, pPrmDocs);
54 LuaFuncDescriptor pDesc = new LuaFuncDescriptor(strFName, strFDoc, paras, paradocs);
55
56 // Add it to the global hashtable
57 pLuaFuncs.Add(strFName, pDesc);
58
59 // And tell the VM to register it.
60 pLuaVM.RegisterFunction(strFName, pTarget, mInfo);
61 // pLuaVM.RegisterFunction(strFName+1, pTarget, mInfo);
62 //说明注_册函数的时候,函数名并不重要,只要mInfo正确即可,就会将一个函数名指向他
63 // 这么多工作也只是为了 获取想要的函数名而已,并不是必须的,
64 // 这样的有利于自动注_册函数,而不用手动.
65 // 代码想要修改才能使用
66 }
67 }
68 }
69 }
这样子就不会有错误了
gamedev中的自动抽取类中有描述的函数,然后注_册到lua解释器中,还能根据函数名来对对应的函数做出说明.
可以在一个帮助系统中使用这部分工作 |
阿莫论坛20周年了!感谢大家的支持与爱护!!
一只鸟敢站在脆弱的枝条上歇脚,它依仗的不是枝条不会断,而是自己有翅膀,会飞。
|