How To Save A Non-Grid-Based Level For A Game

Started by
3 comments, last by Mayday556 9 years, 9 months ago

I've been wondering for a while now: "How does a level editor save a level to be loaded up again?" and after searching around on the internet, I found that there are a few ways to do this. 1) Save the Level as a bunch of text, where 1 letter is a different block than another or 2) Save the Level as a bunch of pixels in an image where 1 pixel color is a different block than another pixel color.

Notice how both of these methods of saving rely on the level being grid-based. Well what about a level that isn't grid-based? There could be any number of objects, at any position possible! So, how does saving a complex level work?

Advertisement

There are loads of ways of doing it.

How a level is saved will depend greatly on what specific things you need saved, and how the game will interpret them.

One way to solve your specific issue is to store a position and a rotation, either as some floats and some rotation angles or something, or by basically having the entire 4x4 matrix stored in the level's file.

Made-up example:

Object_Start

ObjectName = "MyObject"

ObjectType = "AwesomeObject"

ObjectPos = 100.023f, 39.5f, -882.0f

Object_End

Then, in this example when loading, the editor/game would see a new object block (from the "Object_Start" keyword), would assign its name and type based on the next keywords, and put it in the exact position you wanted it, based on the position data.

Hello to all my stalkers.

Like Lactose wrote, there are loads of ways of doing it. It'd probably be best for you to start out using some standard format like XML or JSON, instead of inventing your own text or binary format. Then you'd have a file like

<map>

<object id="MyObject">

<type class="MyAwesomeObject" />

<position x="100.023" y="39.5" z="-882" />

<rotation z="23" />

</object>

...

</map>

or

[

{

id: "MyObject",

type: {

class: "MyAwesomeObject"

},

position: {

x: 100.023,

y: 29.5,

z: -882

},

rotation: {

z: 23

}

}

...

]

Your language of choice will most likely an API for easily serializing/deserializing objects to text like this.

Depending on the size of your level, you can either keep it in memory as a flat list of objects like this, or you might need a spatial partitioning datastructure, like a simple 2d array or a quadtree, to contain the objects, so you dont have to traverse them all in order to touch the objects in a smaller area.

google gleed2d for a map editor that isn't grid based, and uses xml to store the levels.

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

Thanks a lot everyone, this really cleared things up for me!!!

This topic is closed to new replies.

Advertisement