Wait inside a non void method
Hello. I've been searching about this for hours and have found multiple answers to similar questions but not similar enough for me to find a solution. Anyway my problem is: I have a method GetResultMyTeam
that takes an object of class Match
and returns an int[2] result for it, based on user input. The problem for me is how can I wait for the user input inside this method? The method I'm running is as follows:
int[] GetMyTeamResult(Match PlayedMatch)
{
int[] test = new int[2];
UIManager.GetComponent<UIManager>().HideOffers();
UIManager.GetComponent<UIManager>().HideMatchweek();
UIManager.GetComponent<UIManager>().ShowMatchday();
// it should wait here
test[0] = md_manager.GetComponent<MatchdayManager>().HomeTeamGoals;
test[1] = md_manager.GetComponent<MatchdayManager>().AwayTeamGoals;
return test;
Firstly I show the proper UI so that the user can input the result and then get the HomeTeamGoals
and AwayTeamGoals
variables and set them to the test array, which is returned. The problem obviously is that the test array gets filled before the user has a chance to input any data. So I want the method to wait until the user does input some data. I tried
1) To create a couroutine only to wait. But time waits only inside the couroutine, the method returns the wrong value anyway.
2) To replace the whole method by a couroutine but it didn't seem too good of an idea have some other results being given by methods and others by coroutines. Besides I couldn't figure out how to set input paramaters to the couroutine and have it return values the same way the method would. If this is the way to go do tell me how I can implement it correctly.
Answer by streeetwalker · Sep 27, 2020 at 01:13 PM
I think you need to think about the problem differently.
I don't think don't really want to wait per se - that is you don't want to stop Unity from processing everything. What you want to do is not process your test array until all the input has been filled - does that sound right?
If so, I think you can easily do this using a number of schemes. Off the top of my head, you don't want the function you show as is - you need to do some restructuring.
The algorithm goes something like this:
1. show your UI input "screen" (the buttons where you collect the input).
2. for each input item, when the input is entered and validated, call (in #3 below ) a function that takes as a parameter which array item the input belongs to and its value. Essentially, your send the function a message on input - so essentially set up #3 as event handler. Each input item has an event listener that sends the data to #3
3. this is the function that handles the input messages. Inside it you will decide if all the necessary inputs have been received, and if so then you process the test array (or call a function that does). and close your input screen
I think that approach will take care of your problem. Of course, if you need to suspend parts of the game while you are waiting for this input, you can easily set a boolean to handle that when you show the UI input screen, and check that in the relevant parts of your game, and then set it back when you get all the input.
While I appreciate the time you took to help me and think this is a good idea, it wouldn't work without reworking my whole game, which is something I hope I can avoid. The problem is that the method I showed in the original post is itself inside another method, so even if I show the UI separately and only set the test array when the user confirms the inputs, the rest of the methods would still run before the inputs had been set. I really think I would have to rework the match simulation system for it to work like that.
When you click an AdvanceWeek
button it will trigger a method AdvanceWeek
which will trigger`Playmatch`which in turn will trigger a GetResult
(which calculates a result without user input) or Get$$anonymous$$yTeamResult
method depending on if its the player team. So, for your proposed solution to work I would have to show the UI in a completely different way, which would mean a lot of work I'm trying to avoid here!
What did you mean with setting booleans? I tried to put the UI manager parts of the code I showed inside a while loop but of course I created an infinite loop. How could I achieve this? In other words I really must halt the Get$$anonymous$$yTeamResult
method until the user confirms the input
it won't be the first time someone has coded themselves into a corner with no way out. We've all done it.
Without actually seeing all your code, I would think it would be just a matter of re-wrapping your existing UI - just rearrange how and where the function(s) that do the UI are called. I don't think it would actually that much work - of course I'm shooting in the dark.
I may be wrong, and my suggestion is not the only one ( however, I believe all of them are going to require that you decouple the UI from the system you have now) but I don't see how you can get any "wait" to work in the function as it is now.
You only need a boolean or some value if you need to pause other functionality that happens in Update or FixedUpdate loops. So when you show your UI you set a "waitingForInput" to true, and the other systems that are periodically called skip their code if that is true.
All right thanks a lot for the replies! I was hoping it could be done with couroutines but if there is no way out I guess I'll just have to get to it. Thanks again!
Your answer
Follow this Question
Related Questions
Method Skipping Inputs 2 Answers
Dynamic Menu C# 0 Answers
Link UI Input field to Google Map Script 1 Answer
NullReference in Eventsystem Raycast eventCamera 0 Answers
C sharp script, to open a Unity standalone .exe file on a network drive 0 Answers