0 votes

Hey All,

I am turning this over to you because my brain is about to melt.

For some reason, the following for loop only runs twice despite there being 3 items in the array it is iterating over. No change if I increase the size of the array (enemies_stack). I know the break statement is not being run because a print() put before it does not output anything.

var ENEMY = preload(....tscn)
var num_active_enemies = 0
enemies_stack = [3,3,3]

func spawn_enemies():
    for enemy in enemies_stack:
        if num_active_enemies >= 10:

        var enemy_instance = ENEMY.instance()
        enemy_instance.global_position = random_spawn_loc(enemy)

        num_active_enemies += 1

HERE is the GitHub link with the full script and project files. Please help me lol.

Godot version 3.2.4 beta 4
in Engine by (577 points)

1 Answer

0 votes

You're starting your loop on a stack with 3 elements but you remove elements as you go, so your loop can't complete. The solution is to not modify your list as you're iterating on it. You can make a copy before starting your loop for example and remove elements from that copy.

If you're sure you want to remove elements as you go, you could use pop_front

for i in range(stack.size()):
    var enemy = stack.pop_front() # this element is removed from the stack
by (619 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.