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 Riagan · Jul 18, 2013 at 05:11 AM · optimizationfor-loopacceleration

Why is my FOR loop Accelerating each time it is used?

Hey Everyone. I have an issue with the loop below. What happens is every time it runs through the loop it appears to be accelerating (the rate is increasing). This is used for a progress or turn bar for a turn based RPG to determine who goes first.

In the Debug Logs they always produce the same numbers, the rate they are posting is just increased. Does anyone know what is happening here?

(also, the comments and notes in the script are for me... they aren't really pertinent).

 function determineTurn () {

 for(i=1;i>0;i++) {

     yield WaitForSeconds(1);
     
 if (tempP1Agi < turnTotal && turn ==0 ){
         
     tempP1Agi = tempP1Agi + p1turn; //for 1,2,3 characters and monsters
     
     Debug.Log(p1turn.ToString());
     Debug.Log(tempP1Agi.ToString());
     
 }
 else
     {    if (tempP1Agi >= turnTotal && turn ==0){
     
         determineTurnActivated = false;
         
         tempP1Agi = 0;
         
         turn = 1;
         phase = 6;
         
         //StopCoroutine("determineTurn");
     
         }
         else {
             if (tempP2Agi >= turnTotal && turn ==0){
             
             tempP2Agi = 0;
             turn = 2;
             phase = 6;
         
             //StopCoroutine("determineTurn");
             
             }
     
         else {
             if (tempP3Agi >= turnTotal && turn ==0){
             
             tempP3Agi = 0;
             turn = 3;
             phase = 6;
         
             //StopCoroutine("determineTurn");
             
             }
         }
     
     }

 

}

Comment
Add comment · Show 4
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 Riagan · Jul 18, 2013 at 07:27 AM 0
Share

I have it in a separate function so that I can jump in and start the loop over and over again. Is it possible that the old function and new function are both working?

Also, I tried putting it into the Update section, but now the only issue is that it moves much too fast. I am trying a few work arounds, and I am thinking that having a cap for "i" to reach might help. Thanks for the input.

avatar image Dimling · Jul 18, 2013 at 10:30 AM 0
Share

I'm not quite sure if I understand correctly but if you have removed the function from the script i don't think that would be possible, that the old and the new function are working. You can try to do a "clean and rebuild".

If you got the code in the Update() function you can just remove your for-loop since the Update is called every frame.

avatar image Benproductions1 · Jul 18, 2013 at 10:40 AM 2
Share

If you want to run something infinitely, do while (true) { not a for loop...

I shudder at the lack of indentation and formatting...

avatar image Riagan · Jul 18, 2013 at 08:32 PM 0
Share

Thanks. The while (true) works much better. On a side note, I have been program$$anonymous$$g for about a month, I apologize for the terrible indentations and what-not.

2 Replies

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

Answer by Dimling · Jul 18, 2013 at 06:45 AM

 for(i=1;i>0;i++)

This code will run forever if i'm not mistaken. Is it supposed to do so? Since i = 1, and the loop should run as long as the variabel i is bigger then zero, something it will allways be.

If you want the code to run over and over again the Update() method will be the best, instead of a forever running loop.

Comment
Add comment · Show 7 · 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 Riagan · Oct 11, 2013 at 07:01 AM 0
Share

There is a 99% chance this won't make much sense, but... This is used to deter$$anonymous$$e character turns in a JRPG, so it runs simultaneously to the other update function and other characters rates based off of agility and other stats, etc. I am sure there is a way to do it with the Update function, but it works well the way I have it now, which is just killing the function and spawning a new one when the rest of the update is finished. $$anonymous$$y problem was that it spawned multiple of the same function ins$$anonymous$$d of carrying on each time. Thanks for the reply though! Also, I am terribly new at this and I am taking on a fairly complicated project... All help is extremely appreciated.

avatar image Hoeloe · Oct 11, 2013 at 07:18 AM 0
Share

That method is very performance hungry for what you want to do. You're spawning a new thread each time you call yield, which is really not what you want for this. Depending on what kind of system you want, you would either, for a system like Final Fantasy's ATB system, give each player a number, defined by their stats, which simply counts down in the Update function. When it hits 0, it's their turn. A higher initial value for this counter would give a slower character, so you'd need an inverse relation with speed. Alternatively, if you just want to do the logic and deter$$anonymous$$e who goes next instantly (a standard turn-based system), then do the same thing, but ins$$anonymous$$d of counting down over time, just select the lowest initial value - this will necessarily be the player who goes next.

These methods are far more efficient, better coding practice, and much closer to what you would actually do in this kind of environment.

avatar image Benproductions1 · Oct 11, 2013 at 07:36 AM 0
Share

@Hoeloe I believe you are mistaken in your understanding of coroutines. They are, in fact, not separate threads :)

avatar image Hoeloe · Oct 11, 2013 at 07:41 AM 0
Share

Yes, sorry, still half asleep here. Still, coroutines are not the best way to achieve this result.

avatar image Riagan · Oct 11, 2013 at 03:26 PM 0
Share

The way I did it was took the summation of each character/enemy agility and divided it by incriments of their own agility to deter$$anonymous$$e the turn. I think that is basically what you were saying. Also, even if I destroy the coroutine and make a new one, is it still performance hungry? Regardless, I am going to be doing some major revamping of all of the coding in December. If I remember, I will try to repost the new code, which will hopefully be infinitely cleaner. (and have indents and what-have-yous).

I have heard a lot of people suggesting to just use the Update Function, but if I remember correctly, I would have to wait for the countdown before doing the rest of the Update function (this probably isn't true), when I want the countdown to be simultaneous to other actions happening. Plus, OnGUI I think threw a wrench in my newb brain... I will pay you guys five dollars to make this game for me... lol

Show more comments
avatar image
0

Answer by Hoeloe · Jul 18, 2013 at 12:00 PM

As has been mentioned in the comments, for(i=1;i>0;i++) is the problem. Every time you're calling the function, you're spawning a new loop, yet because the for loop never terminates, the old one is still executing. This is producing the problem you're seeing. Be sure, when you're done with a loop, to use break to exit from it, which will terminate the loop.

On another note, if you want a loop to run infinitely, while(true) is a much better option than a for loop, especially since you're not actually using the value of i.

Comment
Add comment · 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

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

18 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

Related Questions

HELP!!!! GetAxis to Accelerometer :) 0 Answers

Performance issues with mesh processing 1 Answer

Performance issues when loading first scene 1 Answer

Draw Call Optimization 3 Answers

Improving script performance 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