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.
Anyone know how I can improve this to make it work without lag?
Any help would be appreciated
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).
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)?
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?
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.