Created
December 12, 2014 20:44
-
-
Save xanathar/8120b8af4b079975bc9d to your computer and use it in GitHub Desktop.
Sample of event handlers in MoonSharp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.