I am trying to call back to ActionScript from my C++ code. So far I could get only generic functions, e.g.,
AS3_Val traceObj = AS3_NSGetS(NULL, "trace");
I could not get or call any class or function I defined. What am I missing?
C++ code:
AS3_Val tNS = AS3_String("alchemy.test"); // I tried others, e.g., "", "cmodule.libtest"
AS3_Val tClass = AS3_NSGetS(tNS, "callback"); // tClass is 0, so AS3_Call() does not work...
AS3_Val t2Class = AS3_NSGetS(NULL, "myCallback"); // t2Class is 0
AS3_Val tMsg = AS3_String("Hello");
AS3_CallTS("myCallback", NULL, "StrType", tMsg); // does not work
AS:
package alchemy.test {
public class libtest {
...
static public function callback(msg:String):int {
return _lib.callback(msg);
}
}
}
MXML:
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" applicationComplete="init()">
<mx:Script>
<![CDATA[
...
public function myCallback(msg:String):void
{
trace(msg);
}
]]>
</mx:Script>
</mx:WindowedApplication>
Thank you,
Csaba
Calling
AS3_NSGetS(AS3_String("nsStr"), "prop")in alchemy code, is pretty much the same thing as calling
(new Namespace("nsStr"))::[prop]
in actionscript. Hence, you could test the former with the latter. Remember that alchemy code runs inside its own package, cmodule.outputName, where outputName is the output file passed to gcc, stripped of its extension .
I note that you are not quite referencing your functions directly -- callback() is inside the libtest() class, so you would first have to call AS3_NSGetS(tNS, "libtest") to access the class housing callback(). Similarly, myCallback is not global -- it is inside the class inheriting from WindowedApplication that is generated from your mxml.
Things are complicated further by the different swc boundaries. I have never quite managed to directly reference anything of my own creation outside alchemy, and find it much easier (and more modular) to pass objects from actionscript to alchemy via alchemy function calls from the actionscript.
I tried the suggested method and some combinations of namespace/class/function names, however I could not get or call my own functions. I could get only built-in objects:
AS3_Val traceClass = AS3_NSGetS(NULL, "trace");
AS3_Call(traceClass, AS3_Undefined(), AS3_Array("StrType", "trace1")); // worked
Actually, calling trace() using AS3_CallS() did not work:
AS3_CallS("trace", AS3_Undefined(),AS3_Array("StrType", "trace2")); // did not work
I could get a reference to the AIR application, but I was not able to get reference to any objects within:
AS3_Val tClass = AS3_NSGetS(NULL, "Test"); // tClass is non-NULL
I could not find any sample code about this problem either.
Csaba
I found a way to call functions I created but it's not the most elegant solution. In short, I found that you need to call a function pointer to the function you intend to call. I'm going from memory so I apologize if it does not compile. Still, it should give you the basic idea.
ActionScript
public class A
{
public var callbackPtr:Function = callback();
public function A()
{
var loader:CLibInit = new CLibInit;
var alchemyClass = loader.init();
alchemyClass.setA(this);
alchemyClass.callAS()
}
public function callback():Function
{
function callbackTrace(bytes:ByteArray):void
{
trace("callback...");
}
return callbackTrace;
}
}
C
AS3_Val classA;
AS3_Val setA(void *self, AS3_Val args)
{
classA = AS3_Undefined();
AS3_ArrayValue(args, "AS3ValType", & classA);
return AS3_Null();
}
AS3_Val callAS(void *self, AS3_Val args)
{
AS3_CallS("callbackPtr", classA, AS3_Null());
return AS3_Null();
}
North America
Europe, Middle East and Africa
Asia Pacific