8 Replies Latest reply on Jul 31, 2007 6:46 AM by kglad

    AS3.0: LocalConnection and custom classes

    RobDan75 Level 1
      Hi,

      I've a small but rather an annoying problem I'm trying to send a custom class instance through localconnection but no luck so far. Here are the sources:

      first.fla(timeline):

      import flash.net.LocalConnection;
      var lc:LocalConnection = new LocalConnection();
      var aargh:Aargh = new Aargh();
      trace(aargh.getSomething());
      lc.send("second", "func", aargh);



      second.fla(timeline):

      import flash.net.LocalConnection;
      var lc:LocalConnection = new LocalConnection();
      lc.connect("masodik");
      lc.client = this;
      function func(args:*):void {
      try {
      trace(args.getSomething());
      } catch (e:Error) {
      trace(":(", args);
      }
      }


      aargh.as:

      package {
      public class Aargh {
      public function Aargh() {

      }

      public function getSomething():String {
      return "I'M HEREEEE";
      }
      }
      }


      If I want to pass lets say a String from the first movie to the second, it works perfectly fine, but when it comes to send my instance it does not work. Any idea on how to solve the problem?
        • 1. Re: AS3.0: LocalConnection and custom classes
          Level 7
          Hi RobDan75,

          I'm afraid you won't be able to do this. There are two main reasons why
          this isn't possible:

          1. When you send an instance, you're sending a memory pointer to an
          instance that exists in one SWF. In the receiving SWF, this instance
          won't exist so there's no way to reference it. However, #2 is even more
          important...
          2. Being able to send references to instances...essentially pointers to
          memory locations...is an extreme security violation. The number of ways
          this could be misused is staggering.

          There is no solution to this problem because what you are trying to do
          is disallowed.You can, however, tie calls to LocalConnection instances
          so that one SWF can call the custom class in the other SWF, but the
          class instance still needs to live in the SWF in which it was created.

          I created a pretty robust messaging mechanism that allows this kind of
          cross-SWF communication (check out the ActionScript 2.0 version of the
          BNMAPI at http://www.baynewmedia.com). It works on an event broadcasting
          system that allows any SWF to communicate with any other number of SWFs
          (though sending instance references still can't be done). Here's a sample:

          Receiving FLA 1:

          function myFunc(eventObj:Object) {
          trace ('Event was received!');
          trace ('Value received: '+eventObj.someVal);
          }
          API.addEventListener(this.myFunc,this,'SomeMessage',null,false);

          Receiving FLA 2 (assuming you want one):

          function myFunc2(eventObj:Object) {
          trace ('Event was received!');
          trace ('Value received: '+eventObj.someVal);
          }
          API.addEventListener(this.myFunc2,this,'SomeMessage',null,false);

          Sending FLA:

          API.broadcast({message:''SomeMessage',sender:this,external:true,someVal:12});
          //"external" specifies broadcasting outside the current FLA

          There is still no way to send instances, though, so if this is what you
          need I highly recommend you look for alternate solutions.

          Patrick


          RobDan75 wrote:
          > Hi,
          >
          > I've a small but rather an annoying problem I'm trying to send a custom class
          > instance through localconnection but no luck so far. Here are the sources:
          >
          > first.fla(timeline):
          >
          > import flash.net.LocalConnection;
          > var lc:LocalConnection = new LocalConnection();
          > var aargh:Aargh = new Aargh();
          > trace(aargh.getSomething());
          > lc.send("second", "func", aargh);
          >
          >
          >
          > second.fla(timeline):
          >
          > import flash.net.LocalConnection;
          > var lc:LocalConnection = new LocalConnection();
          > lc.connect("masodik");
          > lc.client = this;
          > function func(args:*):void {
          > try {
          > trace(args.getSomething());
          > } catch (e:Error) {
          > trace(":(", args);
          > }
          > }
          >
          >
          > aargh.as:
          >
          > package {
          > public class Aargh {
          > public function Aargh() {
          >
          > }
          >
          > public function getSomething():String {
          > return "I'M HEREEEE";
          > }
          > }
          > }
          >
          >
          > If I want to pass lets say a String from the first movie to the second, it
          > works perfectly fine, but when it comes to send my instance it does not work.
          > Any idea on how to solve the problem?
          >

          --
          http://www.baynewmedia.com
          Faster, easier, better...ActionScript development taken to new heights.
          Download the BNMAPI today. You'll wonder how you ever did without it!
          Available for ActionScript 2.0/3.0.
          • 2. Re: AS3.0: LocalConnection and custom classes
            kglad Adobe Community Professional & MVP
            you can't pass an object (like Aargh) as a lc parameter. you could pass the string "Aargh" and have the receiving lc convert that to an object using getDefinitionByName().
            • 3. Re: AS3.0: LocalConnection and custom classes
              Level 7
              True. However, you would be getting a new class reference in this case,
              not a point to the original instance you wanted to work with. If your
              Aargh instance, for example, held a "password" value or something, your
              target couldn't get Aargh.password because you'd be working with a brand
              new copy of Aargh, not targeting the original. I think this is what
              kglad meant (sorry if I implied otherwise)....just wanted to clarify
              what would happen.

              Patrick

              kglad wrote:
              > you can't pass an object (like Aargh) as a lc parameter. you could pass the string "Aargh" and have the receiving lc convert that to an object using getDefinitionByName().

              --
              http://www.baynewmedia.com
              Faster, easier, better...ActionScript development taken to new heights.
              Download the BNMAPI today. You'll wonder how you ever did without it!
              Available for ActionScript 2.0/3.0.
              • 4. Re: AS3.0: LocalConnection and custom classes
                kglad Adobe Community Professional & MVP
                agreed, you can't pass the object.

                as i said, you can pass the string and convert that to AN object but that won't be the same as any object from the swf the contains the sending lc.
                • 5. Re: AS3.0: LocalConnection and custom classes
                  Level 7
                  Agreed as well :)

                  kglad wrote:
                  > agreed, you can't pass the object.
                  >
                  > as i said, you can pass the string and convert that to AN object but that won't be the same as any object from the swf the contains the sending lc.

                  --
                  http://www.baynewmedia.com
                  Faster, easier, better...ActionScript development taken to new heights.
                  Download the BNMAPI today. You'll wonder how you ever did without it!
                  Available for ActionScript 2.0/3.0.
                  • 6. Re: AS3.0: LocalConnection and custom classes
                    kglad Adobe Community Professional & MVP
                    now if robdan agrees, this thread is settled.
                    • 7. Re: AS3.0: LocalConnection and custom classes
                      RobDan75 Level 1
                      Thank you for both of your responses, i did not consider point 1 and point 2, I should have thought about memory management before I post this thread. Thanks for both of you for the heads up.
                      • 8. Re: AS3.0: LocalConnection and custom classes
                        kglad Adobe Community Professional & MVP
                        you're welcome.