class_name Grid
extends Node2D
@export var row := 5
@export var column := 5
@export var cell_size := 150
func _ready():
for i in range(row):
for j in range(column):
var cell = ColorRect.new()
cell.color = Color.WHITE
cell.size = Vector2(cell_size, cell_size)
cell.position = Vector2(j * cell_size, i * cell_size)
add_child(cell)
#for debugging
var border = Line2D.new()
border.default_color = Color.BLUE
border.width = 1 # set the border width to 1 pixel
border.points = [Vector2(0, 0), Vector2(cell_size, 0), Vector2(cell_size, cell_size), Vector2(0, cell_size), Vector2(0, 0)] # set the border points to form a rectangle
border.position = Vector2(j * cell_size, i * cell_size)
add_child(border)
var label = Label.new()
label.text = str(j) + "," + str(i) # set the label text to the cell's row and column number
label.set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER)
label.set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER)
label.position = Vector2(j * cell_size, i * cell_size)
label.size = Vector2(cell_size, cell_size)
label.set("theme_override_colors/font_color", Color(1, 0, 0))
add_child(label)
cell.connect("mouse_button_pressed", on_cell_clicked)
func on_cell_clicked(event):
if event is InputEventMouseButton:
if event.pressed:
var cell_index = event.position / cell_size
print(str(cell_index))
I have this code now but its not printing anything at the moment. So it's not activating the on_cell_clicked function yet. I am getting this error in the compiler when I run the scene. My grid is showing perfectly
Attempt to connect nonexistent signal 'mouse_button_pressed' to callable 'Node2D(gameboard.gd)::on_cell_clicked'.
<C++ Fout> Condition "!signal_is_valid" is true. Returning: ERR_INVALID_PARAMETER
<C++ Source> core/object/object.cpp:1256 @ connect()
<Stack Trace> gameboard.gd:32 @ _ready()
thanks
best regards