Question about button-based selection of a target object or enemy(?)

Godot Version

4.6

Question

I’m trying to recreate the battle mechanics from the PS1 game Parasite Eve for reference. I’ve managed to do a lot of the coding myself so far, but I’ve gotten very stuck on how to approach making a “turn-based” enemy selection, where I can select 1 enemy to target, using the arrow keys to switch through the possible targets.

I currently use a system that instantiates 1-3 enemies when in-battle, and then you can pause the game when a bar is filled to select a target. However I’ve found it difficult to be able to get the enemy amount (as it is added to the main scene tree and not there by default) and then instantiate the target UI when the game is paused (to show which one you’ve selected) and then individually “attack” it. I hope my explanation makes sense. Most videos and forum posts seemed to have only concerned selecting things with a mouse click instead of going through some kind of array of points, where depending which arrow key you press goes through the array leftmost or rightmost, or the selection is very different from what I’m curious about.

What a coincidence - I just finished a turn-based combat game with enemy selection just like this for the most recent Godot Wild Jam!

I approached this as follows:

  1. Add each enemy to an array when you instantiate them
  2. Read from that array when you’re prompting the player to select an enemy
  3. When the player presses left or right, find the index of the currently selected enemy (assuming you’ve stored the current enemy in a variable).
  4. Either add 1 to or subtract 1 from the index depending on whether they pressed right or left to get the index of the next possible target to select. You can use wrapi here to wrap the new index around between 0 and the size of the array.

Is there any reason something like this wouldn’t work for your particular use case?

I think that would work good for my case! the mechanics I’m trying to emulate a game which had a sort of “half turn-based” combat, so your actions are turn-based but the enemy turns are real time. I’ll try to implement this.