0 votes

I would like to know how I can create an infinite FOR loop to instantiate every so long a missile for an indefinite time

Godot version 3.2.3
in Engine by (54 points)

1 Answer

0 votes

Infinite loops are bad. Using a timer is saver.

See https://godotengine.org/qa/9758/timer-node-how-to-use-it-in-code for some scenario's (code or GUI)

Based on new comment (below) we will use yield().

var build_missiles :bool = false
var build_missile_every_second :float = 0.5

func do_build():
    while build_missiles:
        call_func_to_add_missile()
        yield(gettree().createtimer(build_missile_every_second), "timeout")

func start_missiles():
     build_missiles = true
     do_build()

func stop_missiles():
     build_missiles = false

Guess you have bodyentered/exited or similar event which calls start/stopmissiles.

by (638 points)
edited by

I'm not looking for a timer, I'm looking for the syntax to create a loop to indefinitely instantiate a missile every so long when an object enters an area

So it is not an infinite loop :-) I'll change my answer then using yield

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.