7 Replies Latest reply on Sep 10, 2008 12:49 PM by OleKristianHH

    Passing a variable to a script?

    OleKristianHH
      Hallo,

      How can I send a variable to a script when I attach the script to a scripted sprite?
        • 1. Re: Passing a variable to a script?
          Production Monkey Level 3
          Somthing like this should work:

          sprite(someChannel).scriptInstanceList.add(script("scriptToUse").new());

          //the scriptToUse file:
          function doSomething(me,Var1,Var2)
          {
          trace("Var1= " + Var1);
          trace("Var2= " + Var2);
          }


          sprite(someChannel).doSomething(100, "Hello World");
          • 2. Re: Passing a variable to a script?
            OleKristianHH Level 1
            Thank you for your reply, but that does not work for functions that are controlled by the program (exitframe, mouseUp, mouseDown...)

            Let me try to make a more concret example:
            Say I have a lot of spaceships and I want them all to have a different velocity.

            • 3. Re: Passing a variable to a script?
              Level 7
              Try something like:
              // Create 10 space ships
              for(var i = 0; i < 10; i++)
              {
              var vel = random(10);

              channel(someChannel).makeScriptedSprite(member("spaceShip"),
              point(0, 0));

              sprite(someChannel).scriptInstanceList.add(script("spaceShpScript").new(
              random(10)));
              }


              // Content of spaceship script:
              function new(me, aVelocity)(
              this.velocity = aVelocity;
              return me;
              }

              function exitFrame(me)
              {
              var id = _player.currentSpriteNum;

              sprite(id).locV += this.velocity;
              }
              • 4. Re: Passing a variable to a script?
                OleKristianHH Level 1
                Thanks for the code, but it does not work as I would like it to.
                When using the code all the objects use the same velocity (the that was
                passed to the last object)

                Is it possible to maybe first create a copy of the script and then assing it to a sprite?

                var tempScript = new script("spaceShipScript");
                tempScript.init(speed);
                //assing to the sprite...

                Here is the code I tried to use, maybe I made a mistake somwhere?
                • 5. Re: Passing a variable to a script?
                  Production Monkey Level 3
                  I got it to work. Although, I would like to say that JavaScript sucks. JavaScript inside Director really, really sucks. You should consider learning Lingo. It runs faster, is less verbose, enables you to make actual classes which is far superior to the "prototype" concept, and not the least important; the people who help out on the forums program in Lingo.

                  I don't know if this is the best solution. I tried to use a parent script, but that is either impossible or I have not stumbled upon the correct syntax. So, I wrote the space ship script using a movie script and classic javascript syntax.

                  I put the following mouseup script in a Behavior script:

                  function mouseUp() {
                  // Create 10 spaceships
                  for(var i = 0; i < 10; i++){
                  channel(i+10).makeScriptedSprite(member("SpaceShip"), point(i * 50, 0));
                  sprite(i+10).scriptInstanceList.add(new SpaceShip(i+10,random(10)));
                  }
                  }

                  I put the following SpaceShip script in a Movie script:

                  function SpaceShip (SpriteNumber, Vel) {
                  this.Velocity = Vel;
                  this.ThisSprite = sprite(SpriteNumber);
                  return this;
                  }


                  SpaceShip.prototype.exitFrame = function(){
                  this.ThisSprite.locV += this.Velocity;
                  }


                  It works and hopefully will get you pointed in a good direction.
                  • 6. Re: Passing a variable to a script?
                    Production Monkey Level 3
                    A Movie script is the typical way to create a class in javascript. However, for encapsulation purposes you can use a Parent script as an object factory class, which is a class/object that you ask to make objects for you.

                    So, for fun.

                    The mouseUp behavior becomes:

                    function mouseUp() {
                    // Create 10 spaceships
                    for(var i = 0; i < 10; i++){
                    channel(i+10).makeScriptedSprite(member("SpaceShip"), point(i * 50, 0));
                    // sprite(i+10).scriptInstanceList.add(new SpaceShip(i+10,random(10))); // Movie script version
                    sprite(i+10).scriptInstanceList.add(script("Ship Factory").makeShip(i+10,random(10))); // Parent script version
                    }
                    }

                    Create a Parent script named: "Ship Factory" and add this code.

                    // SpaceShip Factory Class
                    // Parent script version of a SpaceShip class.
                    // Creates SpaceShip objects and returns them to the calling function.
                    // All SpaceShip functions are scoped to this script.


                    function makeShip (me, SpriteNumber, Vel) {
                    return new SpaceShip(SpriteNumber, Vel); // Director looks inside this script first for a "SpaceShip" function, then globally.
                    }

                    function SpaceShip (SpriteNumber, Vel) {
                    this.Velocity = Vel;
                    this.ThisSprite = sprite(SpriteNumber);
                    return this;
                    }


                    SpaceShip.prototype.exitFrame = function(){
                    this.ThisSprite.locV += this.Velocity;
                    }


                    SpaceShip.prototype.reverse = function(){ // note: calling this from message window only works if in "lingo" mode
                    this.Velocity *= -1.0;
                    }
                    • 7. Re: Passing a variable to a script?
                      OleKristianHH Level 1
                      Thanks a lot to both of you.

                      The reason why I am using javascript is because thats that the people at work wanted to use (I did not know javascript or lingo at the time, so I did not argue about it), but I later found out that Lingo is better supported, faster and has better documentation.