Help coroutine

Hi, I’m planning an educational purpose a text adventure.
The actions are chosen either by clicking on the buttons, or by keyboard keys.
As for the Onclick Button, it works fine.

What I would like to change is the recall by keyboard. Currently it “waiting” in the Update that some key (Example : WASD) is pressed.
Is it possible to have a method call, where the code stops in this method, until it calls a new method to continue?

I was thinking of a coroutines, but I’m not sure how they work. Are there any examples or tutorials that can come up to me for my case?

Ths for all

[Edited for a better explanation]

For a button based game, it would make much more sense to tie the methods to the button presses. Here’s an old Brackey’s video which shows how to do that.

You may be looking for something like I’ve shown over here where I dynamically subscribe to one or multiple buttons in order to wait for a click inside a coroutine. However, currently that doesn’t mix that well with also waiting for a key press at the same time. Technically that could be done with some additional custom yield instructions to “combine” several yield instructions logically (like AND or OR). Though I’m not sure that would make the concept much simpler. While I’m also a fan of having things only active when needed and coroutines are great to model sequential dialogs and logic, everything has its limits. So it’s possible, but requires some additional boilerplate and helper classes / coroutines to get it working.

Coroutines are NOT always an appropriate solution: know when to use them!

https://discussions.unity.com/t/857304/4

https://discussions.unity.com/t/849607/7

Solve your problem first. Nobody need coroutines to do anything. They only help in certain specific cases. This may or may not be one of those cases.

Yes, I did this for the buttons.
But I don’t know how to do it with regards to pressing keyboard keys.

My game is like a multiple choice dialogue.
Choose to go right (press D)
Choose to go left (press A)

What can help me?

Doing a few tutorials for a quiz game or text adventure game, whatever you think is the closest to your use case.

Coroutines are for doing pre-canned stuff over a period of time, and are written in code.

If you’re not familiar with coding, it may be FAR easier to just use animations.

Again, ten bazillion tutorials out there for animation, so go jump in with both feet.

1 Like