Passing a JavaScript function callback to .NET code

Sep 6, 2010 at 2:00 AM

I'm working on an XNA project, and I want to use Jint to power JavaScript scripting. I want to do things like this:

function dramaticScene(actors) {
    actors.alice.walkTo(actors.bob, function() {
        actors.bob.say("Hi, Alice!");
    });
}

walkTo will be a C# function; how do I do this? As far as I can tell, its signature should be:

public void walkTo(Actor target, JsFunction callback)
...but, how do  I actually call the JsFunction from C#? Will the closure be a problem?

Sep 10, 2010 at 3:13 AM

I got some time to look into the Jint source, and discovered JintEngine.CallFunction -- which does exactly what I was asking about. My DLL version was 0.8.8.

Sep 16, 2010 at 9:24 PM
I'm doing same thing with below and it's working.

public
void walkTo(Actor target, System.Action callback)
{
   callback.DynamicInvoke();
}

but you cannot control the "this" in javascript
Sep 16, 2010 at 9:41 PM

Interesting -- I'm probably going to stick with using JintEngine.CallFunction, but having other options is cool.

I'm confused by your last line -- JavaScript lets you mess with the value of `this` through the functions `call` and `apply`.