-
-
Save unitycoder/6acc29469ff93a7c0d8071ebe46ac1df to your computer and use it in GitHub Desktop.
Sample of event handlers in MoonSharp
This file contains 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