Пример #1
0
		static void CoroutinesFromCSharp()
		{
			string code = @"
				return function()
					local x = 0
					while true do
						x = x + 1
						coroutine.yield(x)
					end
				end
				";

			// Load the code and get the returned function
			Script script = new Script();
			DynValue function = script.DoString(code);

			// Create the coroutine in C#
			DynValue coroutine = script.CreateCoroutine(function);

			// Resume the coroutine forever and ever.. 
			while (true)
			{
				DynValue x = coroutine.Coroutine.Resume();
				Console.WriteLine("{0}", x);
			}
		}
Пример #2
0
		static void CoroutinesAsCSharpIterator()
		{
			string code = @"
				return function()
					local x = 0
					while true do
						x = x + 1
						coroutine.yield(x)
						if (x > 5) then
							return 7
						end
					end
				end
				";

			// Load the code and get the returned function
			Script script = new Script();
			DynValue function = script.DoString(code);

			// Create the coroutine in C#
			DynValue coroutine = script.CreateCoroutine(function);

			// Loop the coroutine 
			string ret = "";

			foreach (DynValue x in coroutine.Coroutine.AsTypedEnumerable())
			{
				ret = ret + x.ToString();
			}

			Console.WriteLine(ret);
		}
Пример #3
0
    IEnumerator LuaRoutine()
    {
        bool pause	= false;

        Debug.Log("start!");
        var script		= new Script();
        script.Options.DebugPrint	= s => Debug.Log(s);

        script.Globals["csharpfunc"]	= (System.Action)delegate()
        {
            pause = true;
        };
        var function	= script.DoFile("testscript");
        var coroutine	= script.CreateCoroutine(function);

        do
        {
            coroutine.Coroutine.Resume();
            Debug.Log("C sharp side");
            yield return new WaitForSeconds(3);
        }
        while (coroutine.Coroutine.State != CoroutineState.Dead);

        yield break;
    }
Пример #4
0
		// See http://www.moonsharp.org/ for more information!

		/*
			How can I redirect the output of the print function ?
				script.Options.DebugPrint = s => { Console.WriteLine(s); }

			How can I redirect the input to the Lua program ?
				script.Options.DebugInput = () => { return Console.ReadLine(); }

			How can I redirect the IO streams of a Lua program ?
				IoModule.SetDefaultFile(script, IoModule.DefaultFiles.In, myInputStream);
				IoModule.SetDefaultFile(script, IoModule.DefaultFiles.Out, myInputStream);
				IoModule.SetDefaultFile(script, IoModule.DefaultFiles.Err, myInputStream);
		*/

		private static void PreemptiveCoroutineTest()
		{

			// This will force the function to yield to the main program every 10 instructions!

			string code = @"
	function fib(n)
		if (n == 0 or n == 1) then
			return 1;
		else
			return fib(n - 1) + fib(n - 2);
		end
	end
	";

			// Load the code and get the returned function
			Script script = new Script(CoreModules.None);
			script.DoString(code);

			// get the function
			DynValue function = script.Globals.Get("fib");

			// Create the coroutine in C#
			DynValue coroutine = script.CreateCoroutine(function);

			// Set the automatic yield counter every 10 instructions. 
			// 10 is likely too small! Use a much bigger value in your code to avoid interrupting too often!
			coroutine.Coroutine.AutoYieldCounter = 10; // 1000 is usually a good starting point.

			int cycles = 0;
			DynValue result = null;

			// Cycle until we get that the coroutine has returned something useful and not an automatic yield..
			for (result = coroutine.Coroutine.Resume(8);
				result.Type == DataType.YieldRequest;
				result = coroutine.Coroutine.Resume())
			{
				cycles += 1;
			}

			// Check the values of the operation
			Console.WriteLine(DataType.Number == result.Type);
			Console.WriteLine(34 == result.Number);
		}