Godot Version
4.3.stable
Question
I learned this code online and I am trying to change it so a button would start the building feature, instead of pressing “1”. Code below (written in the script “tile_map_layers”):
extends Node2D
@onready var ground = $Buildings
@onready var preview = $Preview
var source_id: int
var selected_tile : Vector2i
var select_mode : bool = false
var preview_tile: Vector2i:
set(value):
if preview_tile == value:
return
preview.erase_cell(preview_tile)
preview_tile = value
preview.set_cell(value, source_id, selected_tile)
var atlas_tile: TileSetAtlasSource
atlas_tile = preview.tile_set.get_source(source_id)
var tile_size
if atlas_tile:
tile_size = atlas_tile.get_tile_size_in_atlas(selected_tile)
placeable = true
for j in range (tile_size.y):
for i in range(tile_size.x):
var tile = preview_tile + Vector2i(i,j)
if tile in BuildingManager.used_tiles:
placeable = false
var placeable : bool = true:
set(value):
placeable = value
if value == false:
preview.modulate = Color.RED
else:
preview.modulate = Color("ffffff6f")
func get_snapped_position(global_pos: Vector2) -> Vector2i:
var local_pos = ground.to_local(global_pos)
var tile_pos = ground.local_to_map(local_pos)
return tile_pos
func _physics_process(_delta):
if select_mode:
preview_tile = get_snapped_position(get_global_mouse_position())
func _input(event):
if event is InputEventMouseButton and event.pressed:
if event.button_index == MOUSE_BUTTON_LEFT and select_mode and placeable:
place_tile(preview_tile)
select_mode = false
elif event.button_index == MOUSE_BUTTON_RIGHT:
ground.erase_cell(preview_tile)
if event is InputEventKey:
if event.keycode == KEY_1 and event.pressed:
select_mode = true
source_id = 2
selected_tile = Vector2i(0,0)
func place_tile(tile_pos: Vector2i):
ground.set_cell(tile_pos, source_id, selected_tile)
preview.erase_cell(tile_pos)
BuildingManager.get_tiles(ground, selected_tile, preview_tile)
I did connect a “Build Turbine” button to the script, but I have no idea how to change the code so a button initiates the build feature.