本文整理汇总了C#中Lua.RegisterFunction方法的典型用法代码示例。如果您正苦于以下问题:C# Lua.RegisterFunction方法的具体用法?C# Lua.RegisterFunction怎么用?C# Lua.RegisterFunction使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Lua
的用法示例。
在下文中一共展示了Lua.RegisterFunction方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CreateLuaVM
private Lua CreateLuaVM()
{
Lua l = new Lua();
Type t = GetType();
MethodInfo m = t.GetMethod("WriteLog");
l.RegisterFunction("Bot_PrintLog", this, m);
m = t.GetMethod("Connect");
l.RegisterFunction("Bot_Connect", this, m);
m = t.GetMethod("Disconnect");
l.RegisterFunction("Bot_Disconnect", this, m);
m = t.GetMethod("SendPacket");
l.RegisterFunction("Bot_SendPacket", this, m);
m = t.GetMethod("StartTimer");
l.RegisterFunction("Bot_StartTimer", this, m);
m = t.GetMethod("ResetTimer");
l.RegisterFunction("Bot_ResetTimer", this, m);
m = t.GetMethod("StopTimer");
l.RegisterFunction("Bot_StopTimer", this, m);
m = t.GetMethod("GetTick");
l.RegisterFunction("Bot_GetTick", this, m);
return l;
}
示例2: Main
/*
* Sample test script that shows some of the capabilities of
* LuaInterface
*/
public static void Main()
{
Console.WriteLine("Starting interpreter...");
var l = new Lua();
// Pause so we can connect with the debugger
// Thread.Sleep(30000);
Console.WriteLine("Reading test.lua file...");
l.DoFile("test.lua");
double width = l.GetNumber("width");
double height = l.GetNumber("height");
string message = l.GetString("message");
double color_r = l.GetNumber("color.r");
double color_g = l.GetNumber("color.g");
double color_b = l.GetNumber("color.b");
Console.WriteLine("Printing values of global variables width, height and message...");
Console.WriteLine("width: " + width);
Console.WriteLine("height: " + height);
Console.WriteLine("message: " + message);
Console.WriteLine("Printing values of the 'color' table's fields...");
Console.WriteLine("color.r: " + color_r);
Console.WriteLine("color.g: " + color_g);
Console.WriteLine("color.b: " + color_b);
width = 150;
Console.WriteLine("Changing width's value and calling Lua function print to show it...");
l["width"] = width;
l.GetFunction("print").Call(width);
message = "LuaNet Interface Test";
Console.WriteLine("Changing message's value and calling Lua function print to show it...");
l["message"] = message;
l.GetFunction("print").Call(message);
color_r = 30;
color_g = 10;
color_b = 200;
Console.WriteLine("Changing color's fields' values and calling Lua function print to show it...");
l["color.r"] = color_r;
l["color.g"] = color_g;
l["color.b"] = color_b;
l.DoString("print(color.r,color.g,color.b)");
Console.WriteLine("Printing values of the tree table's fields...");
double leaf1 = l.GetNumber("tree.branch1.leaf1");
string leaf2 = l.GetString("tree.branch1.leaf2");
string leaf3 = l.GetString("tree.leaf3");
Console.WriteLine("leaf1: " + leaf1);
Console.WriteLine("leaf2: " + leaf2);
Console.WriteLine("leaf3: " + leaf3);
leaf1 = 30; leaf2 = "new leaf2";
Console.WriteLine("Changing tree's fields' values and calling Lua function print to show it...");
l["tree.branch1.leaf1"] = leaf1; l["tree.branch1.leaf2"] = leaf2;
l.DoString("print(tree.branch1.leaf1,tree.branch1.leaf2)");
Console.WriteLine("Returning values from Lua with 'return'...");
object[] vals = l.DoString("return 2,3");
Console.WriteLine("Returned: " + vals[0] + " and " + vals[1]);
Console.WriteLine("Calling a Lua function that returns multiple values...");
object[] vals1 = l.GetFunction("func").Call(2, 3);
Console.WriteLine("Returned: " + vals1[0] + " and " + vals1[1]);
Console.WriteLine("Creating a table and filling it from C#...");
l.NewTable("tab");
l.NewTable("tab.tab");
l["tab.a"] = "a!";
l["tab.b"] = 5.5;
l["tab.tab.c"] = 6.5;
l.DoString("print(tab.a,tab.b,tab.tab.c)");
Console.WriteLine("Setting a table as another table's field...");
l["tab.a"] = l["tab.tab"];
l.DoString("print(tab.a.c)");
Console.WriteLine("Registering a C# static method and calling it from Lua...");
// Pause so we can connect with the debugger
// Thread.Sleep(30000);
l.RegisterFunction("func1", null, typeof(TestLuaInterface).GetMethod("func"));
vals1 = l.GetFunction("func1").Call(2, 3);
Console.WriteLine("Returned: " + vals1[0]);
TestLuaInterface obj = new TestLuaInterface();
Console.WriteLine("Registering a C# instance method and calling it from Lua...");
l.RegisterFunction("func2", obj, typeof(TestLuaInterface).GetMethod("funcInstance"));
vals1 = l.GetFunction("func2").Call(2, 3);
Console.WriteLine("Returned: " + vals1[0]);
Console.WriteLine("Testing throwing an exception...");
obj.ThrowUncaughtException();
Console.WriteLine("Testing catching an exception...");
obj.ThrowException();
Console.WriteLine("Testing inheriting a method from Lua...");
obj.LuaTableInheritedMethod();
Console.WriteLine("Testing overriding a C# method with Lua...");
obj.LuaTableOverridedMethod();
Console.WriteLine("Stress test RegisterFunction (based on a reported bug)..");
obj.RegisterFunctionStressTest();
Console.WriteLine("Test structures...");
obj.TestStructs();
//.........这里部分代码省略.........
示例3: TestThreading
public void TestThreading()
{
using (Lua lua = new Lua())
{
DoWorkClass doWork = new DoWorkClass();
lua.RegisterFunction("dowork", doWork, typeof(DoWorkClass).GetMethod("DoWork"));
bool failureDetected = false;
int completed = 0;
int iterations = 500;
for (int i = 0; i < iterations; i++)
{
ThreadPool.QueueUserWorkItem(new WaitCallback(delegate(object o)
{
try
{
lua.DoString("dowork()");
}
catch
{
failureDetected = true;
}
completed++;
}));
}
while (completed < iterations && !failureDetected)
Thread.Sleep(50);
Assert.False(failureDetected);
}
}
示例4: TestRegisterFunction
public void TestRegisterFunction()
{
using (Lua lua = new Lua())
{
lua.RegisterFunction("func1", null, typeof(TestClass2).GetMethod("func"));
object[] vals1 = lua.GetFunction("func1").Call(2, 3);
Assert.Equal(5.0f, Convert.ToSingle(vals1[0]));
TestClass2 obj = new TestClass2();
lua.RegisterFunction("func2", obj, typeof(TestClass2).GetMethod("funcInstance"));
vals1 = lua.GetFunction("func2").Call(2, 3);
Assert.Equal(5.0f, Convert.ToSingle(vals1[0]));
}
}
示例5: TestGenerics
public void TestGenerics()
{
//Im not sure support for generic classes is possible to implement, see: http://msdn.microsoft.com/en-us/library/system.reflection.methodinfo.containsgenericparameters.aspx
//specifically the line that says: "If the ContainsGenericParameters property returns true, the method cannot be invoked"
//TestClassGeneric<string> genericClass = new TestClassGeneric<string>();
//lua.RegisterFunction("genericMethod", genericClass, typeof(TestClassGeneric<>).GetMethod("GenericMethod"));
//lua.RegisterFunction("regularMethod", genericClass, typeof(TestClassGeneric<>).GetMethod("RegularMethod"));
using (Lua lua = new Lua())
{
TestClassWithGenericMethod classWithGenericMethod = new TestClassWithGenericMethod();
lua.RegisterFunction("genericMethod2", classWithGenericMethod, typeof(TestClassWithGenericMethod).GetMethod("GenericMethod"));
try
{
lua.DoString("genericMethod2(100)");
}
catch { }
Assert.True(classWithGenericMethod.GenericMethodSuccess);
Assert.True(classWithGenericMethod.Validate<double>(100)); //note the gotcha: numbers are all being passed to generic methods as doubles
try
{
lua.DoString("luanet.load_assembly('LuaInterface.Test')");
lua.DoString("TestClass=luanet.import_type('LuaInterface.Test.Mock.TestClass')");
lua.DoString("test=TestClass(56)");
lua.DoString("genericMethod2(test)");
}
catch { }
Assert.True(classWithGenericMethod.GenericMethodSuccess);
Assert.Equal(56, (classWithGenericMethod.PassedValue as TestClass).val);
}
}
示例6: TestFunctions
public void TestFunctions()
{
using (Lua lua = new Lua())
{
lua.DoString("luanet.load_assembly('mscorlib')");
lua.DoString("luanet.load_assembly('LuaInterface.Test')");
lua.RegisterFunction("p", null, typeof(System.Console).GetMethod("WriteLine", new Type[] { typeof(String) }));
/// Lua command that works (prints to console)
lua.DoString("p('Foo')");
/// Yet this works...
lua.DoString("string.gsub('some string', '(%w+)', function(s) p(s) end)");
/// This fails if you don't fix Lua5.1 lstrlib.c/add_value to treat LUA_TUSERDATA the same as LUA_FUNCTION
lua.DoString("string.gsub('some string', '(%w+)', p)");
}
}
示例7: TestEventException
public void TestEventException()
{
using (Lua lua = new Lua())
{
//Register a C# function
MethodInfo testException = this.GetType().GetMethod("_TestException", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Instance, null, new Type[] { typeof(float), typeof(float) }, null);
lua.RegisterFunction("Multiply", this, testException);
//create the lua event handler code for the entity
//includes the bad code!
lua.DoString("function OnClick(sender, eventArgs)\r\n" +
"--Multiply expects 2 floats, but instead receives 2 strings\r\n" +
"Multiply(asd, we)\r\n" +
"end");
//create the lua event handler code for the entity
//good code
//lua.DoString("function OnClick(sender, eventArgs)\r\n" +
// "--Multiply expects 2 floats\r\n" +
// "Multiply(2, 50)\r\n" +
// "end");
//Create the event handler script
lua.DoString("function SubscribeEntity(e)\r\ne.Clicked:Add(OnClick)\r\nend");
//Create the entity object
Entity entity = new Entity();
//Register the entity object with the event handler inside lua
LuaFunction lf = lua.GetFunction("SubscribeEntity");
lf.Call(new object[1] { entity });
try
{
//Cause the event to be fired
entity.Click();
//failed
Assert.True(false);
}
catch (LuaException e)
{
//passed
Assert.True(true);
}
}
}
示例8: RegisterFunctionStressTest
public void RegisterFunctionStressTest()
{
LuaFunction fc = null;
const int Count = 200; // it seems to work with 41
using (Lua lua = new Lua())
{
MyClass t = new MyClass();
for (int i = 1; i < Count - 1; ++i)
{
fc = lua.RegisterFunction("func" + i, t, typeof(MyClass).GetMethod("Func1"));
}
fc = lua.RegisterFunction("func" + (Count - 1), t, typeof(MyClass).GetMethod("Func1"));
lua.DoString("print(func1())");
}
}