Exemple #1
0
 public void CallFunction(string func)
 {
     if (luaScript.Globals[func] != null)
     {
         luaScript.Call(luaScript.Globals[func]);
     }
 }
    /// <summary>
    /// hook called from LuaUIButtons
    /// </summary>
    void OnLuaUIButtonClick(GameObject button)
    {
        foreach (LuaHandler luaHandler in FindObjectsOfType <LuaHandler>())
        {
            MoonSharp.Interpreter.Script luaScript = luaHandler.luaScript;

            if (luaScript != null && luaScript.Globals["OnUIButtonClick"] != null)
            {
                luaScript.Call(luaScript.Globals["OnUIButtonClick"], Task.GetOrMakeLuaUIObject(button));
            }
        }
    }
Exemple #3
0
		public static double MoonSharpFactorial2()
		{
			string scriptCode = @"    
		-- defines a factorial function
		function fact (n)
			if (n == 0) then
				return 1
			else
				return n*fact(n - 1)
			end
		end";

			Script script = new Script();

			script.DoString(scriptCode);

			DynValue res = script.Call(script.Globals["fact"], 4);

			return res.Number;
		}
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            string scriptCode = "function f(a,b) " + Chunk.Text + " end";

            var script = new Script();

            try
            {
                script.DoString(scriptCode);

                DynValue luaFunction = script.Globals.Get("f");

                // Type conversion for the parameters is optional
                DynValue res = script.Call(
                                        luaFunction,
                                        DynValue.NewString(ParameterA.Text).CastToNumber(),
                                        Double.Parse(ParameterB.Text));

                // Check the return type.
                if (res.Type != DataType.Number)
                {
                    throw new InvalidCastException("Invalid return type: " + res.Type.ToString());
                }

                Result.Foreground = new SolidColorBrush(Colors.Black);

                // Type conversion swallows exceptions.
                Result.Text = res.Number.ToString();
                // Result.Text = res.CastToNumber().ToString();
            }
            catch (Exception ex)
            {
                Result.Foreground = new SolidColorBrush(Colors.Red);
                Result.Text = ex.Message;
            }
        }
Exemple #5
0
 public void ExecuteDraw()
 {
     Lua.Call(DrawHandle);
 }
 /// <summary>
 /// Asynchronously calls the specified function.
 ///
 /// This method is supported only on .NET 4.x and .NET 4.x PCL targets.
 /// </summary>
 /// <param name="script">The script.</param>
 /// <param name="function">The Lua/MoonSharp function to be called</param>
 /// <param name="args">The arguments to pass to the function.</param>
 /// <returns></returns>
 /// <exception cref="System.ArgumentException">Thrown if function is not of DataType.Function</exception>
 public static Task <DynValue> CallAsync(this Script script, object function, params object[] args)
 {
     return(ExecAsync(() => script.Call(function, args)));
 }
 /// <summary>
 /// Asynchronously calls the specified function.
 /// This method is supported only on .NET 4.x and .NET 4.x PCL targets.
 /// </summary>
 /// <param name="script">The script.</param>
 /// <param name="function">The Lua/MoonSharp function to be called</param>
 /// <returns></returns>
 /// <exception cref="System.ArgumentException">Thrown if function is not of DataType.Function</exception>
 public static Task <DynValue> CallAsync(this Script script, object function)
 {
     return(ExecAsync(() => script.Call(function)));
 }
Exemple #8
0
		private static void CallbackTest()
		{
			var scriptCode = @"    
				-- defines a factorial function
				function fact (n)
					if (n == 0) then
						return 1
					else
						return Mul(n, fact(n - 1));
					end
				end";

			var script = new Script();
			script.Globals["Mul"] = (Func<int, int, int>)Mul;
			script.DoString(scriptCode);

			var res = script.Call(script.Globals["fact"], 4);

			Console.WriteLine(res.Number);
		}
Exemple #9
0
		private static double MoonSharp6()
		{
			string scriptCode = @"    
				-- defines a factorial function
				function fact (n)
					if (n == 0) then
						return 1
					else
						return n*fact(n - 1)
					end
				end";

			var script = new Script();
			script.DoString(scriptCode);

			var luaFactFunction = script.Globals.Get("fact");
			var result = script.Call(luaFactFunction, DynValue.NewNumber(4));
			return result.Number;
		}
Exemple #10
0
		private static double MoonSharp4()
		{
			string scriptCode = @"    
				-- defines a factorial function
				function fact (n)
					if (n == 0) then
						return 1
					else
						return n*fact(n - 1)
					end
				end";

			var script = new Script();
			script.DoString(scriptCode);
			var result = script.Call(script.Globals["fact"], 4);
			return result.Number;
		}
Exemple #11
0
		static void Main(string[] args)
		{
			Script.WarmUp();

			Stopwatch sw;

			sw = Stopwatch.StartNew();

			var _s = new Script();
			_s.LoadString(scriptText);

			sw.Stop();

			Console.WriteLine("Build : {0} ms", sw.ElapsedMilliseconds);

			sw = Stopwatch.StartNew();

			var script = new Script();
			script.Globals.Set("check", DynValue.NewCallback(new CallbackFunction(Check)));
			CallbackFunction.DefaultAccessMode = InteropAccessMode.Preoptimized;

			//script.Globals["print"] = (Action<int, string, int>)PrintX;


			DynValue func = script.LoadString(scriptText);

			sw.Stop();

			Console.WriteLine("Build 2: {0} ms", sw.ElapsedMilliseconds);


			sw = Stopwatch.StartNew();
			for (int i = 0; i < ITERATIONS; i++)
			{
				script.Call(func);
			}
			sw.Stop();

			Console.WriteLine("MoonSharp : {0} ms", sw.ElapsedMilliseconds);


			lua.RegisterFunction("check", typeof(Program).GetMethod("NCheck"));

			File.WriteAllText(@"c:\temp\hanoi.lua", scriptText);

#if !PROFILER

			var fn = lua.LoadFile(@"c:\temp\hanoi.lua");

			sw = Stopwatch.StartNew();
			for (int i = 0; i < ITERATIONS; i++)
			{
				fn.Call();
			}
			sw.Stop();

#endif

			Console.WriteLine("NLua  : {0} ms", sw.ElapsedMilliseconds);

			Console.WriteLine("M# == NL ? {0}", g_MoonSharpStr.ToString() == g_NLuaStr.ToString());

			Console.WriteLine("=== MoonSharp ===");
			//Console.WriteLine(g_MoonSharpStr.ToString());
			Console.WriteLine("");
			Console.WriteLine("=== NLua  ===");
			//Console.WriteLine(g_NLuaStr.ToString());

			Console.ReadKey();
		}