Create Save Games

A place for tutorials on how to get the most out of Flash

Create Save Games

Postby MegaPlayboy » Fri Sep 04, 2015 1:29 am

Hey everyone. I have not the slightest idea to allow a player to save a game to be loaded later. I assume that certain variables/arrays/classes can be saved to a cookie? How does this work and how can I accomplish it?

Any help would be greatly appreciated.
"Thank you Mega-playboy! You make us complete and we will love you forever!!"
User avatar
MegaPlayboy
 
Joined: Wed Aug 05, 2015 1:25 am
Location: United States

Re: Create Save Games

Postby GoRepeat » Fri Sep 04, 2015 12:40 pm

As2 or As3? The method is going to be slightly different although the concept is still the same. To save data to a local cookie file, you need to create a Shared Object Variable, name it, then you just store and recall data from that shared object.

Code: Select All Code
import flash.net.SharedObject;

public var gameData:Array;
public var saveData:SharedObject;

saveData = SharedObject.getLocal("com.mySaveFileName","/");


Then you create a new array in the game to hold all your save data, and copy it to the 'data' attribute of your save cookie. In this example, I used a new array on the shared object called "mainSlot" - if, for example, you wanted multiple saves (like in LoKvG), you could add other arrays to save the other data (like mainSlot, secondSlot, thirdSlot etc etc).

Code: Select All Code
saveData.data.mainSlot = new Array();
gameData = new Array();
gameData[0] = theFirstVariableIWantToSave;
gameData[1] = theSecondVariableIWantToSave;
gameData[2] = theThirdVariableIWantToSave;
gameData[3] = theEtcEtcEtcVariableIWantToSave;
saveData.data.mainSlot = gameData;
saveData.flush();


To load the data later, you just do the opposite: take the saveData.data.mainSlot Array and copy it back to your variables:

Code: Select All Code
gameData = saveData.data.mainSlot;
theFirstVariableIWantToSave = gameData[0];
theSecondVariableIWantToSave = gameData[1];
theThirdVariableIWantToSave = gameData[2];
theEtcEtcEtcVariableIWantToSave = gameData[3]


Make sense? If you are ever looking for the 'physical' copy of the save file, it will be in your adobe system folder under the name you stuck up in the first part of the code ("com.mySaveFileName"). There are some other ways to do it, like actually making a text file and loading/saving to it, but for web based games, cookies are just easier and no fuss no muss.
Picarto LiveStream: https://picarto.tv/GoRepeat
Other Stuff: Click Here
User avatar
GoRepeat
Moderator
 
Joined: Wed Jul 21, 2010 2:26 am
Location: Behind the Looking Glass

Re: Create Save Games

Postby MegaPlayboy » Wed Sep 09, 2015 12:11 am

Got it working, thanks. Now all I have to do is develop my game to the point where saving has a purpose, haha.
"Thank you Mega-playboy! You make us complete and we will love you forever!!"
User avatar
MegaPlayboy
 
Joined: Wed Aug 05, 2015 1:25 am
Location: United States

Re: Create Save Games

Postby Anonymouse » Fri Sep 11, 2015 10:49 pm

Building on GoRepeat's examples, using an associative array makes this a lot easier:

Code: Select All Code
// saving
gameData = new Object();
gameData["variable1"] = variable1;
gameData["variable2"] = variable2;
gameData["variable3"] = variable3;
saveData.data.mainSlot = gameData;
saveData.flush();

// loading
variable1 = gameData["variable1"];
variable2 = gameData["variable2"];


In case you aren't familiar with the Object class, it's worth noting that you can create them and assign variables in several ways:
Code: Select All Code
var someData : String = "blah";

// method 1 - string literals
var obj = new Object();
obj["variable1"] = someData;

// method 2 - string variables
var obj = new Object();
var string1 = "variable1";
obj[string1] = someData;

// method 3 - we don't even need brackets
var obj = new Object();
obj.variable1 = someData;

// method 4 - curly brace creation (because woe betide actionscript notation is consistent)
// note that the variable names are written without speech marks and there is no new keyword
var obj = {variable1 : someData, variable2 : someOtherData};
User avatar
Anonymouse
 
Joined: Sun Nov 24, 2013 2:47 am


Return to Tutorials



Who is online

Users browsing this forum: No registered users