How do I make these nested match statements easier to read?

Godot Version

4.3

Question

Still coding that awesome Turn based RPG.

I’m currently coding the logic for firstly selecting the attack then selecting the enemy (Or enemies)

    if event.is_action_pressed("ui_accept") and isSelecting:
        match currentSelectionType:
            SelectionType.SELECTING_ATTACK:
                currentSelectionType = SelectionType.SELECTING_ENEMIES
                match parent.attackActions[index].targetType:
                    Action.Target.SINGLE_ENEMY: select_single_enemy()
                    Action.Target.ALL_ENEMIES: select_all_enemies()
            SelectionType.SELECTING_ENEMIES:
                parent.deciding_finished.emit()

But I experience a metaphorical stroke every time I try to read this block of code ! How can i rewrite to make it more readable ?

I mean, the simplest thing would be to split the enemy selection stuff into its own function. If you wanna think at a larger scale, you could probably make some kind of SelectionMode class with subclasses for different selection modes, which could contain the relevant logic, and then to change selection mode, you just swap out the selection mode object for a different one.

2 Likes

hmmm that sounds interesting but I already have different objects for selecting attacks, items and magic, So introducing new selection classes under the attacking class would be too much I feel, But thank you for the idea !