Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
2
Question by biohazard · Jul 25, 2011 at 09:54 AM · javascriptcoroutinetime

Keeping track of time?

The thing is, i want to give my coroutine EXACTLY one second to perform all it's actions, and (because some computers are faster than other) wait for this second to complete even if the coroutine finishes in 200 ms..

any advice on how this is done?

Comment
Add comment · Show 7
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 CHPedersen · Jul 25, 2011 at 10:29 AM 0
Share

$$anonymous$$easure the time the operation takes with a StopWatch, then call yield WaitForSeconds with the difference between 1 second and the stopwatch's elapsed time.

avatar image biohazard · Jul 25, 2011 at 10:41 AM 0
Share

Good idea. but i want to ensure that it runs good on all computers, since there might be machines who need more time for the coroutine's execution.

i can't just hard-code stuff which i might want others to use later

avatar image CHPedersen · Jul 25, 2011 at 10:46 AM 0
Share

You're not hardcoding anything but the 1 second. You said you wanted the coroutine to return after exactly 1 second. $$anonymous$$y suggestion does that.

avatar image biohazard · Jul 25, 2011 at 10:47 AM 0
Share

i need everything inside the coroutine done in one second no matter if the machine is faster than that.

like, if my machine does the complete coroutine in 200ms it needs to wait for 800 ms

avatar image CHPedersen · Jul 25, 2011 at 10:49 AM 0
Share

I know. What I posted does that. Would you like a code sample?

Show more comments

4 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by CHPedersen · Jul 25, 2011 at 11:01 AM

Try something like this:

 System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();

 IEnumerator SomeCoroutine()
 {
     stopwatch.Start();

     // Do operations

     stopwatch.Stop();
     float timeTaken = 0.001f * stopwatch.ElapsedMilliseconds;
     stopwatch.Reset();
     // Wait for 1 second, minus the amount of time that the operations were measured to take:
     yield return new WaitForSeconds(1 - timeTaken);
 }

You might have to take a second to translate it to javascript, of course, but that's the general idea.

Comment
Add comment · Show 6 · 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 biohazard · Jul 25, 2011 at 11:40 AM 0
Share

Looking nice, will try it

avatar image biohazard · Jul 25, 2011 at 11:41 AM 0
Share

Javascript doesn't have a stopwatch from what it looks like. Also, i'm using unity free

avatar image CHPedersen · Jul 25, 2011 at 11:49 AM 0
Share

JavaScript should have access to exactly the same as Unity's C# does, since they all compile to the same intermediate language and are all based on $$anonymous$$ono's .Net. System.Diagnostics.StopWatch is a standard .Net 2.0 component. It has nothing to do with Unity, so it doesn't matter that your version is the indie version.

avatar image biohazard · Jul 26, 2011 at 07:33 AM 0
Share

@Christian H Pedersen

i'm getting a NullReferenceException when i try using the stopwatch :S

avatar image CHPedersen · Jul 26, 2011 at 07:47 AM 0
Share

There is no telling what's going wrong when there's no code sample displaying what you've done. ;)

Initial guesses include attempting to call one of its methods without having instantiated it?

Show more comments
avatar image
1

Answer by zeropoint · Jul 25, 2011 at 10:27 AM

How are you exactly counting 1s?

This does work:

 // Prints 0
 print (Time.time);
 // Waits 5 seconds
 yield WaitForSeconds (5);
 // Prints 5.0
 print (Time.time);

It's from documentation.

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 biohazard · Jul 25, 2011 at 10:30 AM 0
Share

yield is not what i need for this..i need realtime waiting...

like the coroutine gets 1 second for its work

if coroutine finishes in 200 ms i want it to wait the other 800ms

avatar image Waz · Jul 25, 2011 at 12:33 PM 0
Share

Then why are you using a coroutine at all? They are not threads. If you don't yield, the coroutine will completely block the UI, input, repainting, progress bars, everything.

If that's actually what you want, then just use a normal function call (and the Stopwatch).

I doubt very much that is what you want.

avatar image
0

Answer by zeropoint · Jul 25, 2011 at 10:56 AM

I don't think we have directly such method to achieve that but you can surely go for polling

 void YourCoroutine(float waitTime){
   float localTime = Time.time; // store the entry point time

   // your stuff
   // goes here
   // ...

   // check whether required time has elapsed
   while(Time.time - localTime < waitTime)
     ; // do nothing -- just wait for your time to elapse
 }

Note that this waiting time doesn't consider the time elapsed in calling and returning from the YourCoroutine method. If you need to take care of that too then take the line float localTime = Time.time; just before calling YourCoroutine and the while loop after calling it.

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 Herman-Tulleken · Jul 26, 2011 at 08:15 AM 0
Share

Does this actually work?

I was under the impression Time.time does not change during an Update?

avatar image Waz · Jul 26, 2011 at 10:51 AM 1
Share

I'm quite sure it doesn't. Time.time and Time.deltaTime are fixed for the entirety of Update and FixedUpdate, as they should be - you often want all Update functions to see the same values, and you need Time.time to always progress by the Time.deltaTime of the last Update.

avatar image
0

Answer by aldonaletto · Jul 25, 2011 at 12:51 PM

You can set the endTime at beginning, and let the routine check it before returning:

function OneSecondRoutine(){
  var endTime = Time.time + 1;
  // do your stuff here
  // wait endTime to finish:
  while (Time.time < endTime){
    yield;  // the routine will check endTime each frame
  }
}
Notice that:
1- You must use yield to free Unity while your routine waits, or your FPS will drop below 1 frame per second;
2- If you call this routine again before it has ended, it will start a new coroutine running in parallel to the first one. To avoid this, you must use a public busy boolean variable defined outside any function:
- only call OneSecondRoutine if busy is false;
- set busy to true at the beginning of OneSecondRoutine;
- set busy to false at the end of OneSecondRoutine;
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

8 People are following this question.

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

Related Questions

Increment X every one second. 2 Answers

Need help with this Rotation Script (fixes) 1 Answer

Finish One Combo 1 Answer

Realtime Clock? (needles) 1 Answer

Breathing sound effect using timers 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