comparing arrays until one as reach a condition

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By quizzcode

Hello there,
I’m hitting a block with some arrays; I’d like you help :slight_smile:

I’m making “bouncing” attack system.
function is find_target_bouncing()
my Projective has an area2d that uses get_overlapping_bodies when hitting an enemy.
So that I can see if/how many enemies are in range for bounce doing a FOR that breaks once it found a proper target.

Then, I select the closest one, and move toward him. Adding him to the bouncing_history array.
On hitting the target, the function is called again.
When I’m hitting my “max_bounce” amount, i queue_free() the projectile.

The problem is when there are 2 enemies. and im supposed to bounce 4 times.
After hitting the 2nd enemy, the projectile will wonder chaotically.

So. My (i think) solution, would be to check the array units = get_overlapping_bodies
and compare it to the bouncing_history and that when ALL unit in units are in bouncing_history , I know that my projectile has hit all it could and i can queue_free early.

I just cant figure out a clean way to compare all entries’ from an array to an other one.
could anyone illuminate me with this ?
thank you

:bust_in_silhouette: Reply From: Wakatta

Looks to be you are over complicating this issue but answer directly since unsure of your projects set-up

Solution 1

func compare_all_entries(array, other_array)
    for entry in array:
        if not other_array.has(entry):
            return false
    return true

Solution 2

When adding to the bouncing_history if current_bounce is one less than max_bounce queue_free()

Honestly you really should use the body_entered & body_exited signals in lieu of get_overlapping_bodies