0 votes

My _ready function looks like:

func _ready():
    while true:
         yield(_execute_state(), "completed")

_execute_state function waits for timers so one average call takes like 3 seconds. It matches the state of the game object to an enum and does some stuff like animating a laser pointed to player, lock target shoot.

But in between all of this the player can fire bullets to the game object, eventually killing it. So, when that happens I want _execute_state to leave everything (firing bullets with delay, moving laser, etc.) and play the die animation instead. But I don't know how! How to stop a function from executing whatever it is doing?

Godot version 3.2.3
in Engine by (38 points)

is executestate() on a secondary thread?

No, it's just another function. Will making it a thread help?

let me understand better: i suppose that after yield() you let _execute_state() run again (otherwise what would be the sense of placing in a loop?)
if that's the case it's not easy to help you without the code/structure, but since it lasts roughly 3 seconds (and you told us it's because of timers) i guess you have plenty of moments where the engine simply wait for the timers to finish. These are good "idling" moments to check if the game object is dead. Something like:

func _on_Timer_timeout():
 if dead:
   terminate_execute_function()
 else:
   keep_going_with_execute_function()

you can also add a function to take care of what happens mid-time, like

func kill_game_object():
 dead=true
 if not $Timer.is_stopped():
    $Timer.stop
 terminate_execute_function()

terminate_execute_function() will bring _execute_state() to conclusion and return the completed signal to proceed in _ready().

That's a good way too. I will try to implement something similar.

1 Answer

+1 vote

I don't know about existence of such a method, we generally project our code beforehand to control its flow. Since death is a priority above any other actions in your code, than You should add appropriate condition in this function like :

If notdead == true()
do
your_stuff()
else:
justdie()

by (2,560 points)

I tried that too, but it's difficult because I use so much yield. Results in too much redundancy.

I don't see how yield interrupts changes in code ?
You were trying to do state machine, right? So You specified conditions for each state already, for example : when press space change state from walking to jumping under condition players in on the ground. So just add everywhere conditions for not being dead :). You can use return for this. Return breaks function immadietely, but it has to be called from within this function.

I will think about that. For now, I added if dead: return statements to avoid situations that the player won't like (e.g. before firing bullets). And set_visible(false) the lasers. The code is not the most elegant but it works.

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.