Wait on click before continuing execution

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Adham
:warning: Old Version Published before Godot 3 was released.

Say I have a script with multiple functions:

myFn1()
myFn2()

How do I make it so that when myFn1() executes, it has to wait for the user to click on something (a signal) before executing myFn2() ?

Both functions are defined on another file that has all my helper functions.

I tried using set_pause in stop mode, but it still wont work. Godot just executed both functions, then wait for the click. I also tried coroutines but didn’t work.

Loading...

atze | 2016-07-14 14:54

:bust_in_silhouette: Reply From: vinod

A simple way to do this will be to use the yield.

First define a signal by

signal mouse_click

Then you need to listen for the mouse click in _input process.

func _input(event):
    if(event.type ==InputEvent.MOUSE_BUTTON and event.is_pressed() and not event.is_echo()):
        emit_signal("mouse_click")

Then inside myFn1,

function myFn1():
    print("this will run right away")
    yield(self,"mouse_click")
    print("this will wait for mouse click")

Hope this works, please check this because I’m typing on mobile.

As I mentioned in my post, I have already tried coroutines. All this does is pause myFn1() mid-execution, but for some reason doesn’t stop myFn2() from being executed normally.
I don’t know why, but that’s what happens.

Adham | 2016-07-14 05:33

I am using coroutines without problems.

How are you calling the two functions?
I’m just making sure of something.

If you are calling like this,

myFn1()
myFn2()

and the yield is inside myFn1, then both functions will run without waiting.

You need to call like this,

myFn1()
yield....
myFn2()

vinod | 2016-07-14 05:56

But I can only use yield inside a function. EDIT: trying this out now on Godot.

Adham | 2016-07-14 06:48