Lua In C#
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中的步骤做下去的话,会突然发现,编译错误.作为新手来说,肯定很头痛吧
//注册函数
public static void registerLuaFunctions(Object pTarget)
{
// Sanity checks
if (pLuaVM == null || pLuaFuncs == null)
return;
// Get the target type
Type pTrgType = pTarget.GetType();
// ... and simply iterate through all it's methods
foreach (MethodInfo mInfo in pTrgType.GetMethods())
{
// ... then through all this method's attributes
foreach (Attribute attr in Attribute.GetCustomAttributes(mInfo))
{
// and if they happen to be one of our AttrLuaFunc attributes
if (attr.GetType() == typeof(AttrLuaFunc))
{//这样的判断较为严谨杜绝了,属性基类的标注的时候,不一样的描述
AttrLuaFunc pAttr = (AttrLuaFunc)attr;
Hashtable pParams = new Hashtable();
// Get the desired function name and doc string, along with parameter info
String strFName = pAttr.getFuncName();//获取属性中描述的 函数名
String strFDoc = pAttr.getFuncDoc();//函数描述
String[] pPrmDocs = pAttr.getFuncParams();//参数描述
// Now get the expected parameters from the MethodInfo object
ParameterInfo[] pPrmInfo = mInfo.GetParameters();//参数表
// If they don't match, someone forgot to add some documentation to the
// attribute, complain and go to the next method
if (pPrmDocs != null && (pPrmInfo.Length != pPrmDocs.Length))
{
Console.WriteLine("Function " + mInfo.Name + " (exported as " +
strFName + ") argument number mismatch. Declared " +
pPrmDocs.Length + " but requires " +
pPrmInfo.Length + ".");
break;
}
ArrayList paras = new ArrayList();
ArrayList paradocs = new ArrayList();
// Build a parameter <-> parameter doc hashtable
for (int i = 0; i < pPrmInfo.Length; i++)
{
pParams.Add(pPrmInfo[i].Name, pPrmDocs[i]);
paras.Add(pPrmInfo[i].Name);
paradocs.Add(pPrmDocs[i]);
}
// Get a new function descriptor from this information
//LuaFuncDescriptor pDesc = new LuaFuncDescriptor(strFName, strFDoc, pParams, pPrmDocs);
LuaFuncDescriptor pDesc = new LuaFuncDescriptor(strFName, strFDoc, paras, paradocs);
// Add it to the global hashtable
pLuaFuncs.Add(strFName, pDesc);
// And tell the VM to register it.
pLuaVM.RegisterFunction(strFName, pTarget, mInfo);
// pLuaVM.RegisterFunction(strFName+1, pTarget, mInfo);
//说明注册函数的时候,函数名并不重要,只要mInfo正确即可,就会将一个函数名指向他
// 这么多工作也只是为了 获取想要的函数名而已,并不是必须的,
// 这样的有利于自动注册函数,而不用手动.
// 代码想要修改才能使用
}
}
}
}这样子就不会有错误了
gamedev中的自动抽取类中有描述的函数,然后注册到lua解释器中,还能根据函数名来对对应的函数做出说明.
可以在一个帮助系统中使用这部分工作

浙公网安备 33010602011771号