How to pause a function, while it wait for a different function to finish

Godot 4.0.3

I am working on a game with a 1D auto battler that is very similar to super auto pets, if yoy have ever played it. If you havent, you will have 6 units that have an ability, and a trigger for said ability. Your front unit then “fights” the front unit of the enemy. Triggers are things like when a unit dies, when the battle starts, when damaged, ect. These triggers are where im struggling.

I have a function that starts the battle that effectively looks like this

Start the battle:
Check units for (start of battle) trigger
Check if any units are dead
Make the front units fight

Like this, all 3 would effectively happen at the same time, which is an issue with tweens and animations. i need each line of this function to only happen after the last line has FULLY completed. Im new to programming and at an absolute loss on how to do this. I just need ideas

I know i can use Await and timers, but that has its own issues in specific corner cases, such as a long ability chain that outlasts a specific timer. currently it is the best option i can think of.

How to pause a function, while it wait for a different function to finish

func main():
	await function1()
	await function2()
	await function3()
	print("done")
1 Like

I think i am missing something with await. I tried this and the functions were still returning before being completed. For instance, say function 1 has a tween that it itself is awaiting on. When that tween is finished it deals damage to a random unit. When i was using await for those functions, function 3 would begin while the tween from function 1 was happening.

My understanding of await was that it doesnt work with all functions. Im just very confused with await and have been unable to get it to work. With that said, i fully admit that i am probobly wrong and its an error on my end somewhere. I will work on it more tonight. I appreciate the response!

how do you wait this?

It creates the tween and then awaits on await tween.finished, to do the damage calculations

technically, it shouldnt go to function3 if you have await on function1 with await on the tween finished in function1. can you confirm that (by print) the function3 triggered the moment function1 triggered?

I have put a print within function 1, 2 and 3 and they all appear to print at the same time

ok, let me try replicate

image

with this code

so something else should try to trigger the function3 in your case

else it’s godot 4.0.3 bug
fyi, im using godot 4.2.1 stable to test the code above

you can try the code in your place, see if it’s really engine bug

func _ready():
	await function1()
	await function2()
	await function3()
	print("done all function")

func function1():
	print("enter function 1")
	var tween=create_tween().bind_node(self)
	tween.tween_interval(3.0)
	await tween.finished
	print("done function 1")
	
func function2():
	return
	var tween=create_tween().bind_node(self)
	tween.tween_interval(3.0)
	await tween.finished
	
func function3():
	print("enter function 3")
	var tween=create_tween().bind_node(self)
	tween.tween_interval(3.0)
	await tween.finished
	print("done function 3")

Thank you so much. I will test this tonight

I would advice you to update your godot version, your version is really low, alot of bugs are fixed after that :slightly_smiling_face:

This is what worked for me! Thank you

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.