Be a Supporter!

dynamic variable name

  • 246 Views
  • 10 Replies
New Topic Respond to this Topic
Tygerman
Tygerman
  • Member since: Aug. 16, 2003
  • Offline.
Forum Stats
Member
Level 17
Blank Slate
dynamic variable name 2010-10-18 23:48:19 Reply

Is it possible to change the name of a variable while the program is running

I want to reference 21 different variables all with names like

Ingredient1, Ingredient2...Ingredient21

how (if I can) would I make it so that I can reference them all by using a for loop and just changing the number part of the variable name


BBS Signature
YoinK
YoinK
  • Member since: Jan. 5, 2001
  • Offline.
Forum Stats
Member
Level 60
Game Developer
Response to dynamic variable name 2010-10-19 00:02:37 Reply

hmm something like this?

for(i=1;i<5;i++){
duplicateMovieClip(_root.ingredient, "Ingredient"+i, 1000+i);

Kanye West Did What??????
They should make a black guy the manager, they know how to run shit!
2 Girls 1 Cup .... THE SONG!!!!

Tygerman
Tygerman
  • Member since: Aug. 16, 2003
  • Offline.
Forum Stats
Member
Level 17
Blank Slate
Response to dynamic variable name 2010-10-19 00:06:01 Reply

yah :) buuut is the duplicating movie clip necessary?


BBS Signature
Tygerman
Tygerman
  • Member since: Aug. 16, 2003
  • Offline.
Forum Stats
Member
Level 17
Blank Slate
Response to dynamic variable name 2010-10-19 00:14:02 Reply

Also, I should clarify, it's to shorten this kind of code:

if (values==1) {
var ingredient1:Ingredient1 = new Ingredient1();
ingredientDrawArr[i]=ingredient1;
}

if (values==2) {
var ingredient2:Ingredient2 = new Ingredient2();
ingredientDrawArr[i]=ingredient2;
}


BBS Signature
ProfessorFlash
ProfessorFlash
  • Member since: Oct. 6, 2007
  • Offline.
Forum Stats
Member
Level 32
Programmer
Response to dynamic variable name 2010-10-19 02:07:25 Reply

for (var i:int = 0; i < 21; i++) {
var s:String = "Ingredient" + i;
var cl:Class = getDefinitionByName(s) as Class;
var mc:MovieClip =  new cl() as MovieClip;
ingredientDrawArr.push(mc);
}

You can solve pretty much any problem you may have with AS3 by consulting the AS3 Language reference.

Redshift
Redshift
  • Member since: Feb. 12, 2005
  • Offline.
Forum Stats
Member
Level 15
Programmer
Response to dynamic variable name 2010-10-19 03:27:36 Reply

Holy crap, just use an Array.


#include <stdio.h>
char*p="#include <stdio.h>%cchar*p=%c%s%c;%cmain() {printf(p,10,34,p,34,10);}";
main() {printf(p,10,34,p,34,10);}

BBS Signature
tenentenen
tenentenen
  • Member since: Apr. 13, 2009
  • Offline.
Forum Stats
Member
Level 05
Blank Slate
Response to dynamic variable name 2010-10-19 14:49:49 Reply

At 10/19/10 03:27 AM, Redshift wrote: Holy crap, just use an Array.

Agreed, Arrays are the most powerful too in coding. If you are trying to reference things with variable names, DON'T! use array locations instead.

use the push() method to add items to an array, and to access item use:

ingerdientArray[location];

to access whatever variable is stored there. and since location is a number, to loop through an aray is as easy as believing in god.

ProfessorFlash
ProfessorFlash
  • Member since: Oct. 6, 2007
  • Offline.
Forum Stats
Member
Level 32
Programmer
Response to dynamic variable name 2010-10-19 15:58:23 Reply

I think you guys should read the thread and not just the title. "Use an array" is not the complete solution to his problem.


You can solve pretty much any problem you may have with AS3 by consulting the AS3 Language reference.

Redshift
Redshift
  • Member since: Feb. 12, 2005
  • Offline.
Forum Stats
Member
Level 15
Programmer
Response to dynamic variable name 2010-10-19 16:08:59 Reply

At 10/19/10 03:58 PM, ProfessorFlash wrote: I think you guys should read the thread and not just the title. "Use an array" is not the complete solution to his problem.

Why not?


#include <stdio.h>
char*p="#include <stdio.h>%cchar*p=%c%s%c;%cmain() {printf(p,10,34,p,34,10);}";
main() {printf(p,10,34,p,34,10);}

BBS Signature
ProfessorFlash
ProfessorFlash
  • Member since: Oct. 6, 2007
  • Offline.
Forum Stats
Member
Level 32
Programmer
Response to dynamic variable name 2010-10-19 16:34:20 Reply

At 10/19/10 04:08 PM, Redshift wrote:
At 10/19/10 03:58 PM, ProfessorFlash wrote: I think you guys should read the thread and not just the title. "Use an array" is not the complete solution to his problem.
Why not?

Well if you read his third post, he seems to have 21 classes in his library conveniently named "Ingredient"+Number. How are you going to create an instance of them all with "use an array"? You push the created instances to an array yes, but you first need to create them, and there comes in my solution.


You can solve pretty much any problem you may have with AS3 by consulting the AS3 Language reference.

Redshift
Redshift
  • Member since: Feb. 12, 2005
  • Offline.
Forum Stats
Member
Level 15
Programmer
Response to dynamic variable name 2010-10-19 19:08:49 Reply

If I was doing this, I'd personally prefer something like this:

public static var ingredients:Array =
[
    Ingredient1,
    Ingredient2,
    Ingredient3,
    ...,
    Ingredient<n>
]

Sure, you have to actually construct the array instead of dynamically using strings to fetch a class. But you get the added benefit of them being explicitly defined, and so that if one of those classes doesn't exist then you'll get a compile error, instead of some weird run-time error that might pop out randomly in the middle of your program. If that Array definition compiles without errors, then it's guaranteed that all elements of that array are actually defined classes.

Then all you have to do is:

for(var i:int = 0; i < ingredients.length; i++){
    var ingredient:MovieClip = new ingredients[i]() as MovieClip;
    // ingredient is now an instance of one of the elements of the ingredients Array
}

// or applied to the snippet of code Tygerman provided:
var ingredient:MovieClip = new ingredient[values]() as MovieClip;
ingredientDrawArr[i]=ingredient;

What you could do to improve on this scheme, is to make an Ingredient class like this:

package SomePackage{
    import flash.display.Sprite;
    import flash.display.MovieClip;
    
    public class Ingredient extends Sprite{ // use Sprite as a light-weight wrapper
    
        private static var ingredients:Array =
        [
            Ingredient1,
            Ingredient2,
            Ingredient3,
            ...,
            Ingredient<n>
        ];
        
        public var id:int; // indicates which ingredient it is
        public var display:MovieClip; // the actual MovieClip instance of the ingredient
                                      // this class is simply a nice wrapper
        
        public function Ingredient(n:int){
            if(n < 0){ // correct the index if it's below 0
                n *= -1;
            }
            n %= ingredients.length; // if n is too large, loop back around
            id = n;
            display = new ingredients[id]() as MovieClip;
            addChild(display);
        }
    }
}

Now you have a nice clean wrapper, where you can add more custom properties and methods to deal with ingredients if you wish, and initializing an ingredient is much nicer:

var ingredient:Ingredient = new Ingredient(someValue);

#include <stdio.h>
char*p="#include <stdio.h>%cchar*p=%c%s%c;%cmain() {printf(p,10,34,p,34,10);}";
main() {printf(p,10,34,p,34,10);}

BBS Signature