Hello, I got stuck trying to decouple my node scripts with signals. I have two nodes, Player and FightGrid (a TileMapLayer), and they are on the same level of hierarchy.
I created a custom signal in the FightGrid scripts, and it returns the calculated path for the player to follow when clicking a tile. My problem is that I also need to get the player position (need to change "global_position" from FightGrid), but I don't want to use a direct reference from Player in the FightGrid script.
I want to only use signals or a pattern that decouples Nodes that are on the same level, or maybe I could make Player a child of FightGrid. I don't know what's the best stategy.
fight_grid.bd:
`
extends TileMapLayer
signal select_tile(path)
var astar_grid: AStarGrid2D
func _ready() -> void:
astar_grid = AStarGrid2D.new()
astar_grid.region = get_used_rect()
astar_grid.cell_size = tile_set.tile_size
astar_grid.diagonal_mode = AStarGrid2D.DIAGONAL_MODE_NEVER
astar_grid.update()
func _process(delta: float) -> void:
if Input.is_action_just_pressed("mouse-left"):
var start_cell := local_to_map(global_position)
var goal_cell := local_to_map(get_global_mouse_position())
var path_map := astar_grid.get_id_path(start_cell, goal_cell).slice(1)
var path_local := path_map.map(func (tile): return map_to_local(tile))
emit_signal("select_tile", path_local)
`
player.gd:
`
extends Node2D
var current_path: Array
func _physics_process(delta: float) -> void:
if current_path.is_empty():
return
var target_pos = current_path.front()
global_position = global_position.move_toward(target_pos,3)
if global_position == target_pos:
current_path.pop_front()
func _on_fight_grid_select_tile(path_local) -> void:
if !path_local.is_empty():
current_path = path_local
`