A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: Transfer multiple values from one object to another?

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

    Question Transfer multiple values from one object to another?

    Hello,

    I am working on an RPG-type game, and in this game I've defined multiple objects for characters, named char1, char2, etc, then assigned the objects values such as Health, Magic, Strength, etc. I then created a dummy object with all of these same values, except every value is set to zero. There are some 30 values in every object.

    I would like to see if I could do something like:

    Code:
    dummychar = char1;
    to transfer all of the values from char1 to dummychar instantly without having to define every value transfer, however, I can't seem to find the correct bit of code, because doing the above seems to erase all the values from the dummychar object completely.

    The point of this excersize is the make the dummychar object a placeholder so I can switch char1 with char2, meaning char1 would take char2's place, and vice versa. The dummychar object will hold the values for char1, then char2's values will overwrite char1's values, then the dummychar's values will overwrite char2's.

    So, in pseudocode:

    Code:
    dummychar = char1;
    char1 = char2;
    char2 = dummychar;
    My current way of doing this includes telling every single value in the char objects to transfer, but that takes up quite a bit of coding space, and I was wondering if there is an easier way.

  2. #2
    Prid - Outing Nig 13's Avatar
    Join Date
    Jul 2006
    Location
    Norway
    Posts
    1,864
    I didn't really understand that. Could you please provide a FLA file, and/or show more with details?

    And what do you mena by object?
    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
    Here's the code better explained, I guess.

    I have, on the first frame, all the objects, variables and functions defined. Within the first frame, I have the following code:

    Code:
    char1 = new Object();
    char2 = new Object();
    char3 = new Object();
    char4 = new Object();
    dummychar = new Object();
    function setup(character){
    	character.namee = "NAME";
    	character.classs = "CLASS";
    	character.level = 1;
    	character.skill = 1;
    	character.str = 0;
    	character.agi = 0;
    	character.vit = 0;
    	character.intel = 0;
    	character.spirit = 0;
    	character.att = 0;
    	character.attx = 0;
    	character.hit = 0;
    	character.def = 0;
    	character.defx = 0;
    	character.evade = 0;
    	character.mdef = 0;
    	character.mdefx = 0;
    	character.mevade = 0;
    	character.bmattx = 0;
    	character.wmattx = 0;
    	character.bmhit = 0;
    	character.wmhit = 0;
    	character.bmatt = 0;
    	character.wmatt = 0;
    	character.hp = 1;
    	character.maxhp = 1;
    	character.mp = 0;
    	character.maxmp = 0;
    	character.row = "front";
    }
    setup(char1);
    setup(char2);
    setup(char3);
    setup(char4);
    setup(dummychar);
    char1.namee = "PLAYER1";
    char2.namee = "PLAYER2";
    char3.namee = "PLAYER3";
    char4.namee = "PLAYER4";
    This code defines 4 player objects and a dummy player (a placeholder), because this RPG will include 4 people in your party. Then it uses my function "setup" to define several values attached to each object, such as HP or MP, their name, etc., and to further the point that the dummy character is simply a placeholder, I do not name it, instead, I keep the default name "NAME".

    The game then proceeds to ask you to name your characters and pick their class. Depending on what you choose for the class defines the rest of the stats with zero as a placeholder. If you enter the game with all of your characters set up and such to be nice and functional, and go into your pause menu, you have the option to switch the character order in your party. It is my wish to, essentially, instead of having to define that "char1" is first, "char2" is second, etc, that I could simply switch the values between objects, and have the program keep the order of char1, char2, char3, and char4.

    To achieve this, this is why I created the dummy character. Let's say I wanted to switch char1 and char2's position. I wish to, in the switching process, store char1's stats in the dummy character so that the two objects are identical. Then, I wish to overrwrite char1 with char2's stats, making char1 and 2 identical. Then, I wish to overrwrite char2's stats with the dummy character's stats, which would actually be char1's stats. Therefore, in essence, I would have switched the two characters.

    My problem is that I want to avoid this sort of code:

    Code:
    char1.namee = char2.namee;
    char1.classs = char2.classs;
    char1.level = char2.level;
    char1.skill = char2.skill;
    char1.str = char2.str;
    char1.agi = char2.agi;
    //and then obviously finish the rest of the stats
    because not only is that a huge block of text with all of the assorted stats I have set up, but I would have to copy/paste that sort of code at least 3 times to make the transfers complete, as well as having to go in and edit that part of the program if I ever decided to add or subtract a certain stat from the characters.

    What I WANT to have is something a lot simpler, say a line of code I'm missing and can't find, that essentially transfers every single value from one object and stores it into another, completely overrwriting what may or may not have been in it, making the two objects completely identical, except for their name.

  4. #4
    Senior Member whispers's Avatar
    Join Date
    Mar 2001
    Location
    CFA2h (respect the HEX)
    Posts
    12,756
    this is correct.. doing that is really just duplicating the reference..and not the data..

    it can be as simple as a lopp to re-write all the properties..

    not sure how this will work when you get custom properties?

    anyways.. try this:

    actionscript Code:
    char1 = new Object();
    char2 = new Object();
    //dummychar = new Object();
    function setup(character) {
        trace("TARGET CHAR: " + character);
        character.namee = "NAME";
        character.classs = "CLASS";
        character.level = 1;
        character.skill = 1;
    }
    setup(char1);
    setup(char2);

    char1.namee = "PLAYER1";
    char1.level = 10;
    char2.namee = "PLAYER2";
    char2.level = 25;

    button_mc.onPress = function() {
        trace("MY NAME IS: " + char1.namee);
        trace("MY LEVEL IS: " + char1.level + newline);
    };
    button2_mc.onPress = function() {
        trace("MY NAME IS: " + char2.namee);
        trace("MY LEVEL IS: " + char2.level + newline);
    };
    button4_mc.onPress = function() {
        switchPosition(char1,char2);
    };

    function switchPosition(target1, target2) {
        var target1_name:String = target1.namee;
        var target2_name:String = target2.namee;   
        //dump char 1 data to temp obj
        for (var p in target1) {
            dummychar[p] = target1[p];
        }
        //update char1 data with char2 data
        for (var q in target2) {
            target1[q] = target2[q];
        }  
        //finally update char2 with tempholder data
        for (var s in dummychar) {
            target2[s] = dummychar[s];
        }
        //replace names
        target2.namee = target2_name;
        target1.namee = target1_name;
    }

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