Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ZirGrizzlyAdams · Jan 21, 2014 at 03:06 AM · loopwhilewhile loopspellsinterrupt

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;
        }
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Vire · Jan 21, 2014 at 03:08 AM 0
Share

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

avatar image ZirGrizzlyAdams · Jan 21, 2014 at 12:58 PM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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
 }
 
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ZirGrizzlyAdams · Jan 22, 2014 at 04:00 PM 0
Share

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.

avatar image ZirGrizzlyAdams · Jan 22, 2014 at 04:03 PM 0
Share

Never$$anonymous$$d I think i know what i must do. Thanks

avatar image KellyThomas · Jan 22, 2014 at 04:31 PM 0
Share

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.

avatar image
1

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;
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image moghes · Jan 21, 2014 at 09:29 AM 1
Share

@ZirGrizzlyAdams this answer must answer your question, please tick as true

avatar image ZirGrizzlyAdams · Jan 21, 2014 at 12:56 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges