How do I wait a certain amount of time without using yield(get_tree().create_timer(1), "timeout")?

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

btw im a beginner. ive coded here and there over the years but the last time i was really into it was when i was 12 with game maker and all i did was copy a youtube tutorials code till i was bored lol.

so the problem is yield(get_tree().create_timer(1), “timeout”) sends an error if the scene is reloaded or the node with that line of code gets freed before the yield is over. Plus a bunch of people said not to use a whole lot of yield functions everywhere.

For example of the code ive been focusing on, I am making a cup head style boss rush (ish) type game. really its just a bunch of bosses. and its top down instead of sidescroller. I set up a match statement to handle going between states, and i have 2 states for my first boss currently. Those states are IDLE and CHARGE. to switch from IDLE to CHARGE, you have to go above a certain y coordinate because in the IDLE the boss goes back and forth at the top of the screen.

this is the code for the CHARGE state

    			#Reset Variables
    			var shot_direction = 0
    			var shot_pos = 1
		#Charge left or right
		if player.position.x < position.x:
			position.x -= speed * charge_mod
			move_right = false
			shot_direction = 180
			shot_pos = 1
			position.x += speed * charge_mod
			move_right = true
			shot_direction = 0
			shot_pos = 2
		yield(get_tree().create_timer(0.3), "timeout")
		#Pause at End of Charge
		charge_mod = 0
		#5 Bullet Spread Shot
		if only_once_charge == true:
			shoot(shot_direction + 20, shot_pos)
			shoot(shot_direction + 10, shot_pos)
			shoot(shot_direction, shot_pos)
			shoot(shot_direction - 10, shot_pos)
			shoot(shot_direction - 20, shot_pos)
			only_once_charge = false
		yield(get_tree().create_timer(1.0), "timeout")
		charge_mod = 4
		has_charged = true

as soon as IDLE switches to CHARGE, it detects what side your on. Then it increases speed in that direction and sets which direction the bullets need to be shot, and yields for 0.3 seconds. after charging towards the player, speed becomes 0 so it doesnt move and it shoots 5 bullets (setting the angle and what side the bullet will come out of respectively). it then yields for 1 second, then sets variables back to make sure the next charge works fine and that it cant bug out by charging back to back.

my problem is the yields. the best way ive seen to handle timings like that is through the animations. but i dont have animations. they are just little squares bouncing around in a grey box with black walls. my friend is supposed to do the art, but he has shit going on so yk nothing i can really do.

ive messed around with timer nodes for a bit, but i dont know how i could have them wait for a specific amount of time and then continue with the code like the yield does. would i need a different timer node for every pause or time i wanted something to happen for a specific amount of time? or could i set the wait time on the fly and only need 1 or 2?

there was a reddit post and it had a comment here that seemed really cool but it has its own problems (mainly being someone elses code that i copypasted) and it feels a little too high level for me

what would the best way of going about doing this? id really like to figure this out before moving on because i feel like id use it alot more at least until i have animations of some kind. plus its fun to learn about stuff like this

if you have any questions then please ask and sorry for the super long post lol. ive been stuck on this for a few days and looking stuff up hasnt helped much


:bust_in_silhouette: Reply From: USBashka

Yeah, Timer node is the best solution for this. You can control it from script, just read the silly docs. It have functions for start/stop, parameter for time and a signal for timeout.

yeah now that i think about it, that would be way easier to implement than anything ive tried so far lol. thanks!! for the timers, im not too worried about too many timers causing poor performance since theres no way my games gonna be even close to that big, but since ill probably have a fair amount of timer nodes, is there anything wrong with sorting them in the tree with just plain Nodes? like the empty ones and calling the empty nodes something to group all the related timer nodes together?

tilliie | 2022-07-18 05:30