A Flash Developer Resource Site

Results 1 to 14 of 14

Thread: Save game data

  1. #1
    Senior Member
    Join Date
    Apr 2009
    Posts
    138

    Save game data

    hi, I am trying to add a save and load feature to my fla.
    Currently on the same frame, I have; Load, Save, Delete.

    The content seems to save the array alright, but when I save "game one", and reload game one only is there. Then when I save "game two", game one and two are there. But if i reload, some random ones are added, from previous although I have them deleted..

    Also, I wondering how I would sort the shared objects.

    I am using localInfo = SharedObject.getLocal("savedgame"+e);
    so say i add
    SharedObject.getLocal.savedgame0
    and the info in the array [0][0] is "Eric"
    then i add
    SharedObject.getLocal.savedgame1
    info: "Bob"
    SharedObject.getLocal.savedgame2
    "Ryan"

    when I reload it will say
    bob
    ryan
    ryan

    Basically I have a feeling that
    localInfo = SharedObject.getLocal("savedgame"+my_list.selected Index);
    localInfo.clear();

    isn't working right. But when I performed a traced it said undefined..
    But then It messes up




    Any help would be greatly appreciated, And after I fix this bug, I will be improving these code and the features then I will share the finished example with every one!
    Actionscript Code:
    _level0.looptmp = [slot1=["", "abc", "dce"]];
    _level0.looptmp2 = [slot1=["", "", ""]];
    var my_list:mx.controls.List;
    for (var e = 0; e<100; e++) {
        localInfo = SharedObject.getLocal("savedgame"+e);
        if (localInfo.data.score == undefined) {
            emptyslot = e;
            e = 100;
            break;
        } else {
            savegamelabelinput = localInfo.data.score[0][0];
            my_list.addItem({label:""+savegamelabelinput+" #"+e, index:e, ref:e});
        }
    }
    function saveScore(tmpvar) {
        localInfo = SharedObject.getLocal("savedgame"+emptyslot);
       
        my_list.addItem({label:""+tmpvar[0][0]+" #"+emptyslot, index:emptyslot});
        localInfo.data.score = tmpvar;
        emptyslot += 1;
    }
    a.onRelease = function() {
        trace(my_list.selectedIndex);
        localInfo = SharedObject.getLocal("savedgame"+my_list.selectedIndex);
        if (localInfo.data.score == undefined) {
            abc.text = "nothing saved";
            //makes the score 0 as if starting a new game
            //with no saved data found
            score1 = 0;
        } else {
            _level0.looptmp2 = localInfo.data.score;
            trace(_level0.looptmp2[0][0]);
        }
    };
    b.onRelease = function() {
        _level0.looptmp[0][0] = savegamelabel.text;
        saveScore(_level0.looptmp);
    };
    c.onRelease = function() {
        localInfo = SharedObject.getLocal("savedgame"+my_list.selectedIndex);
        localInfo.clear();
        my_list.removeItemAt(my_list.selectedIndex);
    };
    Attached Files Attached Files

  2. #2
    When in doubt ask Eager Beaver's Avatar
    Join Date
    Feb 2007
    Location
    Planet Earth
    Posts
    911
    I tried your code, and nothing is happening. I don’t know how this fla. has to be played.
    Please give more information and details about your .fla. That is have I to drag the sprites on the stage to save them. Where the data gets stored? How to reuse the saved data?
    I am quite new to shared objects, and I will be very thankful to learn more about it.
    <signature removed by admin>

  3. #3
    Senior Member
    Join Date
    Apr 2009
    Posts
    138
    use this code instead. It still has some bugs to it.

    Actionscript Code:
    _level0.looptmp = [slot1=["", 0, "dce"]];
    _level0.looptmp2 = [slot1=["", "", ""]];
    var ese:Boolean = false;
    var emptyslot:Number;
    //Initalizes
    for (var e = 0; e<30; e++) {
        var localInfo:SharedObject = SharedObject.getLocal("savedgame"+e);
        //localInfo.clear();
        trace("Saved Var #"+e+" = "+localInfo.data.score);
        if (localInfo.data.score != undefined) {
            mylist.addItem({label:""+localInfo.data.score[0][0]+" #"+e, idxdel:e});
        }
        if (e == 29) {
            mylist.sortItemsBy(idxdel, "DESC");
        }
    }
    function saveScore(tmpvar) {
        if (emptyslot<30) {
            var localInfo:SharedObject = SharedObject.getLocal("savedgame"+tmpvar[0][1]);
            mylist.addItemAt(0, {label:""+tmpvar[0][0]+" #"+emptyslot, idxdel:tmpvar[0][1]});
            localInfo.data.score = tmpvar;
        } else {
            savegamelabel.text = "Max 30 saves reached!";
        }
    }
    savegame.onRelease = function() {
        for (var e = 0; e<30; e++) {
            trace(e);
            var localInfo:SharedObject = SharedObject.getLocal("savedgame"+e);
            if (localInfo.data.score == undefined) {
                if (!ese2) {
                    trace("empty: "+e);
                    emptyslot = e;
                    ese2 = true;
                    e = 31;
                }
                if (e == 31) {
                    ese2 = false;
                    trace("Save Game btn: "+emptyslot);
                    _level0.looptmp[0][0] = savegamelabel.text;
                    _level0.looptmp[0][1] = emptyslot;
                    trace(_level0.looptmp);
                    saveScore(_level0.looptmp);
                }
            }
        }
    };
    loadgame.onRelease = function() {
        trace(mylist.selectedIndex);
        var localInfo:SharedObject = SharedObject.getLocal("savedgame"+idxdel);
        if (localInfo.data.score == undefined) {
            abc.text = "nothing saved";
        } else {
            _level0.looptmp2 = localInfo.data.score;
            trace(_level0.looptmp2[0][0]);
        }
    };
    delgame.onRelease = function() {
        var localInfo:SharedObject = SharedObject.getLocal("savedgame"+idxdel);
        trace(idxdel);
        localInfo.clear();
        mylist.removeItemAt(mylist.selectedIndex);
    };
    mylist.change = function(evt:Object) {
        idxdel = evt.target.selectedItem.idxdel;
    };
    mylist.addEventListener("change", mylist);


    the data gets saved to a temp folder I think.

    vista
    C:\Users\user name\AppData\Local\Temp

    but I dont know.

    this info you want to save gets saved in this array

    _level0.looptmp = [slot1=["", 0, "dce"]];

    when loaded the info goes to

    _level0.looptmp2= [slot1=["", "", ""]];

    Slot1 is a catagory (actually its another array)

    you could do something like this
    var currenthealth:Number = 100
    _level0.looptmp = [Character=[ChosenGfx=0, currenthealth=currenthealth, maxhealth=maxthealth, currentxphealth=currentxphealth, currenthealthxptolvl=currenthealthxptolvl], Items=[slot1="hat",slot2="gloves",slot3="toy"], Area=[namee="Nolta", xx=5,yy=5,timeday="Monday",timehour="16",timemin=" 10"]];

    This one here sets everything blank and can be loaded into here.
    _level0.looptmp2 = [Character=[ChosenGfx="", currenthealth="", maxhealth="", currentxphealth="", currenthealthxptolvl=""], Items=[slot1="",slot2="",slot3=""], Area=[namee="", xx="",yy="",timeday="",timehour="",timemin=""]];

    then you can for example set things like the characters position
    gotoAndStop(_level0.looptmp2[2][0]); (this can be a label namee="name" or a number namee=5)
    player._x = _level0.looptmp2[2][1];
    player._y = _level0.looptmp2[2][2];

    then you set the health
    playerhealthcur = _level0.looptmp2[0][1];

    and so forth. You can do lots of things
    Last edited by brickhouse420; 01-02-2011 at 02:49 PM.

  4. #4
    When you know are. Son of Bryce's Avatar
    Join Date
    Aug 2002
    Location
    Los Angeles
    Posts
    838
    You can use a program like .minerva to view your shared objects (http://blog.coursevector.com/minerva ). FlashDevelop also has a shared object reader within it as well. Being able to look at the Shared Object will probably let you see what's actually going on.

    Also, I'm not sure if it makes a difference, but it might be a better idea to store all 3 games within 1 shared object. That's what I typically do. I'm not sure if you're somehow getting mixed it by loading 3 separate Shared Objects.

    So save your different games into...

    PHP Code:
    localInfo.data.game1 game 1 array;
    localInfo.data.game2 game 2 array;
    localInfo.data.game3 game 3 array; 

  5. #5
    Senior Member
    Join Date
    Apr 2009
    Posts
    138

    great

    Very good idea, and this way I don't have to reload the shared object over and over. But, it seems like any one can navigate to the folder with the shared object and open the file as a text. and basically use that to cheat.

  6. #6
    Funkalicious TOdorus's Avatar
    Join Date
    Nov 2006
    Location
    Nijmegen, Netherlands
    Posts
    697
    Quote Originally Posted by Son of Bryce View Post
    Also, I'm not sure if it makes a difference, but it might be a better idea to store all 3 games within 1 shared object. That's what I typically do. I'm not sure if you're somehow getting mixed it by loading 3 separate Shared Objects.
    I agree, a swf should only use one shared object. No need to clutter a users system up with unneeded files.

    You may want to look up the flush method of shared object, to force the swf to save. This makes sure the data gets saved, even if there is an unexpected ending of the session.

    At the moment you're only putting AS3 native objects (Strings and Arrays) in there, but if in the future you want to save custom classes/objects to yourshared object, check out registerClassAlias.

  7. #7
    Senior Member
    Join Date
    Apr 2009
    Posts
    138

    fixed

    Thank you guys for the valuable input.
    As far as the original question goes, I have fixed it and it runs perfectly.
    But I am still wondering how I can protect this data so people don't mess around with it.

  8. #8
    Senior Member
    Join Date
    May 2009
    Posts
    138
    Well, you can't really trust anything on the client side. There are libraries that let you encrypt data before saving and decrypt it when reading which will make it harder for people to cheat, but nothing will make it impossible.

  9. #9
    Senior Member
    Join Date
    Apr 2009
    Posts
    138
    can you link me to something I can use or a tutorial? Anything that can set me in the right direction.

  10. #10
    Senior Member
    Join Date
    May 2009
    Posts
    138
    I think the Mochi API has something you could use, although they advertise it for use against memory editing utilities I think you can use it for storing data in the Shared Object as well: https://en.mochimedia.com/support/dev_docs#MochiDigits

  11. #11
    Funkalicious TOdorus's Avatar
    Join Date
    Nov 2006
    Location
    Nijmegen, Netherlands
    Posts
    697
    You can always check it against an MD5 hashsum. Just take some properties which will be stored into the shared object, create a hashsum of it and store that hashsum in the shared object.

    When you load the game, take the properties of the shared object and recreate the hashsum. If the hashsum is the same as in the sharedobject, that means that (very probably) the file hasn't been tampered with.

    The problem is, that anyone decompling your swf can still find the MD5 check and remove it. But hey, anyone with that skill set, probably won't bother editing just the shared object, as he could as well hack the game itself. The only real way to be safe is to have a call to a server which creates and checks the hashsums, but that may be a bit overkill for a Flash game.

  12. #12
    Senior Member
    Join Date
    Apr 2009
    Posts
    138
    this md5 hashsum sounds interesting, I haven't messed around with things like it before. Do you have any good links, or should I search around?

  13. #13
    Funkalicious TOdorus's Avatar
    Join Date
    Nov 2006
    Location
    Nijmegen, Netherlands
    Posts
    697
    http://www.phpbb.com/kb/article/diff...n-and-hashing/
    http://www.**********.com/publish/tu...rd_encryption/
    For some reason FK blocks the string pixelTWOlive (replace the TWO with 2). So replace the *********** for that.

    AS3 example

    Code:
    import com.adobe.crypto.MD5;
    
    var salt:String = "your custom salt here";
    var toHash:String = property1.toString() + property2.toString();
    var hash:String = MD5.hash(toHash + salt); //MD5 is a static class

  14. #14
    Senior Member
    Join Date
    Apr 2009
    Posts
    138
    this will work for as2 as well? (because that is what I am using)
    aslo
    http://www.pixel2live.com/ doesn't work

    i see you mean life.
    Last edited by brickhouse420; 01-18-2011 at 11:16 AM.

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