Rows and Columns Array Issue

Godot Version

godot-4.6.1

Question

I am really really new to coding and Im doing this assignment for a class. I am trying to make a 4x4 tile sliding puzzle game and I’ve been following a bunch of guides to do it.

There is this section where, to check your work, you should be able to click each tile and it will produce the row and column number in the output. In the video I’m watching, it should be from 0-3 in both rows and columns, but my column number is coming out at 2-5 every time. This is causing me issues later in the coding process but I cannot for the life of me figure out what I did wrong!

Apologies for not being able to attach a video of the issue as I am a new user, but here is a chunk of the code!

func _process(delta):

if Input.is_mouse_button_pressed(MOUSE_BUTTON_LEFT) and mouse:

var mouse_copy = mouse
print(mouse.position)
mouse = false
var rows = int(mouse_copy.position.y / 135)
var cols = int(mouse_copy.position.x / 135)
print(rows, “,”, cols)

func find_empty(position,pos):

var new_rows = int(position.y / 135)
var new_cols = int(position.x / 135)
var new_pos = new_rows * 4 + new_cols
if tiles[new_pos] == $Tile16:

swap_tiles(pos, new_pos)
return true
else:
return false

1 Like

Hello there! I know it may sound annoying and that you’ve probably checked but when you are moving the mouse around the scene does is it’s position the expected one relative to the cells of the matrix? Also, I usually use get_global_mouse_position() to get mouse position, maybe it can prove useful if you are not too dependent on relative coordinates.

Feel free to share a screenshot of the scene tree too! Might come in handy to understand the issue :smile:

Are you sure 135 is correct? Maybe its 137.
Any posibility of margins between the tiles.

You might want to provide a link to the video as well. Interesting that it seems to be coming out at +2 from what you’re expecting.

1 Like