Godot Version
v4.6.2.stable.official [71f334935]
Question
I’m making a Pokemon-like something but with robots and I often need to filter them by the area where they’re encountered. So I made 2 global enums with all possible bots and all possible types/areas, and then I made arrays containing every bot of the respective type. So right now it looks something like this:
# Lists.gd (autoload script)
enum BattleObjects {
# literally everything that can ever be on the board: fighters and obstacles
}
enum FighterTypes {
# basically grouped by area + boss-exclusive bots + obstacles
}
var fighters_generic: Array[BattleObjects] = [
BattleObjects.CHARGERBOT,
BattleObjects.BOMBERBOT,
BattleObjects.ASSASSIN,
BattleObjects.SHARPSHOOTER,
BattleObjects.SHOTGUNNER,
BattleObjects.TWIN_BARRELER,
BattleObjects.ROCKETEER,
]
var fighters_forest: Array[BattleObjects] = [
BattleObjects.DATAMINER,
BattleObjects.WIREWOLF,
]
var fighters_scrapyard: Array[BattleObjects] = [
BattleObjects.ZOMBOT,
BattleObjects.PROTO_TYPE,
]
# etc
The problem: I wanna be able to filter them in the editor, like, changing the type would change the list of available bots. I tried something like this:
# BattleObject.gd
@export var object_type: Lists.FighterTypes:
set(value):
if object_type != value and Engine.is_editor_hint():
object_id = get_type_array()[0] # supposed to reset the id in case the current value is out of bounds
object_type = value
notify_property_list_changed()
func _validate_property(property: Dictionary) -> void:
if property.name == "object_id":
var type_hint_string: String
var strings_array: PackedStringArray
for i: Lists.BattleObjects in get_type_array():
strings_array.append(Lists.BattleObjects.find_key(i).capitalize() + ":" + str(i))
type_hint_string = ",".join(strings_array)
property.hint_string = type_hint_string
func get_type_array(value: Lists.FighterTypes = object_type) -> Array[Lists.BattleObjects]:
match value:
Lists.FighterTypes.GENERIC:
return Lists.fighters_generic
Lists.FighterTypes.FOREST:
return Lists.fighters_forest
Lists.FighterTypes.SCRAPYARD:
return Lists.fighters_scrapyard
Lists.FighterTypes.SPECIAL:
return Lists.fighters_special
Lists.FighterTypes.OBSTACLE:
return Lists.obstacles
_:
return []
So every time I try to call get_type_array, it returns this error:
ERROR: res://scenes/BattleObjectStats.gd:419 - Invalid access to property or key ‘fighters_generic’ on a base object of type ‘Node (Lists.gd)’.
What is the problem here?
And also I really hate how the get_type_array func is a match statement given that more types could appear in the future. Is there a way to extract variable names and convert them like strings?