I have a board scene and a tile scene.
The tile emits a signal if click on:
extends RigidBody2D
var description = "Blank"
signal touched
func _ready():
pass
func _input_event(_viewport, event, _shape_idx):
if event is InputEventMouseButton \
and event.button_index == BUTTON_LEFT:
print("emit")
emit_signal("touched")
When I run the main scene, I see "touched" logged in the console.
In Board.gd
, I add multiple tiles to the board and I connect tile's "touched" signal to _tile_touched
. However, that method does not seem to be called.
Board.gd
:
extends Node2D
var tileScene = preload("res://Tile.tscn")
var tiles = []
func _ready():
tiles = $TileSet.tiles
for y in 5:
for x in 5:
var new_tile = tileScene.instance()
var width = new_tile.get_node("Body/Sprite").texture.get_width()
var position = Vector2(width * x, 0)
# Connect signal
new_tile.connect("touched", self, "_on_tile_clicked")
_add_tile_to_board(new_tile, position)
# Pause for a bit so not to add all tiles at once
yield(get_tree().create_timer(0.0001), "timeout")
func _add_tile_to_board(new_tile, position):
add_child(new_tile)
new_tile.position = position
func _on_tile_clicked():
print("Received touch")
I expect to see "Received touch" in the console but nothing appears. Have I connected the signal correctly? Why isn't _on_tile_clicked
called?