I got my game framework's map editor working to an extremely functional state recently. Loading and saving along with the collision map. It's designed to export the map as an array [width][height] whereas the position of a number can easily identify it's x position and it's y position. The number itself represents the tile from the tile set to draw. So in the text file, the map data could look like this: (ex. only) 0001110002222111000. So it's in a straight line. but it gets parsed.
So here's the problamo I've come across. The tile number can only be 0-9. Otherwise "11" will fill two spots in the array and cause run-time problems. That means can only allow the editor to store up to 10 tiles at a time which is not a realistic quantity for a game imho.
So what do you think I should do to avoid this? It took forever to write it to work like it is and rewriting it is something I'd rather not do to support more than 10 tiles. Opinions and advice people! Share em! :D
I thought of using characters. That would definitely expand the tile set limit, but would it be enough? There are 26 letters in the alphabet. Uppercase counts as another 26. Then the 0-9. So that would give a total of 62 tiles that can be used in a game. What's the average? Maybe google will help. I'm sure it would be enough though. Using a delimiter would make me have to rewrite it. I know how to, but I really don't want to. :P
I made the map get converted into ASCII characters 33-126 (127 is DEL). So the map editor supports up to 94 tiles. I'm pretty sure that's enough. And that's 94 tiles in a level. It can easily load another tile set for another level. :D Thanks banister.
why dont you use groups of 3 numbers per tile? for my zombies game i made a tilemap class which allows 1000 different tiles, because each tile is represented by 3 numbers (000, 457, etc). you only need to parse the array from 3 to 3. Try it, is cool.
For levelsaving, the best is the Marshal class, which saves objects to file. It can save hash with level settings, array of tiles, etc. Tiles named with symbols can have unlimited number of types.
Example of saving level: @level={'name'=>'new_level', 'width'=> 25, 'tiles'=>[Tile,Tile,Tile...]} Marshal.dump(@level, f=File.new("maps/#{@level['name']}.lvl",'w')) f.close
For load: @level=Marshal.load(File.new("maps/#{@level['name']}.lvl",'r'))
A possible solution nobody has mentioned is to use Array#pack and String#unpack to save the numbers as raw bytes or whatever. If you thing 256 tiles (well, 255 + blank) is enough and you're ok with binary files, then tile_data = tiles.pack('C*') will give you a binary string (not human readable, obviously) and something like tiles = tile_data.unpack('C*') will give you back that array.
If 256 isn't enough (you crazy fellow) then you could use a different format string: n* would convert the numbers to unsigned 16-bit integers, giving you 65536 possible values. As a bonus, the N and n specifiers both use network (big-endian) byte order so it's the same on Intel, PowerPC and everywhere else.
If you don't want to save a binary file (for instance, if you intend to store other information in a human readable format) then you could Base64 encode it, although that will bloat the file size. You could also use zlib compression before you encode it, but now I'm going a little beyond the scope of the discussion. :)
Yeh I'm actually thinking about rewriting it again anyway. I completely forgot about the properties of the tiles (alpha value and color modifiers) so I'm gonna work on the new method and if I get any prpblems, I'll update it here. I'm thinking about using something similar to that Marshal class. Woo. Work. :P