[Solved] Tilemap System? My method is too slow.

I’m trying to figure out how I can make a tilemap system. I need to be able to build and remove tiles real-time ingame.

So I made something up:
Each tile has a size equivalent to 1 world unit, so I’ll spawn them 1 world unit apart, creating a nice tiled map.
Then I created a dictionary that stores the position of the tiles (vector2) in keys, so that I can easily find out if a “tile spot” is occupied - by using dict.ContainsKey()

However as the dictionary gets larger, the time it takes to call dict.ContainsKey() is a bit too long.
With only a few (~20) tiles, the game freezes for ~0.5 secs before the tile appears on the screen.

Here’s a paste of the code if you want to have a look at my approach: http://pastie.org/private/ldzjscipbqolas9yeh64aq

Anyone know how I can improve this to make it work without lag?
Any help would be appreciated :slight_smile:

Edit:
I just realized that I have to have at least 1000 tiles, so my method probably wont be efficient enough.
Is there any better method I could use? I’ve heard about mesh stuff, but would that work in my case, where I want to be able to build and remove tiles in-game, and there can be special blocks (e.g. some cant be removed).

Don’t use string keys.
try an intVector2, or hash the coords

1 Like

Would using an intvector2 / hashing the coords really change that much?

Anyways, I just realized that I have to have at least 1000 tiles, so my method probably wont be efficient enough.
Is there any better method I could use? I’ve heard about mesh stuff, but would that work in my case, where I want to be able to build and remove tiles in-game, and there can be special blocks (e.g. some cant be removed)?

String comparisons are slow, so yeah it might

1 Like

I forgot to mention that I’ve tried vector2 without a difference.

Is the posted code is really all you’ve got, I can’t imagine it taking half a second to spawn a tile, even with a dictionary check.

nb. regular vector2s as keys may be prone to false negatives

1 Like

I’m also highly skeptical that the dictionary lookup has anything to do with it. Dictionary lookups are very fast and essentially constant time regardless of the number of entries.

@AndreasGan , can you back up and tell us exactly what you’ve measured and how?

1 Like

Sorry. I had a script running I forgot all about. :sweat_smile: Thanks for the help though :slight_smile:
I disabled the other script, and it works like it should now.

Anyways, I think I’ll look into a better method for tilemaps, as this is going to be very inefficient, having one gameobject for each block.

The answer is voxels. Google it.

1 Like

Yes, that’s very likely true. Not because of the dictionary, but because of all those GameObjects.

I think the right way to do this is with a dynamic Mesh. If that seems daunting, I’m sure you can find a good grid/tile system on the asset store, or maybe even something open-source. I’m sorry I don’t have any specific recommendations handy, though.

Good luck,

  • Joe
2 Likes

Thank you for the help and suggestions guys.
I forgot Toolkit2D has a tilemap feature, so I’ll be using that because I have tk2d.

I can’t close the thread? :confused: