9 Replies Latest reply on Mar 22, 2007 10:52 AM by Newsgroup_User

    accessing shared object

    Level 7
      If in an SWF file I create a shared object like this
      var so:SharedObject = SharedObject.getLocal("name");
      and sore some value in it
      so.data.my_value="my_value"

      Then I load another swf into this swf, can I retreive my_value?
      I.e. can I access so from the loaded file?


        • 1. Re: accessing shared object
          Level 7
          aa,

          > If in an SWF file I create a shared object like this
          > var so:SharedObject = SharedObject.getLocal("name");
          > and sore some value in it so.data.my_value="my_value"

          Right ...

          > Then I load another swf into this swf, can I retreive
          > my_value? I.e. can I access so from the loaded file?

          Same way. The SharedObject.getLocal() method either gets the shared
          object referenced or creates a new one. If the shared object already
          exists -- because you've already created one -- then you would refer to that
          "so" variable (your instance of the SharedObject class) and reference its
          data property to access that property -- just as you've done to set its
          value.


          David Stiller
          Adobe Community Expert
          Dev blog, http://www.quip.net/blog/
          "Luck is the residue of good design."


          • 2. Re: accessing shared object
            kglad Adobe Community Professional & MVP
            what's the point of using a sharedobject in a swf that's still open and still contains "my_value"?
            • 3. Re: accessing shared object
              Level 7
              I posted this question because ut did not work.
              Tracing so.data.my_value from the file loaded returned undefined.
              I know that each swg created its own shared object, and though it might have
              the same name, it is stored in a different location and one swf cannot
              access shared object.

              I hoped that the loaded swf is executed in the context of the loading SWF
              and therefore can access that shared object. If this assumption is correct,
              how shall I do it?

              "David Stiller" <stiller@quip-remove-.net> wrote in message
              news:etu68s$p4s$1@forums.macromedia.com...
              > aa,
              >
              > > If in an SWF file I create a shared object like this
              > > var so:SharedObject = SharedObject.getLocal("name");
              > > and sore some value in it so.data.my_value="my_value"
              >
              > Right ...
              >
              > > Then I load another swf into this swf, can I retreive
              > > my_value? I.e. can I access so from the loaded file?
              >
              > Same way. The SharedObject.getLocal() method either gets the shared
              > object referenced or creates a new one. If the shared object already
              > exists -- because you've already created one -- then you would refer to
              that
              > "so" variable (your instance of the SharedObject class) and reference its
              > data property to access that property -- just as you've done to set its
              > value.
              >
              >
              > David Stiller
              > Adobe Community Expert
              > Dev blog, http://www.quip.net/blog/
              > "Luck is the residue of good design."
              >
              >


              • 4. Re: accessing shared object
                Level 7
                aa,

                > I posted this question because ut did not work.
                > Tracing so.data.my_value from the file loaded returned
                > undefined.

                Have you done any troubleshooting? Can you create and then access a
                shared object without an external SWF? Is it just the external SWF that
                causes the problem?

                > I know that each swg created its own shared object,
                > and though it might have the same name, it is stored in
                > a different location and one swf cannot access shared
                > object.

                This is possibly new information. ;) Are you saying the external SWF
                is being loaded from another domain? If so, you'll want to check into the
                optional localPath parameter in the static getLocal() method.


                David Stiller
                Adobe Community Expert
                Dev blog, http://www.quip.net/blog/
                "Luck is the residue of good design."


                • 5. Re: accessing shared object
                  Level 7

                  "David Stiller" <stiller@quip-remove-.net> wrote in message
                  news:etu9sd$fm$1@forums.macromedia.com...
                  > aa,

                  > Have you done any troubleshooting? Can you create and then access a
                  > shared object without an external SWF? Is it just the external SWF that
                  > causes the problem?

                  In my context the shared object has to be created by the external SWF and to
                  be accessed by the internal one.
                  The external SWF created SO and traces values stored in it correctly. The
                  internal SWF returnes undefined

                  > > I know that each swg created its own shared object,
                  > > and though it might have the same name, it is stored in
                  > > a different location and one swf cannot access shared
                  > > object.
                  >
                  > This is possibly new information. ;)

                  I wish I was wrong for it will solve my ptoblem. Are you saying that one SWF
                  can access a SO created by another SWF?

                  > is being loaded from another domain?

                  They are in the same folder on my local disk.

                  >If so, you'll want to check into the optional localPath parameter in the
                  static getLocal() method.
                  As I learned from this NG, the local path does not accept syntax like ../
                  which in HTML allows to go to the parent directory.


                  • 6. Re: accessing shared object
                    Level 7
                    aa,

                    > I wish I was wrong for it will solve my ptoblem. Are you saying
                    > that one SWF can access a SO created by another SWF?

                    Yes.

                    >>If so, you'll want to check into the optional localPath parameter
                    >> in the static getLocal() method.

                    > As I learned from this NG, the local path does not accept syntax
                    > like ../ which in HTML allows to go to the parent directory.

                    Flash certainly understands paths ../, /, and the like. I use them all
                    the time. Those are called relative paths, and the only tricky thing about
                    that is that they depend on the calling SWf's point of view in the
                    arrangement of files and folders.

                    But the localPath parameter in this case has its own special syntax, as
                    specified in the documentation. Here's a quick test I did in two SWFs named
                    so-a.swf and so-b.swf.

                    // so-a code:
                    var so:SharedObject = SharedObject.getLocal("aaTest", "/");
                    if (so.data.myVar) {
                    trace("a: " + so.data.myVar);
                    } else {
                    so.data.myVar = "apples"
                    }
                    loadMovieNum("so-b.swf", 1);

                    // so-b code:
                    var so:SharedObject = SharedObject.getLocal("aaTest", "/");
                    if (so.data.myVar) {
                    trace("b: " + so.data.myVar);
                    }

                    Notice the opational localPath parameter used by both. In the first
                    SWF, the SharedObject.getLocal() method is used to retrieve a local shared
                    object named aaTest (named after you). If such a local shared object does
                    not exist, SWF A creates it.

                    If the SharedObject instance's data property contains a myVar property,
                    SWF A traces its value. If not, it sets that propert's value to "apples."
                    Then it loads SWF B.

                    SWF B does very nearly the same thing. When I test these SWF, both A
                    and B trace their respective recognition of the value of myVar.


                    David Stiller
                    Adobe Community Expert
                    Dev blog, http://www.quip.net/blog/
                    "Luck is the residue of good design."


                    • 7. Re: accessing shared object
                      Level 7
                      It works indeed if I run the first file and then the second one.
                      If I load the second into the first, it does not.
                      trace() does not seem to return anything even when I removed conditional if
                      in your code - ???

                      "David Stiller" <stiller@quip-remove-.net> wrote in message
                      news:etucth$4e6$1@forums.macromedia.com...
                      > aa,
                      >
                      > > I wish I was wrong for it will solve my ptoblem. Are you saying
                      > > that one SWF can access a SO created by another SWF?
                      >
                      > Yes.
                      >
                      > >>If so, you'll want to check into the optional localPath parameter
                      > >> in the static getLocal() method.
                      >
                      > > As I learned from this NG, the local path does not accept syntax
                      > > like ../ which in HTML allows to go to the parent directory.
                      >
                      > Flash certainly understands paths ../, /, and the like. I use them
                      all
                      > the time. Those are called relative paths, and the only tricky thing
                      about
                      > that is that they depend on the calling SWf's point of view in the
                      > arrangement of files and folders.
                      >
                      > But the localPath parameter in this case has its own special syntax,
                      as
                      > specified in the documentation. Here's a quick test I did in two SWFs
                      named
                      > so-a.swf and so-b.swf.
                      >
                      > // so-a code:
                      > var so:SharedObject = SharedObject.getLocal("aaTest", "/");
                      > if (so.data.myVar) {
                      > trace("a: " + so.data.myVar);
                      > } else {
                      > so.data.myVar = "apples"
                      > }
                      > loadMovieNum("so-b.swf", 1);
                      >
                      > // so-b code:
                      > var so:SharedObject = SharedObject.getLocal("aaTest", "/");
                      > if (so.data.myVar) {
                      > trace("b: " + so.data.myVar);
                      > }
                      >
                      > Notice the opational localPath parameter used by both. In the first
                      > SWF, the SharedObject.getLocal() method is used to retrieve a local shared
                      > object named aaTest (named after you). If such a local shared object does
                      > not exist, SWF A creates it.
                      >
                      > If the SharedObject instance's data property contains a myVar
                      property,
                      > SWF A traces its value. If not, it sets that propert's value to "apples."
                      > Then it loads SWF B.
                      >
                      > SWF B does very nearly the same thing. When I test these SWF, both A
                      > and B trace their respective recognition of the value of myVar.
                      >
                      >
                      > David Stiller
                      > Adobe Community Expert
                      > Dev blog, http://www.quip.net/blog/
                      > "Luck is the residue of good design."
                      >
                      >


                      • 8. Re: accessing shared object
                        Level 7
                        aa,

                        > It works indeed if I run the first file and then the second one.

                        That's right. Isn't that the idea? You wanted to know if two different
                        SWFs could access the same SharedObject. Maybe I didn't understand?

                        > If I load the second into the first, it does not.

                        That makes sense, because the second one does not set a value to
                        myVar -- as you can see, it only reports on an existing value (if one
                        exists).

                        > trace() does not seem to return anything even when I removed
                        > conditional if in your code - ???

                        Which if statement?


                        David Stiller
                        Adobe Community Expert
                        Dev blog, http://www.quip.net/blog/
                        "Luck is the residue of good design."


                        • 9. Re: accessing shared object
                          Level 7
                          this has been sorted out by re-starting Windows
                          So the idea works, now I'll try to use it for my needs. Thank you