0 votes

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.

in Engine by (45 points)

1 Answer

+2 votes

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.

by (751 points)

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.

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()

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

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 Frequently asked questions and 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 [email protected] with your username.