Sign in to follow this  
MatejaS

Best way to store tile data

Recommended Posts

Hello,

I am currently making a top-down game.Levels are tile-based.I have multiple levels and all of them are using different tiles (textures).

I know that Minecraft for example uses a different class for each tile.So is it a good idea?

I can make a class for each tile and store all the tiles into an array,like:

Tile[] tiles = new Tile[] {
 new GrassTile(),
 new SandTile(),
 ...
};

and then:

int[] level1 = new int[] {
 0, 0, 0, 1, 0,
 0, 0, 1, 1, 0
};

Array level1 stores indices in the tiles array(0 represents GrassTile, and 1 SandTile,...)

Is this a good approach?

 

Sorry for bad explanation...

Thanks,

MatejaS

Share this post


Link to post
Share on other sites

Your approach ought to work well enough but you don't need the indirection of referring to tiles by index. Instead, you can simply store a reference to the tiles themselves. Something like this:

Tile grass = new GrassTile();
Tile sand = new SandTile();
...
Tile[] level1 = {
    grass, grass, grass, sand, grass,
    grass, grass, sand, sand, grass,
    ...
};

This is called a "flyweight" pattern, by the way.

Share this post


Link to post
Share on other sites

For games with tons of tiles and tons of processing, you often have N bytes for each grid cell. Those are then interpreted differently depending on context (the first byte could be the tile type, which determines the meaning of the rest of the bytes. If its stone, the next bytes could index a stone carving database. If its water, the next bytes could contain the water pressure).

 

Basically if performance is a problem, think of it in terms of pure data. Bytes that either contain the data itself, or index an array somewhere else like a pointer. And its more like serialization, where the bytes can be interpreted differently depending on context, since the tile grid needs to be packed into memory efficiently.

 

However, if youre not working on a multiplayer infinite procedurally generated tile based universe with real time light and fluid simulation, you might want to take a simpler approach at first. Perhaps just set up a 'Tile' interface and have the tile grid contain references to instances of classes implementing that interface. That would allow you to easily write up a class for a new tile type. Change approach if there are performance issues.

 

But yeah your approach seems ok. Maybe think of how to add tile specific data (in addition to the tile specific tile type which you already have), and that should be pretty flexible.

Edited by Waterlimon

Share this post


Link to post
Share on other sites


I know that Minecraft for example uses a different class for each tile.So is it a good idea?
No. Not really. Don't hold Minecraft as any sort of standard for game coding.

 

As Waterlimon said, you can express what kind of properties your tiles will have in a very compact manner. So the difference between each tile its just their properties, the data they hold.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this