- Home /
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?
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
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?
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 :)
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
Follow this Question
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