- Home /
coroutine spamming webserver which then stops replying
I have a coroutine. My question is how do I make it only run once? It seems that the routine is repeatedly running so many times that the webserver think's i'm attacking it with a dos attack and stops replying. This breaks my game and causes a crash.
Here is the code.
void OnGUI () {
if(showMyDecks == true)
{
//here we need to query a php script to see if the player has any decks.
//and display those decks with what ever the player has named these decks.
if (deck1 == null)
{
StartCoroutine("getDeck1");
while (deck1 == null){
}
StopCoroutine("getDeck1");
}
and the coroutine
public IEnumerator getDeck1(){
WWWForm form = new WWWForm ();
form.AddField ("userId", userId);
form.AddField ("deck", "1");
WWW www = new WWW(url, form);
yield return www;
response = www.text;
//this should respond with the name of the deck.
deck1 = response ;
yield return;
}
any help would be appreciated thank you.
Answer by NorthernVisionStudio · Jul 05, 2014 at 03:58 AM
OnGUI is called every frame, which is the source of your spamming behavior. That means OnGUI is starting a new coroutine each frame. The simplistic way to 'fix' it is to make a new variable called "busy" in your main Monobehavior class. Then here would be your coroutine:
public IEnumerator getDeck1(){
if (!busy)
{
busy = true;
WWWForm form = new WWWForm ();
form.AddField ("userId", userId);
form.AddField ("deck", "1");
WWW www = new WWW(url, form);
yield return www;
response = www.text;
//this should respond with the name of the deck.
deck1 = response ;
busy = false;
yield return;
}
This will run as quickly as the web server returns the data. You may want to insert a delay in the coroutine to tone the frequency down, such as yield return new WaitForSeconds(x)
Great answer! I ended up created a variable and in OnGUI() checked for it to be true with an if statement rather than checking in the coroutine.
The real problem was me not realizing OnGUI() was starting the coroutine every single frame.
Thanks again! I must have spent hours in this already lol.
Answer by musaranya · Jul 05, 2014 at 04:13 AM
I think your coroutine is been called each time OnGUI is executed and until the first call sets a value to deck1. You can try the follow:
declare a flag as a class member to prevent calling the coroutine more than once and initialize it to false
set this flag to true when entering the coroutine and false when finishing it
always ask for the flag value before call the coroutine
bool getDeck1Flag;
void Start() { getDeck1Flag=false; }
void OnGUI () {
//here we need to query a php script to see if the player has any decks. //and display those decks with what ever the player has named these decks. if(showMyDecks && !getDeck1Flag && deck1 == null) StartCoroutine("getDeck1"); } public IEnumerator getDeck1(){ getDeck1Flag=true; WWWForm form = new WWWForm (); form.AddField ("userId", userId); form.AddField ("deck", "1"); WWW www = new WWW(url, form); yield return www; response = www.text; //this should respond with the name of the deck. deck1 = response ; getDeck1Flag=false; yield return; }
This is only an approach but I think it could work. From here you need to think what would happen if the server doesn't response and update the code accordingly. Hope it helps!
Your answer
Follow this Question
Related Questions
Animation method/coroutine playable once or loop 0 Answers
Anything like run once? 1 Answer
AI function/coroutine animation issues 2 Answers
StartCoroutine works once 1 Answer
Do I have to tell unity to load assets only once with cSharp script 4 Answers