modules: configure option items in the enter tree hook

This commit is contained in:
Dmitriy Pleshevskiy 2024-01-10 11:40:27 +03:00
parent 0405c159ac
commit f03e258cb0
Signed by: pleshevskiy
GPG key ID: 79C4487B44403985

View file

@ -1,16 +1,16 @@
class_name BuildingOption class_name BuildingOption
extends Control extends Control
const buildings_json = preload("res://src/modules/building/building_option/buildings.tres")
signal selected(item: BuildingOptionItem) signal selected(item: BuildingOptionItem)
@export var selected_id: int @export var selected_id: int
@onready var option_button: OptionButton = $OptionButton;
const buildings_json = preload("res://src/modules/building/building_option/buildings.tres")
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(selected_id: int):
@ -20,16 +20,18 @@ func select(selected_id: int):
selected_option = option selected_option = option
selected.emit(option) selected.emit(option)
func _ready(): func _enter_tree():
option_button = $OptionButton;
for building_group in buildings_json.data: for building_group in buildings_json.data:
option_button.add_separator(building_group.group) option_button.add_separator(building_group.group)
for building in building_group.children: for building in building_group.children:
options.append(BuildingOptionItem.new(building)) options.append(BuildingOptionItem.new(building))
option_button.add_item("{label} ({energy} ⚡)".format(building), building.id) option_button.add_item("{label} ({energy} ⚡)".format(building), building.id)
option_button.selected = -1
func _ready():
if selected_id != null: if selected_id != null:
select(selected_id) select(selected_id)