Objective
Moonsharp is a class library that supports C # calling Lua scripts, supports. NET,. NET core, mono, unity, so it can be used in. NET core, and it's easy to load and invoke Lua;
Official website: http://www.moonsharp.org/
Source: Https://github.com/xanathar/moonsharp
Nuget:pm> Install-package Moonsharp
Using the Load Script
1 stringScriptcode =@" 2 sum = 03 for i = 1,4 sum = sum + i5 End6 return sum";7Dynvalue res = script.runstring (scriptcode);//Run the script8Console.WriteLine (Res. number);//Output: 5050
To load a script file:
Console.WriteLine (Script.runfile ("luatest.txt"). number); // Specify a file name to load the script and run
File contents:
0 for 1 - Do = sum + iend return sum
View Code
Passing Data (LUA global variables)
1 stringScriptcode =@" 2 function fact (n)3 if (n = = 0) Then4 return 15 Else6 return N*fact (n-1)7 End8 End9 return fact (num)"; Ten OneScript script =NewScript (); AScript. globals["Num"] =5;//assign a value to the global variable num in the script - -Console.WriteLine (script. Dostring (Scriptcode). number);//Output:
The definition and invocation of LUA functions
1Script script =NewScript ();2 //load the defined function first3Script. Dostring (@"4 function fact (n)5 if (n = = 0) Then6 return 17 Else8 Return n * fact (n-1)9 EndTen End One "); A - //If the function is reused, then it should be called instead of calling the dostring load script call every time (loading the script every time is a fee-based performance) -Console.WriteLine (script. Call (script. globals["fact"],5). number);//Output: theConsole.WriteLine (script. Call (script. globals["fact"], Dynvalue.newnumber (5)). number);//same as above.
Passing Collection parameters
1Script script =NewScript ();2Script. Dostring (@"3 function sum (list)4 local total = 0;5 For i,v in Ipairs (list) do6 Total = total + V;7 End8 return total;9 EndTen "); One AConsole.WriteLine (script. Call (script. globals["sum"],Newlist<int> () {1,3,5,7,9}));//Output:Multi-valued return: Tuple
1Script script =NewScript ();2Script. Dostring (@"3 function sum (KV)4 local total = 0;5 local KS = ';6 For k,v in pairs (kv) do7 Total = total + V;8 ks = ks: K.. ‘,‘; --string concatenation9 EndTen return KS, total; --Multi-valued return: Tuple One End A "); - - varDict =Newdictionary<string,int> ()//Pass Dictionary the { -{"K1",1 }, -{"K2",2 }, -{"K3",3 }, + }; - varTP = Script. Call (script. globals["sum"], dict). Tuple;//return tuple type +Console.WriteLine (tp[0]. String);//output: K1,k2,k3, AConsole.WriteLine (tp[0]. number);//output: 0, if the invocation of string type number will output: 0 atConsole.WriteLine (tp[1]. number);//Output: 6
Functions for loading and invoking C # code definitions in LUA scripts
1 Public Static voidcalllist ()2 {3Script script =NewScript ();4Script. globals["GetList"] = (func<list<int, list<int>>) GetList;//load the functions defined in C #5Script. Dostring (@"6 function sum (list)7 local total = 0;8 For i,v in Ipairs (list) do9 Total = total + V;Ten End One return total; A End - "); - the stringSCODE =@"return sum (getlist ({One, one, all, +))";//calling functions defined in C # in a script -Console.WriteLine (script. Dostring (SCODE));//Output: - - } + - Private Staticlist<int> GetList (list<int>list) + { A for(inti =1; I <=Ten; i++) at list. ADD (i); - returnlist; -}
Class library for loading LUA scripts in. NET Core: Moonsharp