• Register

Trash scape is a real-time city builder based in a world where the earth has become inhospitable for human life. The game is designed so the player can build their dream sky city, whether it is a slum, or a pristine city with a full power grid. In order to create the perfect city the player will need to build power routes, construct farms and trade with merchants and slavers. The game was built in 7 days in ludum dares 7day rts comp.

Report RSS DevBlog #1: developing a save/load system.

Saved games are all about preserving and restoring state, which developing for is in essence 100% evil!

Posted by on

Saved games are all about preserving and restoring state, which developing for is in essence 100% evil!

At least that's what I thought when I started planning how I would do this. Fortunately the actual process of making one turned out much easier than I originally thought.

So how did I do it. (**ALL CODE SNIPPETS USED ARE PROBABLY PSEUDO-CODE**)

Basicly it works like this, when an object is created (lets so a house) it activates a script attached to itself that:
1: checks how many other saved objects in the world exist from the main logic object that controls all the important variables.

otherObjects = logic.transform.GetComponent<logic>().count;

it then increases the number of objects in the world by 1 (this is to account for itself)

logic.transform.GetComponent<logic>().UpdateObjectsCount();

It then saves the objects position, rotation and the type of object it is ( in this case a house)

Save.SaveVector3("BuildPosition" + count,transform.position);
Save.SaveQuaternion("BuildRotation"+count, transform.rotation);
Save.SaveInt("Type"+count, type);

So now the player has made a house and quit the game, he then reboots the game and continues is save file. This then loads an empty scene where the aforementioned logic game-object checks to see if there are any saves and if yes re-creates the object type (house) at the position and rotation that was saved using something along the lines of:

void Start () {
		count = Load.LoadInt("count");
		for(int x = 0; x<count; x++){
			int type = Load.LoadInt("Type"+x);
			Vector3 spawnPos = Load.LoadVector3("BuildPosition"+x);
			Quaternion spawnRot = Load.LoadQuaternion("BuildRotation"+x);
			Instantiate(cube[type], spawnPos, spawnRot);
		}
	}

Its important to understand that when the object is re-created by the logic object it does NOT activate the original script which saved the position, rotation and type of object when it was originally made by the player, this would cause the same object to be saved twice and then remade twice. Here is a video of it in action

Post comment Comments
TheSniperFan
TheSniperFan - - 376 comments

Thanks for reminding me of what I need to look into in Unity. Totally forgot about learning how to implement a game saving mechanism in Unity.

Looks good btw.

Reply Good karma Bad karma+3 votes
Post a comment

Your comment will be anonymous unless you join the community. Or sign in with your social account: