• Register
Post news 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

Comments
TheSniperFan
TheSniperFan

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
Sign in or join with:

Only registered members can share their thoughts. So come on! Join the community today (totally free - or sign in with your social account on the right) and join in the conversation.

News
Tags
Dev Diary
Browse
News
New
Post news
Share
Related Games
TrashScape
TrashScape Real Time Strategy
Related Engines
Unity
Unity Commercial