4 Replies Latest reply on Jul 28, 2011 4:47 PM by fazstp

    Script ilk #script or #instance?

    fazstp Level 2

      If I have a script returning the me object should it's ilk be #script or #instance?

       

       

      ie.

      In a script member named "Test"

       

      on new me

        return me

      end new

       

       

      In another routine;

       

      scr_obj = script( "Test" ).new()

      put ilk( scr_obj )

        • 1. Re: Script ilk #script or #instance?
          fazstp Level 2

          In answer to my own question it seems that an #instance is only returned by the "on new" handler. Any other handler seems to return a #script rather than an #instance.

           

           

          ie.

          In a script member named "Test"

           

          on new me

            return me

          end new

           

          on test me

            return me

          end test

           

           

          In another routine;

           

          scr_obj = script( "Test" ).new()

          put ilk( scr_obj )

          -- #instance

           

          scr_obj = script( "Test" ).test()

          put ilk( scr_obj )

          -- #script

           

          • 2. Re: Script ilk #script or #instance?
            Production Monkey Level 3

            Yep. That's the way it works.

             

            "new" is special. It is called the Constructor and is used to create an instance of a script.

             

            If you call a method on a script directly through the script object such as "scr_obj = script( "Test" ).test()", then "me" is the script object because that is the object you are referencing. Essentially you are calling a static method on the "Test" class. Generally you don't do this. Instead you create an instance by calling "new" and then call any other methods through the instance reference that is stored in a variable.

             

            TestOb = script( "Test" ).new()

            CurMe = TestOb.test()
            put ilk( CurMe )

             

             

            Or you can use chaining:

             

            CurMe = script( "Test" ).new().test()
            put ilk( CurMe )

            • 3. Re: Script ilk #script or #instance?
              Production Monkey Level 3

              Here's another one you can try in the message window.

               

              sprite(3).scriptInstanceList.add(script( "Test" ).new())
              put ilk(sprite(3).test())

               

              -- #sprite

               

               

              "me" is just a variable. It can be many different things.

              • 4. Re: Script ilk #script or #instance?
                fazstp Level 2

                Thanks. I guess I assumed the new handler was merely a convention and that any handler within a parent script could return an instance of that script. I never realised that the new handler was actually required specifically to get an instance.