- Home /
I am making a level editor and need ideas on how to save the level file.
I have been creating my own level editor for my game and i have it to the point where you can make your level, but now i need a way to save the level to a file. i am not sure on the best way to do this and i am looking for suggestion. Basically all i need it to do is save the type of object, its position and scale and any variable associated with that object. Right now i am looking to be able to run my game on desktop and webplayer, but i am looking to migrate it to mobile later too, in case that matters. Thanks in advance for any and all help.
What kind of level editor ? 2D/3D ? What kind of data to store ? Object + behaviours + components ? Basically : how complexe is it ?
it is 3d but it is a 2d side scroller so i only care about 2 dimensions and it just needs to store what type of object(ie door, wall, character), its position and scale, and some other variables for certain objects.
Id suggest writing custom serializers for each class you care about. Then all you have to do is go through your objects and Serialize them and append to file. That or just go with a simple Json save/load method, but that's not as slick.
I have seen a lot about the serialization, but I don't really understand how it works. Do you have any sites/tutorials that would explain it to me?
Basically you write 2 methods in your class. One that saves/Serializes, another that loads/Deserializes.
The loader/Deserializer will receive what the saver/Serializer writes as input. So you can assume some things about your input.
You can use whatever means you want, but I tend to build a dict or hashtable, then use a JSON lib to convert it to text. Simple, transferable, and uses standard .NET objects for building.
Say you have a complex building hierarchy that looks like this:
BuildingGrp |-Window |-Walls |--Door |-Stairs
You could have 1 script on the grp called Building, and another script on the door called Door. When it deserializes it, you could write out a list of lists (tree) representing your hierarchy to text. Then when you Deserialize it, you can rebuild the heirarchy with gameobjects, and add any necessary sub-scripts (Door).
Basically ins$$anonymous$$d of having one monolithic Save/Load method, you let each object do their own saving and loading. Then in your main save/load, you tell everyone else to save/load and compile it to one file. $$anonymous$$akes for much cleaner and simpler uses.
Also super useful when you want to transfer say a Building definition over a network to your other game clients. BOO$$anonymous$$, just serialize and send.
Hope I'm not confusing you more than helping...
Answer by Fabkins · Jan 15, 2014 at 06:40 PM
Here is something that saves the content of 100x100 tile map. Could be anything really. The format of the file is text and is the object type ("tile" only in this example") and then however many variable for your objects. This is separated by ":". Means you need to ensure you dont use ":" for anything else.
function SaveMap(filepathIncludingFileName : String)
{
Debug.Log("Saving....");
var sw : StreamWriter = new StreamWriter(filepathIncludingFileName);
var y:int;
for(y=0;y<100;y++)
{
var x=0;
for(x=0;x<100;x++)
{
sw.WriteLine("Tile:"+x.ToString()+":"+y.ToString()+":"+metaMap[x,y].tileType.ToString());
}
}
sw.Flush();
sw.Close();
Debug.Log("Save Complete");
}
function LoadMap(filepathIncludingFileName : String) {
Debug.Log("Loading....");
var sr = new File.OpenText(filepathIncludingFileName);
var input = "";
while (true)
{
input = sr.ReadLine();
if (input == null) { break; }
var paramaters = input.Split(":"[0]);
switch( paramaters[0])
{
case "Tile":
var x: int=System.Int32.Parse(paramaters[1]);
var y: int=System.Int32.Parse(paramaters[2]);
var i: int=System.Int32.Parse(paramaters[3]);
SetInternalMap(x,y,i);
break;
}
}
completeUVoperation();
sr.Close();
Debug.Log("Load Complete");
}
i have one question with this. Would it work if i deploy my game to a website?
No , you would need to post something on the server and have server side code to do it.
You would need to post a form with whatever metadata. Use WWWForm . I would create a random unique identifier that you could reference for retrieving the save files or have a log on method. If you had something unique you could store that in the PlayerPrefs. Shame you cant save a file in the same place that PlayerPrefs does.
If you had really small amount of data, you could always use PlayerPrefs, but not really designed for keep maps and the such.
Can't you just write it to a text file on the server, then read it back?
For my game, I generate unique game ids using the current time + the game host name, as it is impossible for the same host to start multiple games at the same time. Then I salt it and md5 it to make it a little more abstracted.
Also, no PlayerPrefs is not something you want to save levels to. That'd be bad.
Your answer
Follow this Question
Related Questions
UnitySerializer Only Saving one Game Problem 0 Answers
Save File for Android Game.. 2 Answers
Save scene 1 Answer
Save Data Strategies 1 Answer
Save and Load 2 Answers