- Home /
Breaking from a Loop
What I want to happen is: if p1Fizzled = true at anytime it will stop the whole function. The yieldwaitforseconds is my spell cast time, but if p1fizzled=true then i want it to stop casting all together. The ct variable is to make it only run once otherwise it keeps looping. Any ideas on how to get the whole function to halt when p1fizzled = true?
function CastFireball()
{
var spellLvl = 1;
ct = true;
combatIndex.isCasting = true;
combatIndex.spellLoaded = false;
while(p1Fizzled == false && ct == true)
{
yield WaitForSeconds (4);
if(combatIndex.isCasting == true && p1Spellsent == false)
{
p1Lastspell = 10;
p1Lastmana = 10;
spellwait = 0;
combatIndex.spellLoaded = true;
combatIndex.CastRecovery();
ct = false;
}
Any reason you can't just use return;
? Edit: Sorry I meant to say break;
rather than return;
- break will leave the loop meanwhile return will leave the function completely
But you cannot break during the 4 seconds of yield. Yes its true i could break after but not during. I would like the stop the cast time immediately.
Answer by KellyThomas · Jan 21, 2014 at 02:07 PM
If you have a need to run code immediately when casting stops you may need to split your logic over a number of methods. Here is a three stage approach:
var isCasting = false;
function FireballStart() {
isCasting = true;
//perform start logic
yield FireballLoop()
}
function FireballLoop() {
while(isCasting){
//perform loop logic
yield WaitForSeconds (4);
}
}
function FireballStop() {
isCasting = false;
//perform stop logic
}
What you have and what you are saying is correct. Im more worried about once it hits the yield waitforseconds (4). I cannot snap it out of that yield. If you take damage during that 4 second yield i want you to stop casting.
Never$$anonymous$$d I think i know what i must do. Thanks
If the player takes damage just call FireballStop()
(and play the ouch animation or whatever you would normally do).
The loop inside the FireballLoop()
will not run again as the while's condition will evaluate to false. For this to work optimally it is important that the yield
is placed on the last line of the loop.
Answer by YoungDeveloper · Jan 21, 2014 at 07:10 AM
break; //breaks current loop
return; //returns a value - ends function, as void does not return anything, its just return;
@ZirGrizzlyAdams this answer must answer your question, please tick as true
But you cannot break during the 4 seconds of yield. Yes its true i could break after but not during. I would like the stop the cast time immediately.
Your answer
Follow this Question
Related Questions
Stop Current Coroutine, Then Restart It 1 Answer
How to wait for Coroutine to finish? 2 Answers
While Loop Issue 0 Answers
While loop crashing 2 Answers
Problem with while statement. 1 Answer