Class Library for loading lua scripts in. net core: MoonSharp, luamoonsharp

Source: Internet
Author: User

Class Library for loading lua scripts in. net core: MoonSharp, luamoonsharp
Preface

MoonSharp is a class library that supports C # Calling lua scripts. net ,. net core, mono, unity, so in. net core can also be used, and it is easy to load and call lua;

Official Website: http://www.moonsharp.org/

Source code: https://github.com/xanathar/moonsharp

Nuget: PM> Install-Package MoonSharp

 

Use the load script
1 string scriptCode = @ "
2 sum = 0
3 for i = 1, 100 do
4 sum = sum + i
5 end
6 return sum ";
7 DynValue res = Script.RunString (scriptCode); // Run script
8 Console.WriteLine (res.Number); // Output: 5050

Load the script file:

Console. WriteLine (Script. RunFile ("luatest.txt"). Number); // specify the file name to load the Script and run it.

File Content:

sum = 0
for i = 1, 100 do
    sum = sum + i
end return sum
View Code

 

Transfer Data (lua global variable)
1 string scriptCode = @ "
  2 function fact (n)
  3 if (n == 0) then
  4 return 1
  5 else
  6 return n * fact (n-1)
  7 end
  8 end
  9 return fact (num) ";
10
11 Script script = new Script ();
12 script.Globals ["num"] = 5; // Assign global variable num in the script
13
14 Console.WriteLine (script.DoString (scriptCode) .Number); // Output: 120

 

Lua Function Definition and calling
1 Script script = new Script ();
  2 // Load the defined function first
  3 script.DoString (@ "
  4 function fact (n)
  5 if (n == 0) then
  6 return 1
  7 else
  8 return n * fact (n-1)
  9 end
10 end
11 ");
12
13 // If the function will be reused, then it should be called instead of calling the DoString load script every time (loading the script every time is costly)
14 Console.WriteLine (script.Call (script.Globals ["fact"], 5) .Number); // Output: 120
15 Console.WriteLine (script.Call (script.Globals ["fact"], DynValue.NewNumber (5)). Number); // Same as above
Pass set parameters
1 Script script = new Script ();
  2 script.DoString (@ "
  3 function sum (list)
  4 local total = 0;
  5 for i, v in ipairs (list) do
  6 total = total + v;
  7 end
  8 return total;
  9 end
10 ");
11
12 Console.WriteLine (script.Call (script.Globals ["sum"], new List <int> () {1, 3, 5, 7, 9})); // Output: 25
Multi-value return: Tuple
1 Script script = new Script ();
 2 script.DoString (@ "
 3 function sum (kv)
 4 local total = 0;
 5 local ks = '';
 6 for k, v in pairs (kv) do
 7 total = total + v;
 8 ks = ks .. k .. ','; --string concatenation
 9 end
10 return ks, total; --Multi-value return: Tuple
11 end
12 ");
13
14 var dict = new Dictionary <string, int> () // pass dictionary
15 {
16 {"k1", 1},
17 {"k2", 2},
18 {"k3", 3},
19};
20 var tp = script.Call (script.Globals ["sum"], dict) .Tuple; // Returns tuple type
21 Console.WriteLine (tp [0] .String); // Output: k1, k2, k3,
22 Console.WriteLine (tp [0] .Number); // Output: 0, if it is a String type, Number will output: 0
23 Console.WriteLine (tp [1] .Number); // Output: 6

 

Load and call functions defined by C # code in the lua script
1 public static void CallList ()
  2         {
  3 Script script = new Script ();
  4 script.Globals ["getlist"] = (Func <List <int>, List <int >>) GetList; // Load functions defined in C #
  5 script.DoString (@ "
  6 function sum (list)
  7 local total = 0;
  8 for i, v in ipairs (list) do
  9 total = total + v;
10 end
11 return total;
12 end
13 ");
14
15 string scode = @ "return sum (getlist ({11, 12, 13, 14, 15}))"; // Call the function defined in C # in the script
16 Console.WriteLine (script.DoString (scode)); // Output: 120
17
18}
19
20 private static List <int> GetList (List <int> list)
twenty one         {
22 for (int i = 1; i <= 10; i ++)
23 list.Add (i);
24 return list;
25} 

 


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.