The loop (and the function) will exit as soon as it encounters a ship whose select
flag is false. You probably want it the other way around - loop until you find the selected ship and then exit.
However 🙂
Your code looks quite problematic in general. There's a lot of logical and stylistic problems that add up to a very tangled piece of code. To name just a few:
get_node(get_path_to(X))
is exactly the same as just using X
, and you do this in many places, completely needlessly.
- Using
selector
as a local variable in a function named selector
- Using a loop iterator with plural name
ships
, while at the same time having a singular ship
as a different variable inside the same loop. This would confuse even experienced coders, let alone beginners.
- Having function names that are not clear and not verb-oriented like
selector
or attack_action
. A functions represents work. It does something. Its name should clearly tell us what it does. For example: select_unit
, deselect_all_units
, attack
, move
, sort_by_initiative
I could go on. I recommend scraping this and starting over. Start by writing low level helper functions with verb-focused names that do simple tasks you know you'll need later, like select_unit
. Then build higher level functions that call those lower level functions.
When you write each function, test it to check if it does exactly what you want it to. Only move on to building the next function once you're sure that there are no problems with previously added function. You test a function by calling it from another place with different inputs and check if its outputs and/or behaviors are as expected.
Before you start, decide how the system should behave and write it down in plain language. This will make easier to figure out what functions you need and how to organize them. For example:
Attack phase:
If player left-clicks on an unit
- clear existing selection and select clicked unit
If player left-clicks outside any unit
If player right clicks on a non-selected unit, while some other unit is selected
- attack clicked unit with selected unit
And so on. From such a description you can immediately see possible function candidates. For the above example they might be: clear_selection()
, select_unit(unit)
, get_selected_unit()
, is_unit_selected(unit)
, attack(source_unit, target_unit)
, `