Hello Guest

Author Topic: Serializing and Deserializing Tile Map  (Read 4244 times)

MaddoScientisto

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 4
    • View Profile
Serializing and Deserializing Tile Map
« on: April 10, 2015, 02:08:36 pm »
I've purchased this great toolkit about a week ago and I've been very pleased so far by the possibilities it offers.

I have made a few TileMaps and I've been writing code to piece the maps together into bigger maps, so far I've been saving each TileMap as a prefab and loading them up. This is getting too cumbersome though, I'd rather just serialize the map to a text/xml/whatever file and then both in the editor and ingame just load up that file, instance a new tilemap and build it back up according to the previously saved data.

I've been looking at the documentation and tried to make a few loops to try and catch all the data so right now I have the following for each tile:
- tile id
- tile coordinates
- tile flags (saved as string for now because I have no idea what they are supposed to contain yet)
- tile color (saved as string but I have no idea what it can be used for)
- tile layer number

Also I grabbed the editorDataGUID and the name of the "data" object.

Where is data stored?
Where is color stored (was it just that color variable from the tile?)?

Is this information enough to rebuild a fully working tilemap object?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Serializing and Deserializing Tile Map
« Reply #1 on: April 10, 2015, 06:31:13 pm »
You need to iterate through the layers, x & y, and for each Layers[layer].GetRawTile(x, y).
That will give you the tile including flags.

And for x & y, tileMap.ColorChannel.GetColor(x, y)
That'll give you the colors (not necessary if you don't add a color channel)

Data stores the layers in the tilemap. Editordata isn't required if you're creating things at runtime.
Don't forget to call Build after you finish setting what you need to.


i agree prefabs is cumbersome, but to stitch larger worlds consider having all the parts in their own scene and LoadLevelAdditive. It'll be way way faster than creating a tilemap procedurally which is what you'll have to do if you Build it at runtime.
« Last Edit: April 10, 2015, 06:33:08 pm by unikronsoftware »

MaddoScientisto

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Serializing and Deserializing Tile Map
« Reply #2 on: April 11, 2015, 11:48:40 am »
Thank you for the answer!

instead of rawtile I used
Code: [Select]
var flags = square._tileMap.GetTileFlags(i, j, l);
var tileId = square._tileMap.GetTile(i, j, l);
(where i is tile x, j is tile y and l is layer, I discard tiles -1)

is it the same thing?


Also I didn't think about loadleveladditive, that sounds really good, also because it seems to have an async version.

My goal is to make a big map like metroid but of course just load the "rooms" (rooms are a collection of tilemaps).
I wanted to be able to have the whole map loaded in the editor at the same time to properly place everything but maybe I can just do the serialization thing for the editor and load the maps the way you said ingame instead.

I'l look into it and I'll post my serialization code for somebody who might stumble into this post later, if I do end up doing the serialization.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Serializing and Deserializing Tile Map
« Reply #3 on: April 11, 2015, 12:08:47 pm »
var flags = square._tileMap.GetTileFlags(i, j, l);
var tileId = square._tileMap.GetTile(i, j, l);
This is fine, just stores 2x the data. Internally the tileId and flags are stored inside the RawTile. i.e.. you can get away with one int as opposed to 2 per tile.