modules: dedup select building in the option component

This commit is contained in:
Dmitriy Pleshevskiy 2024-01-10 12:01:53 +03:00
parent f03e258cb0
commit c255e5aa96
Signed by: pleshevskiy
GPG key ID: 79C4487B44403985

View file

@ -13,12 +13,17 @@ var option_button: OptionButton;
var selected_option: BuildingOptionItem = null var selected_option: BuildingOptionItem = null
var options: Array[BuildingOptionItem] = [] var options: Array[BuildingOptionItem] = []
func select(selected_id: int): func _select_option_by_id(building_id):
option_button.selected = option_button.get_item_index(selected_id)
for option in options: for option in options:
if option.id == selected_id: if option.id == building_id:
selected_id = building_id
selected_option = option selected_option = option
selected.emit(option) selected.emit(option)
break
func select(building_id: int):
option_button.selected = option_button.get_item_index(building_id)
_select_option_by_id(building_id)
func _enter_tree(): func _enter_tree():
option_button = $OptionButton; option_button = $OptionButton;
@ -37,10 +42,5 @@ func _ready():
func _on_option_button_item_selected(index): func _on_option_button_item_selected(index):
var selected_id = option_button.get_item_id(index) var building_id = option_button.get_item_id(index)
for option in options: _select_option_by_id(building_id)
if option.id == selected_id:
print("selected item: %s (id: %s, energy: %s)" % [option.label, option.id, option.energy])
selected_option = option
selected.emit(option)
break