Skip navigation
cgyulai
Currently Being Moderated

Calling back from C++ library

May 1, 2009 2:02 PM

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

 
Replies
  • Currently Being Moderated
    May 1, 2009 4:21 PM   in reply to cgyulai

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 15, 2010 8:57 PM   in reply to cgyulai

    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();

    }

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points