While true loop crash my game

Godot Version

4.6.1

Question

Hello evryone i try to create an FPS game in godot but when i create the weapon it miss delay for shoot so i use the await for create a timer but i d’ont works on the process() and input() fuction so after watch a tutorial of await i create a shoot() function set a while true loop for d’ont stop and call it at _ready() function but it crash my game please help me

func _ready() -> void: 
	shoot()
	

func shoot():
	while true:
		if Input.is_action_pressed("shoot"):
			instance = ball.instantiate()
			instance.position = raycaster.global_transform.basis
			get_tree().current_scene.add_child(instance)
			await get_tree().create_timer(timedelay).timeout

Creating a while loop will completely freeze the main thread of your game. This is expected behavior. You should never have to use a while loop like this. Just set up a timer and subscribe to it’s timeout signal.

3 Likes

Never ever saw someone use this in Godot.

Also,
Awaits are bug factories - @normalized

3 Likes

but i d’ont undrestand in this tutorial it works https://www.youtube.com/watch?v=yktnda7JDGo

Just because it’s on the internet it doesn’t mean that it’s right / true / how it should be done.

That is a horribly wasteful way of doing this.

All you have to do to achieve this in a proper way, is to set up a timer, and connect it’s timeout signal to a function. Set the timer to timeout every second, and that function will be called every second.

6 Likes

“While” here doesnt mean while as in “for as long as you keep the button pressed down”, but rather that “if it is true that the button is pressed down during this frame, this loop will run for ever”. “While” is not used for s duration of time but for a single frame. If it is true that you hold the button during the start of that frame, it will remain true for an endless amount of iterations. The frame never changes

5 Likes

Why you are telling me?
lol

1 Like

Sorry. On mobile! I meant it for the guy who started the thread

2 Likes

ha ok i understand sorry for the inconvenience

1 Like

ohh no prob lol : D

2 Likes

I think maybe it has to do with my browser actually. I’ve noticed sometimes the quote function here doesnt work for me. I use brave browser that blocks a lot of things, including adds. I disabled it now for this forum. Anyway, since the problem is solved i thought i’d take the chance to advertise this great free browser.

2 Likes

i finaly found the solution

That code, as @tibaverus said, is just straight up the wrong way to do something like that in Godot. I do not recommend you follow this person’s tutorials as they are going to lead you down a lot of wrong paths.

7 Likes

ok tank you i will folow your recommadation

1 Like

WUT :distorted_face:
That’s the most broken piece of code I have ever seen!

1 Like

i know i am dumb

No you are not. You have just been taught some dodgy code, that’s all. If you have a desire to do game dev, found Godot, installed it, started following a tutorial and coding something you can see on the screen, you most certainly are NOT dumb.

You are a motivated and independent learner. Keep going. You will do well.

5 Likes

Tanks i will keep this in mind

4 Likes

It’s easily fixable though, just add more awaits :rofl:

func shoot():
	while true:
		if Input.is_action_pressed("shoot"):
			instance = ball.instantiate()
			instance.position = raycaster.global_transform.basis
			get_tree().current_scene.add_child(instance)
			await get_tree().create_timer(timedelay).timeout
		else:
			await get_tree().process_frame

This would be better in respect to player experience than the marked solution. Because the marked solution checks for the input only periodically and can miss it when the player quickly presses and releases the action while the coroutine is awaiting. So timeout should only be awaited if the ball was fired. Otherwise just await one frame so the engine has time to do its thing.

Now as others have pointed out doing this with awaits is rather bad for multitude of reasons. @akoyo118 try to implement it with regular signal handlers.

3 Likes

thanks next time i will remember that and mind before write code and open topic

1 Like