+1 vote

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.

in Engine by (146 points)

1 Answer

+2 votes
Best answer
  1. Use a TileMap it is optimized and has specific Methods !
  2. Use a Dictionary

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

by (282 points)
selected by

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.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.