First of all, thank you so much for taking the time to respond – and for such a helpful diagram. I understand generally the code you posted, and what it should do. My issue is knowing where to parse it within my code now. For some reason Godot is giving me issues with Vector2i arrays, and I’m not quite sure why. I’m also drawing null values with simple things like player.global_position.
(The error I get with the arrays is “Invalid operands ‘Vector2’ and ‘Vector2i’ in operator ‘+’.”)
The below code I am posting works – for changing 1 tile. I tried adding in the new proposed variables under the other variables, tried making a function, and tried creating range for the adding cells. My player.gd script has “var player_direction: Vector2” which works in many instances, but for some reason I am having trouble calling in this code.
You can see too that I have distance within the get_cell_under_mouse method, and that is working well and I can change that up nicely. The player’s global position is called in there, and it works – for some reason I can’t get it to work in other instances.
I think I have a gap of something foundational here on my end. For me it’s always best to learn by making mistakes and trying to solve the puzzle. I tried, so now I’m back! (For context: new to Godot!)
Thanks in advance for any further tips. Btw, the origin tiles should all be adjacent to the player, directional (top, bottom, left, right – never diagonal), and then generate the 3x3 tile square which you nicely showed with the gray.
class_name ChangeTerrainComponent
extends Node
@export var topsoil_tilemap_layer: TileMapLayer
@export var new_tilemap_layer: TileMapLayer
@export var terrain_set: int = 0
@export var terrain: int = 1
var player: Player
var mouse_position: Vector2
var cell_position: Vector2i
var cell_source_id: int
var local_cell_position: Vector2
var distance: float
func _ready() -> void:
await get_tree().process_frame
player = get_tree().get_first_node_in_group("player")
func _unhandled_input(event: InputEvent) -> void:
if event.is_action_pressed("remove_water"):
get_cell_under_mouse()
remove_cells()
elif event.is_action_pressed("use"):
get_cell_under_mouse()
add_cells()
func get_cell_under_mouse() -> void:
mouse_position = topsoil_tilemap_layer.get_local_mouse_position()
cell_position = topsoil_tilemap_layer.local_to_map(mouse_position)
cell_source_id = topsoil_tilemap_layer.get_cell_source_id(cell_position)
local_cell_position = topsoil_tilemap_layer.map_to_local(cell_position)
distance = player.global_position.distance_to(local_cell_position)
print("mouse_position: ", mouse_position, " cell position: ", cell_position, " cell_source_id: ", cell_source_id)
print("distance: ", distance)
func add_cells() -> void:
if distance < 50.0 && cell_source_id != -1:
new_tilemap_layer.set_cells_terrain_connect([cell_position], terrain_set, terrain, true)
func remove_cells() -> void:
if distance < 50.0:
new_tilemap_layer.set_cells_terrain_connect([cell_position], 0, -1, true)