How To Tell How Much Time Has Passed?

G’day,

A quick question that probably has a simple answer. Using the datetime functions (Date Time | Unreal Engine Documentation)

How can I tell how many minutes have passed?

I have a save file for a game, which stores the datetime that it was created. I’d like to check the difference in time between the save files datetime and the current time to see if it’s been more than say, ten minutes. If it has, do this, if it hasn’t, do that.

Any help is appreciated!

There are two ways to do this:

One is to get the actual system time (as you probably did) using the Now node, which returns a Date Time Structure, presumably something of the form Year-Month-Day-Hour-Minute-Second. Given such a Date Time Structure, you can then query the elements (GetDay, GetHour, GetMinute). So you could implement a function that checks the difference between two such structures by comparing all the date elements. You’ll need to pay attention to overflows: obviously more than ten minutes have passed from 15:23 to 15:42, but the same is true for 15:57 to 16:08. When testing your function, you can manipulate your system clock to test whether you catch all such cases correctly. (Or you might decide that it’s okay if there are some edge cases, such as rolling past midnight, where you just don’t care.)

Alternatively, you could use Get Real Time Seconds (since the session has started) and compare the differences between two second counts. Converting to minutes is easy in this case.

Awesome, thank you =) I will give it a whirl and report back!

Having some serious trouble working this one out, any more help is appreciated =)

Basically, I have a merchant NPC in place that the player can buy/sell goods to. When they leave the merchant, it currently resets what he has for sale. But what I want to do, is save the date and time that they left the NPC (already doing) and if they come back and it’s been less than say 10 minutes the current inventory with the items the player sold are in it. But if its been more than 10 minutes, the merchants items have reset.

It’s to give the player an opportunity to re-buy items if they accidentally sold them.

That’s probably easier using a Timer node.

When your character leaves the shop start a Timer to call ResetInventory (or whatever) after 10 minutes (600 seconds) have passed. If the character returns while the timer is still running, stop the timer.

I haven’t actually used timers yet in Unreal 4.9, but the system looks fairly straightforward. In a quick test, I didn’t get any errors or warning trying to clear an uninitialized handle (when first entering the shop, the variable hasn’t been set yet).

If you need to save the timer to continue at the same time when loading a savegame, you can use Get Timer Remaining Time and save the returned float value. Then when loading your savegame and this value is greater than zero, simply restart the timer (using Set Timer By Event) with the remaining time instead of the full 10 minutes.

58416-timer_getremainingtime.png

Good luck!