A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: Variable Embedded Object Names [AS2]

  1. #1
    Junior Member
    Join Date
    Jul 2010
    Posts
    7

    Variable Embedded Object Names [AS2]

    Hello all,

    I currently am building an RPG type game, and had a question about adding variables to the names of objects when calling them.

    I am attempting to run a for loop that checks a character's items and whether or not they exist. Each character is an object, with seperate variables within them, such as Health, Strength, but also their 5 slots they can hold an item in. Each item slot itsself is an object, where the object holds all the properties of the item.

    Example:

    Code:
    function setupchar(character, char, type) {
    	characterroster[characterroster.length] = char;
    	character.namee = char;
    	character.unittype = type;
    	character.hp = 0;
    	character.maxhp = 0;
    	//There goes a lot of other stats here, shortening it for convinience sake
    	character.item1 = new Object();
    	character.item1.namee = " ";
    	character.item1.uses = "--";
    	character.item1.maxuses = "--";
    	character.item2 = new Object();
    	character.item2.namee = " ";
    	character.item2.uses = "--";
    	character.item2.maxuses = "--";
    	character.item3 = new Object();
    	character.item3.namee = " ";
    	character.item3.uses = "--";
    	character.item3.maxuses = "--";
    	character.item4 = new Object();
    	character.item4.namee = " ";
    	character.item4.uses = "--";
    	character.item4.maxuses = "--";
    	character.item5 = new Object();
    	character.item5.namee = " ";
    	character.item5.uses = "--";
    	character.item5.maxuses = "--";
    	//there are more stats here, again shortening it for convinience sake
    }

    I know that when running a for loop, you can use variables to check object names, namely the "i" that you use to count how many times the loop runs. For example, if I had "Player1", "Player2", and "Player3" made with my setup function, I could check for, say, their name, with the following:

    Code:
    for(i = 1; i <= 3; i++) {
    	if(_root["Player"+i].namee == "Bob") {
    		variable1 = i;
    	}
    }
    Which would return variable1 with the player number that is named "Bob".

    My question stemms from the following problem: I am trying to check the assorted item slots of a given character, but the item objects are embedded into their own seperate objects, being the character objects. I have the following code:

    Code:
    function additem(character, item) {
    	for(i = 1; i <= 5; i++) {
    		if(character._root["item"+i].namee == " ") {
    			character._root["item"+i].namee = item;
    			if(item == "Iron Sword") {
    				character._root["item"+i].uses = 40;
    				character._root["item"+i].maxuses = 40;
    			}
    			//the code goes on to define every item in the game, shortening it for convienience
    			i = 5;
    		}
    	}
    }
    The function, when ran, should, in theory, add an item to the last available slot in the character's inventory, or in more technical terms, assign values to the first item object belonging to the character object that does not already have a value other than the preset " ".

    For example, if I were to type:

    Code:
    additem(Jim, "Iron Sword");
    It should in theory look at Jim.item1.namee to see if the item has been defined or not, meaning an item exists in that item slot, and if it has, it will continue down the list, going to Jim.item2.namee and so on until Jim.item5.namee, where if each item was still defined, it would return the player with a "cannot do this" message. However, the for loop in the additem function seems not to be adding the i variable correctly to the "item" part of the object name, which is the embedded part.

    So overall, my question is:

    Is there a way to call a variable name of an object that's embedded within another object?
    Last edited by AMZYoshio; 03-11-2012 at 06:25 AM.

  2. #2
    Prid - Outing Nig 13's Avatar
    Join Date
    Jul 2006
    Location
    Norway
    Posts
    1,864
    Finding a variable dynamically requires this:

    scope[parts_to_join]

    where scope can be, for instance, _root, _parent, _global or this, the meaning is to have a target, so in your addItem function, you already have a scope, which is character, which is the target, so you don't need to specify and extra target, so new code:

    Actionscript Code:
    function additem(character, item) {
        for(i = 1; i <= 5; i++) {
            if(character.["item"+i].namee == " ") {
                character.["item"+i].namee = item;
                if(item == "Iron Sword") {
                    character.["item"+i].uses = 40;
                    character.["item"+i].maxuses = 40;
                }
                //the code goes on to define every item in the game, shortening it for convienience
                i = 5;
            }
        }
    }

    You could also use, _root.character.["item"+i].namee = item;

    Hope this helps
    I am back, guys ... and finally 18 :P

    BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS

  3. #3
    Junior Member
    Join Date
    Jul 2010
    Posts
    7
    Code:
    **Error** Scene=Title, layer=code, frame=1:Line 93: Expected a field name after '.' operator.
         		if(_root.character.["item"+i].namee == " ") {
    
    Total ActionScript Errors: 1 	 Reported Errors: 1
    Same thing happens if I remove the _root scope.

  4. #4
    Junior Member
    Join Date
    Jul 2010
    Posts
    7
    Ahhh, nevermind, you have to remove the period after "character" and before "["item"+i]", just like you'd remove the period after _root when defining the variable name.

    Problem seems to be resolved, thank you so much

  5. #5
    Prid - Outing Nig 13's Avatar
    Join Date
    Jul 2006
    Location
    Norway
    Posts
    1,864
    Happy to help
    I am back, guys ... and finally 18 :P

    BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center