Array iteration speed

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By CalmTurtle

I have a scene with any number of tiles, (usually around 2500). A tile is a Node2D extended object.

The limiting factor in my map creation speed is whenever I need to search for a specific tile. In my implementation I have tried two methods. One by storing all the tiles in a separate array and searching through it. The other by searching through the node tree for the item. Keeping a reference to the objects in the array and searching that way has reduced my search time by about 30%, but I need some more juice.

Here is my current implementation:

func tile(location) -> Node2D
     for item in tiles:
          if item.location == location:
               return item
     return null

Is there a way to use find() by looking for not just the specific object but one of its attributes? And would this be faster? I feel like in the backend it would be doing the same thing I am doing above.

Thank you in advance for any insight.

:bust_in_silhouette: Reply From: brainbug
  1. Use a TileMap it is optimized and has specific Methods !
  2. Use a Dictionary

var tiles := {}
tiles[location] = tile

tile = tiles[location]

Wow… I mean wow. When I was researching this project I decided early on that I wouldn’t use a dictionary to hold my tiles. I had read somewhere that keys in GDScript had to be integers… :\

Thanks a ton for smacking me in the mouth and opening my eyes.

CalmTurtle | 2019-04-08 18:01