0 votes

Can someone help a newbie out? My game is crashing after a few shots because of this code. I have a timer node named "shot_timer" set to autostart and 1 second.

func ontimertimeout():
var times = [1,2,3]
var shots = 0
var cooldown = rand

while shots!= times[randi() % times.size()]:
    var laser = scn_laser.instance()
    shots += 1

I have been using yields with one shot timers, but was getting errors and was told to use timers instead. Thought they would be easier to use...

Thanks in advance.

in Engine by (29 points)

Can you elaborate on your desired behavior? It's a little hard to tell what you want to do with just your code. while statements have a knack for locking up the app in undesired ways. Sometimes when I write while and that happens, I find that if statements suffice.

This code is used to have the enemy ship (in a space shooter game) shoot at slightly random intervals a slightly random number of times.
The "while loop" is there because the function is used only once.
I wonder if I could use if and restart the timer... Gonna try that instead.

Thanks for the help. You were extremely helpful.

1 Answer

0 votes

Thank you for the nudge!

All I was doing wrong was using the while loop, when also forgetting to restart the timer after a loop finished.

I have replaced the "while" with an "if" and made sure do "start" the timer again at the end. I wasn't considering the fact that by refreshing the timer the "if" would trigger over and over again thanks to the "on timeout" function.

by (29 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.