Jump to content
  • Advertisement
Sign in to follow this  
3DModelerMan

Unity Game level/scene file format

This topic is 2570 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi. I'm working on writing an engine and an editor, but I've ran into a problem with scene file formats. The engine uses a component based entity system similar to Unity 3D. I want it to support parenting of objects. The problem is that when writing the game objects to binary files, I can't figure out a way to write child game objects. I have the GameObject class implement a Save(FileWriter* writer) function (and reader functions). And then I just call Save on the root GameObject when I need to save a level. The game objects have unique IDs that are also saved to the scene files, and at the end of their Save/Load functions they call Save and Load on their child objects. I want to use binary files so that players can't modify them, and because of speed. How do you usually go about saving and loading these kinds of objects? And also, if I just used XML scene files in a password encrypted archive file (to prevent players from modifying them), would it be that much slower?

Share this post


Link to post
Share on other sites
Advertisement

Hi. I'm working on writing an engine and an editor, but I've ran into a problem with scene file formats. The engine uses a component based entity system similar to Unity 3D. I want it to support parenting of objects. The problem is that when writing the game objects to binary files, I can't figure out a way to write child game objects. I have the GameObject class implement a Save(FileWriter* writer) function (and reader functions). And then I just call Save on the root GameObject when I need to save a level. The game objects have unique IDs that are also saved to the scene files, and at the end of their Save/Load functions they call Save and Load on their child objects. I want to use binary files so that players can't modify them, and because of speed. How do you usually go about saving and loading these kinds of objects? And also, if I just used XML scene files in a password encrypted archive file (to prevent players from modifying them), would it be that much slower?

A simple way is, to save all your entities in a sqeuence independently of their parent/child relationshipment. The trick is, to save the unique IDs of the parent. When loading, you first create and read all entities and in a postprocessing step, reconnect all childs according to their parent ID.

Here's some pseudo code:

class Entity:
List childList;
int parentID;


write:
for each e : entity do
e.writeToFile( file)
end

read:
Map<int,Entity> newlyCreatedEntityMap = ...;

// read first all entities
while !endOfFile(file) do
Entity* e = new Entity();
e.readFromFile(file);
// add entity to map
newlyCreatedEntityMap.put( e.getID(), e);
end

// reconnect entities
for each Entity e in newlyCreatedEntityMap do
int parentId = e.getParentID();
// valid id ?
if parentID!=0 then
// get parent
Entity p = newlyCreatedEntityMap.get( parentID);
// connect now
p.addChild( e);
end
end

Share this post


Link to post
Share on other sites
I'm working on writing an engine and an editor... I can't figure out a way to write child game objects. I have the GameObject class implement a Save(FileWriter* writer) function (and reader functions). And then I just call Save on the root GameObject when I need to save a level... I want to use binary files so that players can't modify them, and because of speed.
I had a similar problem some time ago. I had a serious complication however, I could not envision a system to allow save/restore of a selected object subset. In the end, I've settled on a system which produces a description of how to build objects and connect them (instead of describing the objects directly). So far, it appears to work and allows some really nice tricks. Too bad it's still far from release.
I wouldn't mind about performance. Get this ready to go first, polish it later.


Share this post


Link to post
Share on other sites
Another option would be to consider writing your binary files in chunks using a combination of header/payload blocks. The reason behind this is that you can store some bookkeeping information in the header blocks such as version, time stamp, size, etc that all can be useful in reconstructing the payload from the file. I've found this particularly useful in many scenarios and not only games, when I suspected the format or layout would change over time. This allowed me to patch the code files with ease and still be able to read older versions of saved files which were automatically converted to the newer format upon save/shutdown.

The concept Ashaman73 shows above would work well in conjunction to what I outlined above. Naturally entity structures may change or newer components may require more payload data as you extend your framework and having a way to easily do this without affecting your storage is crucial for backward compatibility of older saved game files or formats.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!