how to make a delay for WWW?

Well this is my code

private IEnumerator RSendRequest(WWWForm dataToSend, string function)
    {
        //string output = string.Empty;
        // Send request to server
		print(website + function);
        WWW request = new WWW(website + function, dataToSend);
        yield return request;
		
		if(request.error != null){
			
			print(request.error);
			
		}else{
			// website processed correctly.
			// print("Website processed correctly.");
			result = request.text;
			request.Dispose();
			print(result);
		}
    }

so this fully works, BUT I have to do the process 2 times, it isnt waiting till result = request.text.
and its going by what result.request,text was before the process was done.

im calling it like this (its in the GUI function):

StartCoroutine(RSendRequest(codeForm, "test.php"));
			
Debug.Log(result);

the courutine will do its thing will the rest of the code continues…its not blocking.
So, either move that print statement inside the co-routine, or put a while loop to wait for result to have valid data.

so in between the startcoroutine and the debug should be a while loop?

kinda. what i do is have a loop in the co-routine. The co-routine calls the real code, and has a loop that waits until that code finishes, like so:

public IEnumerator waitForShittoFinish(int someParam)
{
   doSomeLogic();//this will do something that you need to wait until its finished

   while(someLogicVar == null)//or however you know the data is not yet complete
   {
		Debug.Log("stil waiting....");
		yield return 0;
   }
   continueLogic();//this wont execute until that logicVar says its okay to continue, or you make a timeout value

}

i use something like this for pinging a server, connecting to it, getting data, and other things that you have to wait for data to come back before continuing processing the info

well i thought i solved it… guess not x.x… the thing is I can’t do that technique since all the calling is already done… and i rather not touch it, i did put a while statement in between the debug and the coroutine but my unity crashed, and same thing happened with the for loop. can anyone help?

shameless bump :frowning:

OK, so you start a coroutine, then expect the result of that coroutine the frame you start it?

This is not good. Your code has your answer. Start a coroutine and let the coroutine give you the answer.

You dont want to loop, you just want to let the coroutine do it’s job.

the way it was before was they added this coroutine, but in webplayer it froze

while((!request.isDone)  (stop > Time.time))
			StartCoroutine(StartSending(request, stop));

....


while((!location.isDone)  (stop > Time.time))
		{
			
			yield return new WaitForSeconds(1);
		}

it worked in standalone, but we want this game to be for webplayer. I mean i will rewrite the whole code if necessary im just hoping i can avoid it?

I am trying to get the result variable in the same frame yes… and i know its bad… but can i make it work?

ah, no, you must understand the difference between a regular routine (function) an a coroutone (subroutine)

If you tell your code to count to 1000 then continue, it does exactly that. it doesnt care that the frame is over.

So by implementing Wait(until the WWW object is done) your code literally waits until the object is done. (or the coroutine)

This is not good.

Consider that we want a button. You press the button to load a text file from a server… You want the button to only appear when it is not loading…

Observe:

string website = "http://www.mysite.com/data/";
string buttonText = "Press Me To Load";
bool isLoading = false;


void OnGUI() {
	if(! isLoading){
		if (GUI.Button(new Rect(10, 10, 150, 100), buttonText)){
			StartCoroutine(LoadFile("myButton.txt"));
		}
	}
}

IEnumerator LoadFile(string filename){
	WWW request = new WWW(website + filename);
	isLoading = true;
	yield return request;
	
	if(request.error != null){
		buttonText = "Error Loading";
	} else {
		buttonText = request.text;
	}
	request.Dispose();
	isLoading = false;
}

We keep track in our game, frame to frame if we are loading, when we are no longer loading, we allow the button to work again.

This method can be adjusted for whatever you need.