Getting the time of the game being inactive

Hello!!

I’m thinking of implementing a weapon factory for my game and I want it to produce while the game is inactive, so I want to substract the time of the last activity from the new activity in a float value.

Do you have any suggestions on how to acchieve this?

The System.DateTime class can give you an interval between any two arbitrary dates/times.

Save the time when the Application quits, then compare it to the time on app restart, and based on how much time passes, do your thing.

NOTE: this is not the same time mechanism as Unity’s UnityEngine.Time class.

This is limited by the fairness of your user tho. If they change the internal clock of the device they can get around wait times. This problem has been there for a very long time on mobile, but if that’s not a problem you can make do with this approach.

If you want more secure time, use a server clock if the game is online required. Just request the time from any online clock… may it be your own server, or using a public NTP clock like google’s public NTP:

Though I’d back up a second and ask you to define “activity”. If by “activity” you mean the closing and reopening of the program/app, these answers are what you’re looking for (may you use your system clock or a network clock… system clock being faster but can be user manipulated… though technically even a server clock can be manipulated by spoofing their local network DNS).

BUT if you mean something like “the last time they moved their mouse” or something like that as activity. You COULD just use UnityEngine.Time. And really your question is about how to track when they’ve gone inactive and became active again.