What is the performance impact when saving & loading from a SharedObject?


(billy2000) #1

I whanted to begin makeing the saveing process to my game and I have seen this post:

All I whant to save is just 3 vectors 1 int and 1 string variables so ill use zachwlewis’s code. But i still wander something.How much time would take for a slower computer to save the data? Or it will going to be saved in a single frame?If not ,how can i see when the save data process is finished? I am asking this becose i whant to make the save data as a world called at the end of each level,and after the save process is complete to go to the next level/worldMap world.


(rostok) #2

This amount of data should save pretty instantly. I wouldn’t worry at all if it’s going to happen at the end of level.


(billy2000) #3

Tank you :smile: ill beign the working then xD


(billy2000) #4

But 1 more question. When the player palyes the game for the 1st time he cant load data cuz there is no data.What should i do in this case?I mean to not load the data is the game if played for the 1st time.


(Ultima2876) #5

If you’re using a SharedObject the data won’t actually be ‘flushed’ (or written to disk) until you force it to flush (could do this between levels) or the app is ended. That means that ‘saving’ is 100% instant because all it’s doing is setting up a few variables ready to be actually written at a later time.

If you can’t load data then just set up the defaults that you’d like for the first time the game is played. After having a quick look at the serialize class in the link you posted, it looks like it handles this anyway. All you have to do is make sure your classes set the right default values for your vars.


(billy2000) #6

you mean this part of code?

// load

ba.position = 0; // read from start   
`myPlayer = Serialized.deserialize(ba)`;
enemy = Serialized.deserialize(ba);

never saved a game and rly dont know how to make a default save data >.<


(billy2000) #7

If ill set a default to a variable that meens the next time the game will be played will not use the defaults but the save data right?


(billy2000) #8

hmm i taked a closer look …so u mean this part?

if (!saveData) return;

but i whanted to use zach’s saveGame class. Should i use byteArray?


(billy2000) #9

i took a look in the documentation at data methods. So after all u mean the default values from readbol,readint and readstring? xD


(Ultima2876) #10

If you’re using zach’s class, these lines are the relevant ones for setting default values in case there is no saved data to load:

currentLevel = so.data.hasOwnProperty("playerLevel") ? so.data.playerLevel : 1;

What this does is checks if the save data has a saved value for ‘playerLevel’ - if it does, it loads it, if not, it sets it as 1. 1 in this case being the default value. If you wanted to store a high score, for example, you might have:

playerHighScore = so.data.hasOwnProperty("playHighScore") ? so.data.playerHighScore : 0;

If there’s no stored high score, you want it to be initially set as 0. If there is a high score in the save data, load the ‘playerHighScore’ from the data.


(billy2000) #11

to load i tried something like this …is it good? O:

G.Coins = Data.readInt("TotCoins", 0);
G.LifeLvl = Data.readInt("life", 1);
G.LifeDed = Data.readBool("lifeShop", false);
G.towerState = Data.readString("state", "no");

(billy2000) #12

I mean does the default variables are add there too?


(billy2000) #13

ok guys so i did this code and it seems that defaults are working O:.

package  

{ import net.flashpunk.Entity; import net.flashpunk.graphics.Backdrop; import net.flashpunk.World; import net.flashpunk.FP; import flash.ui.Mouse; import net.flashpunk.utils.Data;

/**
 * ...
 * @author ...
 */
public class SaveGameW extends World 
{
	[Embed(source = "../assets/WorldMap/Wall up.png")]private const LEFT:Class;
	[Embed(source = "../assets/WorldMap/Wall down.png")]private const RIGHT:Class;
	private var Lwall:Backdrop = new Backdrop(LEFT, false, false);
	private var Rwall:Backdrop = new Backdrop(RIGHT, false, false);
	private var left:Entity;
	private var right:Entity;
	private var timer:int = 10;
	private var i:int = 0;
	private var changer:int;
	private var where:int;
	
	public function SaveGameW(whatDo:String,whereTo:int) 
	{
		FP.camera.y = -44;
		left = addGraphic(Lwall);
		right = addGraphic(Rwall);
		left.layer = -100;
		right.layer = -100;
		//so equal on half
		left.y = -221;
	    right.y = 67;
		where = whereTo;
		
		if (whatDo == "load") {
			loadData();
		}
	}
	override public function begin():void
	{
		FP.camera.y = -44;
	}
	override public function update():void
	{
		super.update();
		
		FP.camera.y = -44;
		changeLevel();
		Mouse.hide();
		
	}
	private function saveData():void	
	{
		
	}
	private function loadData():void
	{
		G.Coins = Data.readInt("TotCoins", 0);
		G.LifeLvl = Data.readInt("life", 1);
		G.LifeDed = Data.readBool("lifeShop", false);
		G.towerState = Data.readString("state", "no");
		
		G.Treasure.length = 10;
		G.TreasureRelative.length = 10;
		G.CoinsFinal.length = 100; 
		G.Levels.length = 22;
		
		for (i = 1; i <= 9; i++) {
			changer = i + 0;
	        G.Treasure[i] = Data.readBool(changer.toString(), false);
		}
		for (i = 1; i <= 9; i++) {
			changer = i + 0;
			G.TreasureRelative[i] = Data.readBool(changer.toString(), false);
		}
		for (i = 1; i <= 99; i++) {
			changer = i + 100;
			G.CoinsFinal[i] = Data.readInt(changer.toString(), 0);
		}
		for (i = 1; i <= 21; i++) {
			changer = i + 10;
			G.Levels[i] = Data.readInt(changer.toString(), 0);
		}
	}
	private function changeLevel():void
	{
		timer--;
		if (timer < 0) {
			if (where == 0) {
				FP.world = new WorldMap();
			}
		}
	}
}

}

THX guys :smiley: