Skip to content

Instantly share code, notes, and snippets.

@xanathar
Created December 12, 2014 20:44
Show Gist options
  • Save xanathar/8120b8af4b079975bc9d to your computer and use it in GitHub Desktop.
Save xanathar/8120b8af4b079975bc9d to your computer and use it in GitHub Desktop.
Sample of event handlers in MoonSharp
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MoonSharp.Interpreter;
namespace PerformanceComparison
{
class Sample
{
// This prints :
// 3
// hello world
// 3
// hello world
// 3
// hello world
// 3
// hello world
// Done
public static void Main()
{
string code = @"
x = 3
function onThis()
print(x)
x = 'hello'
end
function onThat()
print(x .. ' world')
x = 3
end
";
// Load the code
Script script = new Script();
script.DoString(code);
var onThis = script.Globals.Get("onThis").Function.GetDelegate();
var onThat = script.Globals.Get("onThat").Function.GetDelegate();
for (int i = 0; i < 4; i++)
{
onThis();
onThat();
}
Console.WriteLine("Done");
Console.ReadKey();
}
}
}
@Antypodish
Copy link

Superb, this was bit of code, that I was looking for past 2 days.

It allows to store persistent variable, and pass it between calls of each function.

Many thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment