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
1
Question by scarffy · Nov 10, 2020 at 02:12 AM · timedatetime

Check System Time and execute function

Hi,

I have a function I want to trigger based on current real time.

For example, there is an event that happening at 2pm and I want to execute a function to play a video.

I have create this code but I am not sure if this is the right way

  DateTime eventTime;
 
         private void Update()
         {
             if(currentTime.Date != eventTime.Date)
                 return;
 
             if(currentTime.Hour == eventTime.Hour && currentTime.Minute == eventTime.Minute)
             {
                 PlayVideo();
             }
         }




If this a good way to do the code?

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

1 Reply

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

Answer by jackmw94 · Nov 10, 2020 at 10:58 AM

I haven't had a proper look at DateTime but I expect that it includes very precise float values meaning it becomes incredibly unlikely that two times are ever equal (unless they came from the same source).

What you can do is to check whether the eventTime has elapsed:

 DateTime eventTime;
 bool videoHasBeenPlayed = false;
  
 private void Update()
 {
     if( !videoHasBeenPlayed && currentTime > eventTime )
     {
         videoHasBeenPlayed = true;
         PlayVideo();
     }
 }

This could make perfect sense to you but for avoidance of doubt, I'll explain it a little - usually the > sign test whether the former value is larger however DateTime objects use the > operator to test whether the former value is later. I've opted for a check that we're passed your eventTime because it doesn't take into account the Update step time which is about 16 milliseconds for a 60FPS game. We use the videoHasBeenPlayed flag to ensure we only call the PlayVideo function once, setting this to true inside the if-statement makes sure that we can't enter that if-statement again (without resetting that videoHasBeenPlayed back to false). You might have come across it while working on this but the bottom if-statement in your code will evaluate to true for a while minute causing PlayVideo to be called a lot.

Hope this helps! Let me know if not

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 scarffy · Nov 11, 2020 at 01:54 AM 0
Share

Thank you for your answer @jackmw94 Following your suggestion, I made some changes to my script

 void CheckTime(DateTime curTime, DateTime sesTime, DateTime enTime)
         {
             // Compare the time
             int compareResult;
             compareResult = DateTime.Compare(curTime, sesTime);
 
             if (compareResult < 0)
             {
                 // Time Earlier
                 // Do coroutine?
                 Debug.LogError("Time earlier");
                 TimeSpan timeLeft = sesTime - curTime;
                 Debug.LogError("time left " + timeLeft);
 
                 if (timeLeft.Hours < 1)
                 {
                     if (timeLeft.$$anonymous$$inutes < 60)
                     {
                         float fl = (float)(timeLeft.$$anonymous$$inutes * 60);
 
                         StartCoroutine(WaitAndPlay(fl));
                     }
                 }
             }
             else if (compareResult == 0)
             {
                 // Time to play
 
                 PlayYoutube();
 
             }
             else
             {
                 // Time Passes
                 // If not end, play video
 
                 TimeSpan timeLeft = enTime - curTime;
                 //Debug.LogError("time passed before end " + timeLeft);
                 if (timeLeft.$$anonymous$$inutes > 0 && timeLeft.Hours < 1)
                 {
                     if (!string.IsNullOrEmpty(fullLinkYoutube))
                     {
                         PlayYoutube();
                     }
                 }
             }
         }


 IEnumerator WaitAndPlay(float value)
 {
             yield return new WaitForSeconds(value);
             //currentTime = DateTime.Now;
             //Debug.LogError("Calling test at " + currentTime);
             PlayYoutube();
 }



What do you think of this?

avatar image jackmw94 scarffy · Nov 11, 2020 at 02:44 AM 0
Share

At the end of the day the question is does it work? I've got a feeling it's not quite there yet but it's a big step up :D

$$anonymous$$y first question is when is this function called? And if the answer is it's called ONCE only then is its job to setup playing the video at the right time? If the answer is every frame then is its job to check whether it's time and playing the video if so?

If the answer is that this is called once then this can be as simple as:

 public void CheckTime(DateTime currTime, DateTime vidTime)
 {
     TimeSpan timeRemaining = vidTime - currTime;
     int secondsLeft = timeRemaining.Seconds;
     if (secondsLeft >= 0)
     {
         StartCoroutine(WaitAndPlay(secondsLeft));
     }
     else
     {
         Debug.LogError("Too god damn late");
         // play straight away if too late? up to you
     }
 }
 
 private void WaitAndPlay(float value)
 {
     yield return new WaitForSeconds(value);
     PlayYoutube();
 }

I think you should forget the possibility that DateTimes can be equal to one-another, since they have milliseconds in it's likely that we will never hit the condition where they are equal. Even for updates each frame, these happen milliseconds apart so one frame we could be 2 milliseconds before and the next be 10 milliseconds after thus the time elapses without them ever being equal. It's not like you lose much precision doing this anyway, I doubt anyone's going to notice if your video plays at 0 or 20 milliseconds passed the given time!

If the answer is that it gets called every frame, then we're probably going to be playing that video quite a lot! I haven't ran this so this is all speculation based on what it looks like.. but say we enter that function one frame and we're 32 $$anonymous$$utes off your chosen play time:
Compare result will be positive so we enter the first if-block, hours is 0 so we enter the second if-block and $$anonymous$$utes is 32 so we enter the third then we start the coroutine scheduling it to play in 32 $$anonymous$$utes. The next frame we enter this function again at 31 $$anonymous$$utes 59 seconds and 980 milliseconds and do the exact same thing. We'll probably end up creating thousands of coroutines before we hit our time!

In conclusion, if you're calling this once then great, we just need to check how long we've got left then pass that time to your very nice WaitAndPlay function. If you're checking every frame then we don't want to call any function that will cause it to start more than once, so wait until the current time has gone passed the target time THEN play the video BUT make sure you only do that once! Have a check (a bool variable in the class, not in the function) that says if we have tried to play this video before then we don't have to do it again :)

avatar image scarffy jackmw94 · Nov 12, 2020 at 01:52 AM 0
Share

I agree with you. I should just forget DateTimes can be equal to one-another.

I do need to them to play once, but there are many video lining up to play after one video done playing. I'm not sure if I need to have a stop playing video or not since the url parameter will be pass into the video player directly and I will call PlayYoutube();

Thank you for your answer

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

139 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make a date/time system with a fast forward feature? 2 Answers

How can I track how much time has passed since a game was turned off? 2 Answers

How to compare two dates? 1 Answer

How to make notification appear at certain time without having to press button again? 2 Answers

Check How Many Minutes Have Passed 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