Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

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

Discussion in 'Scripting' started by AndreasGan, Jan 15, 2015.

  1. AndreasGan

    AndreasGan

    Joined:
    Feb 27, 2014
    Posts:
    16
    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 :)

    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).
     
    Last edited: Jan 15, 2015
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Don't use string keys.
    try an intVector2, or hash the coords
     
    AndreasGan likes this.
  3. AndreasGan

    AndreasGan

    Joined:
    Feb 27, 2014
    Posts:
    16
    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)?
     
    Last edited: Jan 15, 2015
  4. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    String comparisons are slow, so yeah it might
     
    AndreasGan likes this.
  5. AndreasGan

    AndreasGan

    Joined:
    Feb 27, 2014
    Posts:
    16
    I forgot to mention that I've tried vector2 without a difference.
     
  6. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    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
     
    AndreasGan likes this.
  7. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,840
    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?
     
    AndreasGan likes this.
  8. AndreasGan

    AndreasGan

    Joined:
    Feb 27, 2014
    Posts:
    16
    Sorry. I had a script running I forgot all about. :oops: Thanks for the help though :)
    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.
     
  9. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    The answer is voxels. Google it.
     
    AndreasGan likes this.
  10. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,840
    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
     
    AndreasGan and Kiwasi like this.
  11. AndreasGan

    AndreasGan

    Joined:
    Feb 27, 2014
    Posts:
    16
    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? :/