Godot Version
4.5.1 stable
Question
In my game the player can place walls turrets etc on a buildinglayer (which adjusts a floor layer so navigation works)
I need to make it so if its not possible for my enemies to navigate to a location if they were to place that wall then it wont let them place the wall
currently I am just using print statements to try see if it gets it right I cant seem to workout why this will never print correctly and will always print its possible both when it is and when its not
extends Node2D
# Towers
@export_category("Placeables")
@export var simple_tower: PackedScene
@export var wall : PackedScene
@onready var PathTester = $PathTester
func _ready():
Globals.connect("new_item_selected", Callable(self, "_item_selected"))
print("AGENT MAP: ", PathTester.get_node("NavigationAgent2D").get_navigation_map())
print("TEST FLOOR MAP: ", $PathfindingTestFloor.get_navigation_map())
func _process(delta):
if Input.is_action_just_pressed("place_tower"):
if Globals.mouse_over_HUD == false:
if Globals.current_selection == Globals.build_options.WALL:
if ResourceManager.Packets >= 15:
ResourceManager.remove_packets(15)
var mouse_pos = get_global_mouse_position()
place_tower(mouse_pos, Globals.current_selection)
add_buffer(mouse_pos, $Floor)
else:
if ResourceManager.Packets >= 50:
ResourceManager.remove_packets(50)
var mouse_pos = get_global_mouse_position()
place_tower(mouse_pos, Globals.current_selection)
add_buffer(mouse_pos, $Floor)
else:
print("Not enough money")
func test_nav(world_mouse_pos, tile_coords): # test if enemy navigation can work after adding this tile
$PathfindingTestFloor.set_cell(tile_coords, 5, Vector2i(0, 0), Globals.current_selection)
add_buffer(world_mouse_pos, $PathfindingTestFloor)
$PathfindingTestFloor.notify_runtime_tile_data_update()
PathTester.get_node("NavigationAgent2D").target_position = Globals.last_node.position
$PathfindingTestFloor.notify_runtime_tile_data_update()
if PathTester.get_node("NavigationAgent2D").is_target_reachable(): # returns false if no path could be found
print("Target reachable")
else:
print("Target unreachable")
# Helper to create unique keys for occupied_cells dictionary
func _cell_key(cell: Vector2i) -> String:
return str(cell.x) + "," + str(cell.y)
func add_buffer(world_mouse_pos: Vector2, tilemap): # adds a buffer around a certain tile so that enemy navigation works properly
var mouse_local_pos = tilemap.to_local(world_mouse_pos)
var tile_coords = tilemap.local_to_map(mouse_local_pos)
# TOP
var tile_coords_top = Vector2i(tile_coords.x, tile_coords.y - 1)
tilemap.set_cell(tile_coords_top)
# DOWN
var tile_coords_bottom = Vector2i(tile_coords.x, tile_coords.y + 1)
tilemap.set_cell(tile_coords_bottom)
# LEFT
var tile_coords_left = Vector2i(tile_coords.x - 1, tile_coords.y)
tilemap.set_cell(tile_coords_left)
# RIGHT
var tile_coords_right = Vector2i(tile_coords.x + 1, tile_coords.y)
tilemap.set_cell(tile_coords_right)
# UNDERNEATH
tilemap.set_cell(tile_coords) # set it to air
tilemap.notify_runtime_tile_data_update()
print("Tile coords: " + str(tile_coords.x) + str(tile_coords.y))
func _item_selected(selected_item):
Globals.current_selection = selected_item
print(Globals.current_selection)
func place_tower(world_mouse_pos: Vector2, tower):
var mouse_local_pos = $BuildingLayer.to_local(world_mouse_pos)
var tile_coords = $BuildingLayer.local_to_map(mouse_local_pos)
test_nav(world_mouse_pos, tile_coords)
print("Source id: " + str($BuildingLayer.get_cell_source_id(tile_coords)))
$BuildingLayer.set_cell(tile_coords, 5, Vector2i(0, 0), Globals.current_selection) # tile coords, source id, atlas coords, alternative tile
print("Source id: " + str($BuildingLayer.get_cell_source_id(tile_coords)))
print("global mouse pos: " + str(world_mouse_pos))
print("tile coords: " + str(tile_coords))
Extra info you might find helpful:
the navigation agent used to test if its possible to get there (technically it never moves)
the test tilemap layer (basically a clone of my normal tilemap but I can place things on it etc and it wont affect the game)
Please any help is greatly appreciated

