How to stop a function, from outside said function.

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

With code like this to start and wait for a function to be finished:

self.call_deferred('function_a')
yield(self, 'function_a_completed')

function_a()->void:
    checking a thing...
    checking a thing...
    checking a thing...
    ...
    checking last thing
    emit_signal("function_a_completed")

Is there an easy way to some how stop or “turn off” function_a from running if it’s taking too long?

I want the AI to make some decisions, but I want the function to stop if it takes too long (say, more than 10 seconds).

:bust_in_silhouette: Reply From: fershopls

I think it depends on what your function is actually doing.

Lets say your function is doing a while loop. Maybe you can create a one-shot timer.
Set it to 10 seconds and start it. Then check at every while cycle if the timer has stopped.

If the timer is stopped means you should break the loop

Gotcha, yeah I was kind thinking I could keep checking the time inside the function. However, I was wondering if the function could be stopped/break/ended from a place outside that function.

For instance, maybe somehow a signal emitted outside function_a could be connected to something that then stops function_a. Or maybe that outside signal emitted somehow connects with something inside function_a to make it stop right away.

eod | 2020-02-29 04:58

:bust_in_silhouette: Reply From: Sir Skylight

I’m very positive that typing “pass” or “return” should do the trick.

Example:

func example():
      if stop == true:
            pass
      elif stop == false:
            do something...