Hello Guest

Author Topic: Procedural Tile Map Issues  (Read 7888 times)

Wuzseen

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 9
    • View Profile
Procedural Tile Map Issues
« on: October 19, 2013, 09:15:21 pm »
I'm trying to do a basic random tile map to generate a space background.  At a certain point, when I need to generate more of the map I increase the size of the map and then I want to iterate over the whole map and set tiles on all new spaces.

Code: [Select]
        int oldHeight = map.height;
int oldWidth = map.width;
int newHeight = (map.height *= 2);
int newWidth = (map.width *= 2);
print (oldHeight + " " + oldWidth);
print (map.height + " " + map.width);
int tileCount = map.data.tilePrefabs.Length;
map.Build();
int a;
for(int i = 0; i < map.height - 1; i++) {
a = 0;
for(int z = 0; z < map.width - 1; z++) {
print (a);
a++;
if(map.GetTile(i,z,0) < 0)
map.SetTile(i,z,0,Random.Range(0,tileCount));

}
}
map.Build();

That code has some of the debugging stuff in it, but you should see the process.

Anyway, the problem I'm having is that GetTile throws an array out of bounds exception whenever I get past a value of 64 on the x/y coordinate of the tile.  I'm not entirely sure why this is happening and more importantly how I can fix it.  Since I've reset the height of the map shouldn't this not be a problem?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Procedural Tile Map Issues
« Reply #1 on: October 19, 2013, 11:21:06 pm »
You can't change the tilemap size at runtime intentionally. Simply create the largest tilemap size you'd ever need - the store system is partitioned, and an empty 1024x1024 tilemap doesn't occupy a lot more than an empty 10x10 tile map. I'm happy to add some more API functions to resize, etc. but in almost 100% of cases, it isn't really necessary.

Wuzseen

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Procedural Tile Map Issues
« Reply #2 on: October 19, 2013, 11:26:47 pm »
I could make an arbitrarily large map to begin with.  In my game it is theoretically possible to go on forever though; the background would grow in size exponentially as well, not linearly.  It's also mobile, if the partitions become large enough is it possible to run into some of the limitations mobile platforms have with texture size?  Maybe I'm not thinking about it straight.  If my map is 1024x1024, does that translate to 1024x1024 tiles or 1024x1024 of 'pixels'.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Procedural Tile Map Issues
« Reply #3 on: October 19, 2013, 11:35:46 pm »
1024x1024 is just 1024x1024 sprites / tiles. It has nothing to do with texture sizes, that is sourced from the sprite collection. Everything is partitioned, so for 1024x1024 empty tiles, the overhead is negligible.

Wuzseen

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Procedural Tile Map Issues
« Reply #4 on: October 20, 2013, 02:22:54 am »
Fantastic, I think it will be hard, if not impossible, for any player to reach the limits if I have it set to 1024x1024.

When the scene starts though all the objects change to the default render layer and their transforms set back to the origin; I'm rendering this with its own background camera and they need to be on a BG layer.  Is there a work around from the tk2d side of things?
« Last Edit: October 20, 2013, 02:39:46 am by Wuzseen »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Procedural Tile Map Issues
« Reply #5 on: October 20, 2013, 11:44:51 am »
You can set up the unity layer per tilemap layer, in tilemap inspector > Settings > Layer. That should keep the layer to what you set it to.

Wuzseen

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Procedural Tile Map Issues
« Reply #6 on: October 20, 2013, 08:17:55 pm »
Great, thanks!  Think there's a way to anchor the center of the tile map from its center instead of the bottom left?  With 1024x1024 tiles there could potentially be a variable "center".  The idea would be to build from the center out as opposed to the bottom left to the top right.

I'll write up a tutorial on this I think.  I've encountered a couple people who have tried to roll some procedural effects with the tilemaps in tk2d but have run into a few hiccups here and there.
« Last Edit: October 20, 2013, 08:19:52 pm by Wuzseen »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Procedural Tile Map Issues
« Reply #7 on: October 20, 2013, 10:40:52 pm »
There isn't a way to anchor the center to anything but the bottom left. It should be easy to offset in code though.
Remember though - if you want to change tilemap stuff at runtime (i.e. not at level load, but while the game is running), you should probably try to avoid using the collider system - it is really expensive to update colliders in Unity, but updating the mesh is fine.

Wuzseen

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Procedural Tile Map Issues
« Reply #8 on: October 20, 2013, 10:43:32 pm »
I'm just generating a static background--completely uninteractive in terms of collision.  It just grows/shrinks overtime.  Thanks for the heads up!

whale

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 29
    • View Profile
Re: Procedural Tile Map Issues
« Reply #9 on: January 10, 2014, 03:05:01 am »
Sorry to resurrect a dead thread, but I figured it would be better to keep this information in one place.

It seems that setting your tilemap dimensions to be very large (1024x1024) and then saving your scene serializes all of the render mesh objects.

This results in LONG save and load times, and a 250+ MB scene file.

Would it not be easier to just resize the tilemap at runtime?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Procedural Tile Map Issues
« Reply #10 on: January 10, 2014, 11:21:01 am »
Hi,
I'm thinking of adding a "Create Tilemap" api at runtime with no resize support. That might work around that issue.