JScript : Calling JScript functions from Flash (AS3)

Before you can call a JScript function it has to be loaded into a script module using the SWF Studio Script commands. Loaded script modules become automatic extensions to the SWF Studio ActionScript API. Once the JScript has been loaded into a module, you can begin calling functions in that module until the module is unloaded.

  Create a script module called "MyScriptModule".

ssCore.Script.create({module:"MyScriptModule", language:"jscript"});

  Load your JScript code into "MyScriptModule".

This loads JScript code from a Flash variable into "MyScriptModule".

var myCode = "function myFunction(){alert('myFunction called in JScript!');};";

ssCore.Script.addCode({module:"MyScriptModule", code:myCode});

This loads JScript code from an external file (file.js) into "MyScriptModule".

ssCore.Script.loadCode({module:"MyScriptModule", path:"startdir://file.js"});

Any code that is not inside a function will be called immediately when the code is added to the module. Functions within the code can be called from Flash through ssCore using the module name as the command object, and the function name as the method.

  Call a JScript function "myfunction" that was loaded into MyScriptModule.

ssCore.MyScriptModule.myFunction();

Note: You can also send data to the script function and receive data back. The script function call from Flash behaves the same way a call to any other SWF Studio command behaves. You pass data via an object and if you specify a callback, it will be called when the script function ends. The script can also return data (always converted to a string) to the callback. The out parameter (the parameter passed to the callback as part of the return object) is always called result.

Note 2: If you call a JScript function from ActionScript synchronously, the JScript function can not call jsCore.execute or attempt to access ActionScript since the Flash Player is waiting for the JScript function to return. ActionScript in the Flash Player is single-threaded, and in this case, means the Flash Player will wait for the synchronous command to complete before running any further ActionScript. If your JScript function needs to access jsCore.execute or the Flash Player you should call the JScript function asynchronously.

Flash ActionScript that calls a JScript function:

ssCore.MyScriptModule.getAnswer({filename:"file.txt"}, {callback:"onGotAnswer", scope:this});

function onGotAnswer(return_obj:Object, callback_obj:Object, error_obj:Object):void
{
    ssDebug.trace("The answer to life the universe and everything is: "+return_obj.result);
}

JScript code called from Flash.

function getAnswer(obj)
{
    return "42";
}