Can I reference underlying scene placed using SceneCollections on a Tilemap by its Vector2i coordinates?

Godot Version



Hi there (thanks for popping in!),

I recently learned that we can use scenes as tiles in tilemaps!

This is really powerful to me as I can then code methods and properties to scenes that are instantiated in the tilemap.

But I can’t find resource as to how I can reference the added child scene in the TileMap via their cell coordinates relative to the Tilemap.

How can I access the instanced scene child , say… at Vector2i(1,2) of the TileMap, so as to trigger a method of that SceneTile. Is there a way I can do this?

Even if there’s no built-in method/function to achieve this, is there a elegant work around? E.g. Converting the Vector2i of the tilemap to its global position, then using it somehow to reference the scene tile placed at that location?

Thanks in advance!

I don’t understand what you want to do. I hope you can describe more details.

Hi, sorry for not being clear.

I made custom tile blocks for an isometric tilemap, each saved as a Scene. An example of such a scene here:

  • These scenes are custom and can have scripts/components attached to them.
  • An example of such a component, would be an animation that can play when the attached Area2D detects a mouse over event.

I then placed these on the tilemap using a SceneCollectionSource tileset.

Since cells on tilemaps have a corresponding reference Vector2i coordinate, e.g. (0,0), (0,1)…
I wanted to know if I can access the underlying Scene thats instantiated as a tile on the tilemap at a particular Vector2i.

An example situation would be when I have an event that would affect a range of tiles on the tilemap (e.g. Vector2i(0,0), Vector2i(0,1), Vector2i(0,2)). I want to reference the child scenes planted as tiles on these coordinates in the tilemap, to invoke methods in their scripts.

I’ve seen a lot of people ask this question. The solution that I came up with was to:

  1. get all of the children of the tilemap
  2. convert each one to their cell location in the tilemap
  3. compare that cell location to the one I’m looking for
func get_scene_at(grid_position: Vector2i) -> Node:
    for node in tilemap.get_children():
        var grid_position = grid_index_from_position(node.global_position) # This is a utility function that I wrote to convert between position and cell
        if node.x == grid_position.x && node.y == grid_position.y:
            return node
        return null

If your tilemap is sufficiently large and you’re worried about time, you could create an array with the children and index into it directly.