Godot Version
4.2.2.stable
Question
Hi,
I have picked upt Godot recently and am quite new to game development and coding, so I am still learning a lot.
I have recently run into a problem with input and a Node “SelectionArea” I have created to select buildings or items in my scene, which can also be placed. Now I want to differentiate between placing items and selecting items. For that I added a variable that triggers a “preview_mode” true or false depending on whether my I am in “placing” mode or not. The code for the selection area looks as follows:
class_name SelectionArea
extends Area2D
signal selection_toggled(selection)
@export var exclusive = true
@export var selection_action = "map_left_click"
var selected = false: set = set_selected
var is_mouse_over: bool
var preview_mode := false
func _ready():
SignalManager.connect("preview_active", _on_preview_active)
func set_selected(selection):
if selection:
_make_exclusive()
add_to_group("selected")
else:
remove_from_group("selected")
selected = selection
selection_toggled.emit(selected)
print("Selection toggled: ", selected)
func _make_exclusive():
if not exclusive:
return
get_tree().call_group("selected", "set_selected", false)
func _mouse_enter():
is_mouse_over = true
func _mouse_exit():
is_mouse_over = false
func _input(event):
if event.is_action_pressed(selection_action) and is_mouse_over:
if preview_mode:
return
else:
set_selected(not selected)
get_viewport().set_input_as_handled()
print("Click: Selection")
return
func _on_preview_active(status):
preview_mode = status
print("Preview mode status changed: ", status)
The actual problem right now is, if I want to place another building on a tile (I am using a tilemap) behind the selecetion area, it still triggers the “selection” even though “preview_mode” is set to true. (I was able to confirm that, as once I enter the “placing mode” the signal come through and prints:
Preview mode status changed: true
If I then place an item in the world I have a print in another script that correctly prints:
Click: Place Building
and also correctly places the item
However if I want to place an item now, for example, on the tile above the now occcupied tile that is covered by the selection are I recieve:
Selection toggled: true
Click: Selection
even though the preview_mode = true
Is my syntax wrong or am I not understanding something about the _input() function?
Any help is appreciated,
Thanks