- Home /
Wait inside method until user action
Hello there! I need to wait until user click button and set some value. This value is needed in another class as shown below. I have no idea how to implement. I've tried to use coroutines but no matter what I do Method1 always returns value before it is set by user. I've also tried do..while but obviously it freezes program.
class Class1{
if(Class2.Method1())
//do something
else
//do something else
}
class Class2{
bool Method1() {
ShowDialog();
WaitUntilUserSetValue();
CloseDialog();
return value;
}
}
Any help & tips are welcome!
If you're waiting until the user has clicked a Button, have you tried assigning a function to that button? What does the button do right now?
Button is actually changing returning value. I want to create universal custom confirmation dialog box and use it at every destructive action.
Actually I have no idea how to wait for Button function to fire before executing the following lines of $$anonymous$$ethod1 (before returning proper value)
Well it looks like you're going to have to get into call backs, delegates & some subclassing. Google semaphores this might help where you can delay execution and continue after some condition has been met.
Answer by Timo326 · Feb 05, 2018 at 06:29 PM
Thank you for your further explanation in the comments below. I edited this answer depending on your information. Try it, i hope it will work, i haven't tested it.
public class Class1 : MonoBehaviour {
public GameObject confirmationDialog; // To set in inspector
public GameObject gameObjectToDelete; // To set in inspector
private bool clickedYes = false;
private bool clickedNo = false;
// Function is called if the object has du be deleted
public void onDeleteClick()
{
StartCoroutine(deleteGameObject(gameObjectToDelete));
}
IEnumerator deleteGameObject(GameObject obj)
{
clickedNo = false;
clickedYes = false;
confirmationDialog.SetActive(true);
while (clickedYes == false && clickedNo == false)
{
yield return 0;
}
confirmationDialog.SetActive(false);
Destroy(gameObjectToDelete); // ... or delete file ... or anything else
}
// function is called when button yes is clicked in the confirmation Dialog (Event)
public void confirmationDialogClickedYes()
{
clickedYes = true;
}
// function is called when button no is clicked in the confirmation Dialog (Event)
public void confirmationDialogClickedNo()
{
clickedNo = true;
}
}
Well, but if I call $$anonymous$$ethod1 from another class as StartCoroutine, then how to wait until this coroutine ends (and proper Value is set by user) before actually using this Value in following lines of code?
I think i did not understand your first question correctly. Well, now i think i understand what you are trying to get.
$$anonymous$$ake your dialog with an attached script (class2) and add an event (onClick) to the Buttons of your dialog. In the script set a bool ("clicked" for example) to true and set your value.
In an other gameobject (class1) check inside the Update() function if class2.clicked == true. And if this is done, read the value and execute your code.
Sorry, but i don't know how else i could explain this. Hope you get it!
First of all thank you for your effort. You are explaining this really well and I get it. However unfortunately I cannot check if the user clicked desired buttons inside Update function. Actually I am trying to create simple universal confirmation dialog (with custom layout, texture etc.) which will show up before some critical actions like deletion. For example to delete Unit user needs to press Delete button, and I would like to onDeleteClick() method call the ConfirmationDialog (with some custom string) and based on its result finally delete or not the selected Unit. By universal I mean that the same instance of ConfirmationDialog (with some other string passed as parameter) could be also use for confirmation of quitting the game.
Answer by spokrock · Feb 05, 2018 at 09:08 PM
Have a look at this youtube channel:
Dialog System
https://www.youtube.com/watch?v=_nRzoTzeyxU
Answer by BadKarus · Feb 06, 2018 at 12:23 AM
Thanks to you I finally found working solution. Here is pseudocode. Maybe someday will be helpful for someone.
class Class1 {
void onButtonClicked(){
Class2.Method1(Action SomethingToDo);
}
void SomethingToDo(){
...
}
}
class Class2 {
Action actionToDo;
bool userClicked = false;
bool confirmed;
void Method1(action){
actionToDo = action;
ShowDialog();
StartCoroutine(WaitForUserConfirm());
}
IEnumerator WaitForUserConfirm(){
yield return new WaitUntil(() => userClicked);
if(confirmed)
actionToDo();
CloseDialog();
}
void onConfirmButtonClicked(){
userClicked = true;
confirmed = true;
}
void onDeclineButtonClicked(){
userClicked = true;
confirmed = false;
}
}
Your answer
Follow this Question
Related Questions
Help with waiting / coroutine c# 1 Answer
C# Coroutine help 1 Answer
A simple Wait-Function without Coroutine (C#) 12 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers