I have a script in GDScript that contains a simple mouse position listener, and a set_cell function that sets a cell. For some reason, when I set the cell on my simulation, it doesn't spawn. The mouse position is set correctly, and the set cell position, when set to (1, 1), works. But for some reason. This does not. Here is my code:

func _process(delta):
    var mouse_pos = get_viewport().get_mouse_position()
    var mx = mouse_pos.x
    var my = mouse_pos.y
    if Input.is_action_just_pressed("click"):
        set_cell(mouse_pos.x, mouse_pos.y, 0)

If you find a mistake, feel free to tell me, otherwise, have a nice day, and I appreciate your time.


1 Answer 1


I'm going to bet it is creating them out of view.

First of all, you are getting the mouse position relative to the Viewport:

var mouse_pos := get_viewport().get_mouse_position()

And second set_cell wants map coordinates. We can convert local coordinates to map coordinates with world_to_map. But first we need to get the mouse position in local coordinates.

We can try getting it out of get_viewport().get_mouse_position(), which I believe would be like this:

var mouse_pos := make_canvas_position_local(get_viewport().get_mouse_position())

Or better yet, take the mouse position in local coordinates already:

var mouse_pos := get_local_mouse_position()

Once we have the local mouse position, we can convert local coordinates to map coordinates:


And since that is a vector, let us use set_cellv:

var mouse_pos := get_local_mouse_position()
# …
set_cellv(world_to_map(mouse_pos), 0)
  • \$\begingroup\$ It worked! Thanks! \$\endgroup\$
    – Athsmooth
    Apr 2 at 14:07

You must log in to answer this question.

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