Hello Guest

Author Topic: [TileMap] Edit tilemaps during gameplay  (Read 9722 times)

iopixel

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 6
    • View Profile
[TileMap] Edit tilemaps during gameplay
« on: December 26, 2012, 10:18:29 pm »
Hi,

I would like to edit my tilemap during the game (add and remove one or multiple tiles). I understand it can take some time (for collider and mesh creation). What is the best way to do that ?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: [TileMap] Edit tilemaps during gameplay
« Reply #1 on: December 27, 2012, 06:34:12 am »
tileMap.Layers[0].SetTile(x, y, tileId);
After changing what you want, call
tileMap.Build()

You can optimize tilemap construction by changing the partition/chunk size. tk2d only rebuilds changed chunks.

iopixel

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: [TileMap] Edit tilemaps during gameplay
« Reply #2 on: December 27, 2012, 01:27:36 pm »
I made a simple test.

One tilemap of 512x512 tiles, chunks 32x32, when I remove one block (tileMap.Layers[0].SetTile(x, y, -1) and then tileMap.Build()), it takes 2000ms.

When I try this on a 32x32 tilemap, it takes less than 5ms to remove one block. Should I make my own "chunks" with multiple 32x32 tilemaps or did I forget something ?

Note: when profiling I see TileMapCollision.Update() takes 1950ms.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: [TileMap] Edit tilemaps during gameplay
« Reply #3 on: December 27, 2012, 02:08:03 pm »
It depends on the contents. The collision update is what takes the longest as you've found - if you can do without it, then you might be able to get away with it. 16x16 chunks will be a lot faster to update, though the collision update will still be significant.

iopixel

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: [TileMap] Edit tilemaps during gameplay
« Reply #4 on: December 27, 2012, 03:26:24 pm »
The strange thing is: it only takes 5ms on a 32x32 tilemap, why does it take so much time on a 512x512 tilemap with 32x32 chunks if only modified chunks are rebuilt ?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: [TileMap] Edit tilemaps during gameplay
« Reply #5 on: December 27, 2012, 04:01:12 pm »
I'd step through the function to see what gets rebuilt. It should only rebuild modified chunks, but perhaps it thinks more chunks are modified? Also, if certain blocks don't have colliders, the collider won't be regenerated.

Its hard to say why without looking at the data.

iopixel

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: [TileMap] Edit tilemaps during gameplay
« Reply #6 on: December 27, 2012, 04:24:26 pm »
Sure, Can I send you a sample project ?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: [TileMap] Edit tilemaps during gameplay
« Reply #7 on: December 27, 2012, 05:05:15 pm »
Certainly - support@unikronsoftware.com

Pfaeff

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: [TileMap] Edit tilemaps during gameplay
« Reply #8 on: March 06, 2013, 02:19:43 pm »
I need to edit only the color layer of my tilemap at runtime (preferably every frame), so I need to call tilemap.Build(), which seems to work, but is still very inefficient. I tried to create a build method, that would only do the things I need (I think it just had a call BuilderUtil.CreateRenderData()). Unfortunately, it was just as slow. Is there a way to make this work faster? I think the alternative would be using a custom mesh overlay for the whole tilemap, but I'm not sure how efficient that would be.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: [TileMap] Edit tilemaps during gameplay
« Reply #9 on: March 06, 2013, 03:58:24 pm »
The efficiency is directly related to the size of the tilemap partition.
Perhaps you could find a number that works better for you by reducing the partition size?

Pfaeff

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: [TileMap] Edit tilemaps during gameplay
« Reply #10 on: March 06, 2013, 04:15:19 pm »
Thank you. Does it mean that if you only make changes in a certain chunk, the update would be more efficient, because only that particular chunk would be marked as "dirty" and thus being the only one that is updated?

EDIT: I was using 32x32 sized chunks. Now with 16x16, I get about 10fps more. 8x8 is actually worse. The "radius" in which I update the color layer is 10x10, so I think 16x16 is a good choice.
I will also decrease the update rate, so that this is called in certain time intervals instead of every frame.

Thanks again.
« Last Edit: March 06, 2013, 04:22:37 pm by Pfaeff »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: [TileMap] Edit tilemaps during gameplay
« Reply #11 on: March 06, 2013, 04:41:34 pm »
Yup that's it exactly.
There is some funky stuff which happens at borders. The border colors are repeated in adjacent chunks, so if you were to update at the border, you'd be updating up to 4 chunks at a time. If you can stagger the updates (modify the tilemap build so it only ever builds one chunk every frame, or something like that) you may get a huge performance boost at smaller chunk sizes. Its worth a quick experiment with that.

Pfaeff

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: [TileMap] Edit tilemaps during gameplay
« Reply #12 on: March 06, 2013, 05:13:57 pm »
Thank you very much. Yeah, I figured that if the radius in which I update the colors is smaller than the chunk size, I would have a maximum of 4 chunks to update. The size of those chunks I want to keep to a minimum of course, so 16x16 seems fine for now. I will have a look at your suggestion about building only one chuck per frame. What I do at the moment is I am building every 100ms and it still looks fluid enough, though the framerate has increased dramatically. I have to do some further testing, though. Thank you for your awesome support.