1
\$\begingroup\$

Conway's Game of Life

I have been working with a tilemap for the implementation of a cellular automata game. My current method for getting mouse button input is using an Area2D as a child of the Tilemap and detecting mouse button input like that.

I have done this and have since been wondering if there is something built in that I can use for this. I have looked into it and cannot find anything better.

Is there something built-in to Node2Ds or Canvas Items that I can use.

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

The recommended way would be to override Node._unhandled_input() and use TileMap.world_to_map() and TileMap.map_to_world() methods.

Example:

# tile_map.gd
extends TileMap

func _unhandled_input(event):
    if event is InputEventMouseButton:
        if event.button_index == BUTTON_LEFT and event.pressed:
            var clicked_cell = world_to_map(event.position)

Do note that world_to_map() takes a local position. Node2D supplies the to_global() and to_local() methods to ease the conversion.

I recommended using _unhandled_input() rather than _input() so gui input events are not passed through to the tile map. If the the tile map is undesirably receiving mouse events when you interact with a control, make sure that the control's mouse_filter property is not set to "Ignore".

\$\endgroup\$
7
  • \$\begingroup\$ And if I have controls that sometimes overlap the tilemap. your solution was what I tried out before I ran into that problem. So you have any way around that? Thanks in advance! \$\endgroup\$ Commented Jul 8, 2020 at 5:30
  • 2
    \$\begingroup\$ Can you expand on what problem you face when controls overlap the tilemap? \$\endgroup\$
    – hola
    Commented Jul 8, 2020 at 5:33
  • 1
    \$\begingroup\$ I click on the controls and end up setting one of the tiles underneath it. \$\endgroup\$ Commented Jul 8, 2020 at 5:35
  • 2
    \$\begingroup\$ The events shouldn't propagate to _unhandled_input() unless if all the Control's that overlap have their mouse filter property set to ignore. \$\endgroup\$
    – hola
    Commented Jul 8, 2020 at 5:40
  • \$\begingroup\$ I must have been doing it wrong then. Thank you. I was not using unhandled input previously. \$\endgroup\$ Commented Jul 8, 2020 at 5:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .