- Home /
Yield And Return
I started working with the WWW class to retrieve levels data from my host, and obviously I need to wait for the download to complete before moving on with further line codes.
Now, being my first project my code is a little confused sometimes, I have several functions calling each others to retrieve data and stuff like this, and that posed me in front of a little problem.
Let's say I have function A that acts like a main engine for the system, calling function B to retrieve a data, so function B is supposed to RETURN a value...
Now let's say that this function B to evaluate the data to return must check an url result, so it will instantiate a WWW object and wait for it to be ready.
Here come the problem, a function with a yield inside it cannot return a value...
I worked the whole thing around by using global variables instead of return values, so at this point now I'm able to make it work, which is pretty good!
But is there a way to make it work without passing to global variables as I did?
Like another way to wait in a blocking manner (because the yield start a coroutine, if I have understood it right) for a download to be completed?
Answer by yoyo · Apr 14, 2011 at 04:57 PM
Seems like you need to rework the design of function A, so it doesn't require data to be available immediately. While you could get B to block until the data is ready and then return it, that won't create a good experience for your user. Dealing with asynchronous processes is a fact of life in game development (and application programming generally), so you want to design your game to deal with systems that may not return data immediately.
The simplest way to handle this for level loading is to have some sort of loading screen. The loading screen could itself be a very small Unity "level" that's always in memory. When you leave level 1, you switch to the loading screen, request the data for level 2, unload level 1, and display the loading screen until level 2 data is ready.
In your case, function A would continue to update the loading screen until it hears back from B that the WWW level download has completed.
But in that case function B is always saving the WWW level download to a global variable, so I'm back to start, right? :) (I'm already using this kind of "persistent loader" system, but thanks anyway! ^^)
Not really. A creates an instance of B, B initiates the loading process, A polls B.result until it's not null. No global variables in sight.
Answer by Proclyon · Apr 14, 2011 at 02:30 PM
I am not able to give you a clear answer on how to solve this but with a work-around make a method C that does the returning for you when YOU want it to return and let method B do all the work/logic.
Also using globals is pretty much a common dilemma in programming. When using an OOP(Object-Oriented-Programming) design globals go straight around that by being a unique object that can not be instantiated and have a sort of "we only play OUR way" mentality. It's great and easy at first, just like singleton, but if you take the easy read to often you can end up with the worst headache code of your lifetime.
IF INTERESTED: To explain this point I suggest you look into the keywords. OOP Design, Design Patterns (Particularly singleton), Static and global variables. Those should lead to endless resources on just this topic.
I'm aware of how OOP works, and of global, static and stuff like that... :)... I just wanted to know how to deal with the yield/return problem without using global, exactly because I know that it's not the best solution, usually... :)
well as I tried to explain , all I can give you as an answer to that specifically right now is the workaround using a method C and split the tasks
Your answer
Follow this Question
Related Questions
Yield function goes slow sometimes/differently? 2 Answers
How to properly use Yield for a coroutine? 3 Answers
Calling function in another script dying without error 0 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Can't find Yield, LoadImageIntoTexture doesn't work with Texture 1 Answer