A Flash Developer Resource Site

Results 1 to 9 of 9

Thread: [CS3][AS2] Nesting Objects Inside Other Objects?

  1. #1
    Member
    Join Date
    May 2008
    Location
    Ohio, USA
    Posts
    63

    [CS3][AS2] Nesting Objects Inside Other Objects?

    I'm trying to organize the massive amount of coding I'll need to do for an RPG I'm making before I get too far into it and realize I have to modify 1800 lines of code because I didn't plan things out ahead of time. Plus it'll make managing the game data easier, especially with saving. So what I'm trying to figure out is if it's possible to nest a set of objects within another object.

    For example, I want to have a main container object called charContainer, inside of which I'll have individual character objects (char1, char2, char3, etc.). Inside of those character objects I'll store all of the character-specific variables like stats and equipment. Then I'll do the same thing with my other variables for enemy stats, items, and everything else.

    So is this possible? Or is there a better way of organizing things? Any help would be appreciated.

  2. #2
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,223
    Should be possible.

  3. #3
    Student
    Join Date
    Apr 2001
    Location
    -
    Posts
    4,756
    just a simple guess:
    you haven´t discovered arrays in combination with objects before

    AS example
    PHP Code:
    var array= new Array();
    array.
    push({a:12,b:"name",c:0xff0000});
    array.
    push({a:23,b:"John",c:0xee6622});

    for (var 
    i=0;i<array.length;i++){
        
    trace(i+". item a: "+array[i].a+", b: "+array[i].b+", c: "+array[i].c);

    for any explainations read the manual that comes with Flash (f1 key) and look for
    - arrays
    - objects

  4. #4
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,223
    Yes but you dont need to use arrays, you can use objects which to me have always been easier to manage. Array is only useful if you need to know how many objects your container has (its length) and/or if you need to know the position of each object inside container (is it first, last, next etc).

  5. #5
    Member
    Join Date
    May 2008
    Location
    Ohio, USA
    Posts
    63
    Yeah, I figured I could use an array to sort everything, I just hate using them when I'm not dealing with numbers. I have a bunch of arrays set up for my characters' stats and a function that determines which level they are when they level up and then moves all of their stats up to the next level, but for all my variables I'd rather just be able to reference them like this:
    PHP Code:
    function equipmentStats (CHAR) {
        if (
    charContainer.CHAR.weapon "weapon1"){
            
    charContainer.CHAR.weaponSTR 12
        
    }

    Then I'd fill in the rest of the weapons, armor, accessories, etc. and when I call the function I'd replace "CHAR" with whichever character is being equipped with something. This way I just have to write one function, then call it for each individual character. Then when I need to save my game data I can just do this:

    PHP Code:
    function saveGameData() {
        
    gameDataSO SharedObject.getLocal("gameData");
        
    gameDataSO.data.charData charContainer;
        
    gameDataSO.flush();

    The way I have it set up right now, I have a bunch of objects for 4 characters, 50+ enemies, 111 items, about 20 weapons... you get the idea. I'd rather consolidate all the related objects into one large container so I at least have some hierarchy and so I can just save those containers in my saveGameData function instead of saving each individual object.

    Anyway, what I really need to know is how I would go about actually creating the nested object. Is it just this? -

    PHP Code:
    containerObject = new Object
    containerObject
    .nestedObject = new Object 

  6. #6
    Script kiddie VENGEANCE MX's Avatar
    Join Date
    Jun 2004
    Location
    England
    Posts
    2,590
    I'm not convinced that nested objects can be saved... when I tried saving anything other than strings and numbers in Commando 3, I eventually had to give up and make my own proprietary data format (in the form of a single long string).
    http://www.birchlabs.co.uk/
    You know you want to.

  7. #7
    Member
    Join Date
    May 2008
    Location
    Ohio, USA
    Posts
    63
    Quote Originally Posted by VENGEANCE MX
    I'm not convinced that nested objects can be saved... when I tried saving anything other than strings and numbers in Commando 3, I eventually had to give up and make my own proprietary data format (in the form of a single long string).
    How exactly did you do that? Building the string itself would be easy enough but how would I retrieve the information from that string and redistribute it to all the variables when the player loads the saved game?

  8. #8
    Script kiddie VENGEANCE MX's Avatar
    Join Date
    Jun 2004
    Location
    England
    Posts
    2,590
    Something like:

    PHP Code:
    myString "player1|100|true
    player2|75|false"
    ;

    myArray myString.split("\n"1000);
    // myArray = ["player1|100|true", "player2|75|false"];

    for (i=0i<myArray.lengthi++) {
    array2 myArray[i].split("|"1000);
    // array2 = ["player1", "100", "true"]; or other. btw it's a string "true" not a Boolean
    createPlayer(array2[0], array2[1], array2[2]); // pass these values to createPlayer() function

    Something like that?
    http://www.birchlabs.co.uk/
    You know you want to.

  9. #9
    Member
    Join Date
    May 2008
    Location
    Ohio, USA
    Posts
    63
    So basically what you're doing is telling Flash that every time there's a return (\n) in the string, it separates the string into different array values, then when it finds a | in the array you just made, it further splits them into individual values and puts them into a 2nd array? Seems simples enough. What's the 1,000 for after you tell it where to split them?

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