本文整理汇总了C#中LuaState.GetGlobal方法的典型用法代码示例。如果您正苦于以下问题:C# LuaState.GetGlobal方法的具体用法?C# LuaState.GetGlobal怎么用?C# LuaState.GetGlobal使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类LuaState
的用法示例。
在下文中一共展示了LuaState.GetGlobal方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
static void Main(string[] args)
{
TestGC();
Class1 cl1 = new Class1();
LuaState state = new LuaState();
state.GetGlobals().Register("MyCallback", new LuaPlusCallback(MyCallback));
state.DoString( "MyVar = 5" );
// System.Reflection.MethodInfo info = typeof(Class1).GetMethod("Func2");
// state.GetGlobals().Register("Func", null, typeof(Class1).GetMethod("Func")); //typeof(Class1).GetMethod("Func"));
// state.DoString("Func(5)");
LuaObject obj = state.GetGlobal( "MyVar" );
state.DoString("MyCallback('Hello')");
LuaObject funcObj = state.GetGlobal("print");
// funcObj.Call("Stuff", 0);
try
{
// state.DoString( "print('Hello, world!')" );
}
catch (Exception e)
{
}
state.DoString( "MyTable = { 1, 2, [3] = { \"Hi\" } }" );
double num = obj.GetNumber();
LuaObject obj3 = state.GetGlobals()["MyTable"];
String s = obj3.TypeName();
LuaObject obj2 = new LuaObject(obj);
obj2.AssignWString(state, "Yo baby!");
state.DoString("MyTable = { 1, 2, 3, A = 5, 4 }");
foreach (LuaNode node in state.Globals["MyTable"])
{
Console.WriteLine(node);
}
try
{
LuaObject globObj = state.Globals["MyTable"][1][2];
}
catch (LuaException e)
{
int hi = 5;
}
System.IO.FileStream fileStream = new System.IO.FileStream("Out.lua", FileMode.Create);
LuaStateStreamOutFile outFile = new LuaStateStreamOutFile(fileStream);
state.DumpObject(outFile, "MyTable", state.Globals["MyTable"]);
}
示例2: TestCall
public void TestCall()
{
LuaState L = null;
using (L = new LuaState())
{
L.PushFunction(l => {
var a = L.ToNumber(1);
var b = L.ToNumber(2);
L.PushNumber(a * b);
return 1;
});
L.PushNumber(12);
L.PushNumber(4);
L.Call(2, 1);
Assert.Equal(1, L.GetTop());
Assert.Equal(48, L.ToNumber(-1));
L.DoString(@"
function testA(a,b)
return a-b
end
function testB(a,b)
DoAnError(a,b)
end
");
Assert.Equal(1, L.GetTop());
L.GetGlobal("testA");
L.PushNumber(12);
L.PushNumber(4);
L.Call(2, 1);
Assert.Equal(2, L.GetTop());
Assert.Equal(8, L.ToNumber(-1));
L.GetGlobal("testB");
L.PushNumber(12);
L.PushNumber(4);
var ex = Assert.Throws<LuaAtPanicException>(() => L.Call(2, 1));
Assert.Equal("[string \"\r...\"]:6: attempt to call a nil value (global 'DoAnError')", ex.Message);
}
}
示例3: TestSetGetGlobal
public void TestSetGetGlobal()
{
LuaState L = null;
using (L = new LuaState())
{
L.PushString("Value A");
L.SetGlobal("a");
Assert.Equal(0, L.GetTop());
L.GetGlobal("a");
Assert.Equal(1, L.GetTop());
Assert.Equal(LuaType.String, L.Type(-1));
L.GetGlobal("b");
Assert.Equal(2, L.GetTop());
Assert.Equal(LuaType.Nil, L.Type(-1));
}
}
示例4: TestRegister
public void TestRegister()
{
LuaState L = null;
using (L = new LuaState())
{
L.GetGlobal("a");
Assert.Equal(1, L.GetTop());
Assert.Equal(LuaType.Nil, L.Type(-1));
L.Register("a", (l) => {
l.PushNumber(12.34);
return 0;
});
L.GetGlobal("a");
Assert.Equal(2, L.GetTop());
Assert.Equal(LuaType.Function, L.Type(-1));
}
}
示例5: TestPCall
public void TestPCall()
{
LuaState L = null;
using (L = new LuaState())
{
L.PushFunction(l => {
var a = L.ToNumber(1);
var b = L.ToNumber(2);
L.PushNumber(a * b);
return 1;
});
L.PushNumber(12);
L.PushNumber(4);
Assert.Equal(LuaStatus.OK, L.PCall(2, 1, 0));
Assert.Equal(1, L.GetTop());
Assert.Equal(48, L.ToNumber(-1));
L.DoString(@"
function testA(a,b)
return a-b
end
function testB(a,b)
DoAnError(a,b)
end
");
Assert.Equal(1, L.GetTop());
L.GetGlobal("testA");
L.PushNumber(12);
L.PushNumber(4);
Assert.Equal(LuaStatus.OK, L.PCall(2, 1, 0));
Assert.Equal(2, L.GetTop());
Assert.Equal(8, L.ToNumber(-1));
L.GetGlobal("testB");
L.PushNumber(12);
L.PushNumber(4);
Assert.Equal(LuaStatus.ErrRun, L.PCall(2, 1, 0));
Assert.Equal("[string \"\r...\"]:6: attempt to call a nil value (global 'DoAnError')", L.ToString(-1));
}
}