SimTime using real time? (SiO2)

Hi

Every time restarting game server SimTime starts from 0.

What about adding a boolean option useRealTime to SimTime so that when enabled, it directly passes System.nanotime()?

This can be useful for persistent components/systems that require using real-time.

Note, alternatively, I can directly use System.nanotime(). :slightly_smiling_face:

Regards

“real time” and “simulation time” are two different things.

So if you don’t play the game for a year but a year’s worth of time must pass then use real time. If you want to know how much simulation time has passed, ie: time when the game was actually running then use SimTime.

Do note that System.nanoTime() is also not “real time”. It has no direct basis on system time. You have to use System.currentTimeMillis() if you want actual real time.

I’m trying to understand when this would be useful, though, unless the game continues to ‘play’ even when it’s not running somehow.

Edit: note… I do see that there is no settable bias which makes it a little tough to continue a game where one leaves off. Unless I’m overlooking something, that’s going to be a problem for me also.

1 Like

Googling this I noticed System.currentTimeMillis() is based on a fixed origin but can be affected if the user change the machine time. (i.e easy to cheat)

I am not sure which one I should use then. I do not need the actual real time, just want to know the time passed since the last saved time. (Could be that player left the game, stopped JVM and turned of the machine)

Can System.nanoTime() be used in this case?

nanoTime() is using a CPU counter. Historically it could even be different from CPU to CPU which was really interesting when threads would hop around. I count on the fact that they’ve fixed that. But it’s starting point is still arbitrary.

SimTime needs an easy way to force set the offset. (ie: back-date the base time) This can be faked with the existing API but it’s not what I’d consider straight-forward.

When your game shuts down, get the time: getTime() and save it.

When the game starts up again, instead of letting the default update(System.nanoTime()) initialize SimTime, initialize it yourself with System.nanoTime() - timeYouSaved… which should back-date the base time.

Edit: note that I have not looked to see if the rest of the GameLoop/GameSystemManager makes that easy at all.

1 Like

Do you mean single execution of the JVM? Does the arbitrary value change when I start a new instance of the JVM?

If it changes then saving the last time will not be safe.

Edit:

from javadoc I noticed

The same origin is used by all invocations of this method in an instance of a Java virtual machine; other virtual machine instances are likely to use a different origin.

So it is not safe to be used in my case. I will use currentTimeMillis() as suggested.

You can’t really prevent cheating there, unless you take the time from some “web time API” or something and even then I can proxy that or just patch your class.
So if that really is a concern, it needs separate consideration.

NanoTime is probably based on some QueryPerformanceCounter stuff from the Windows API (or depending on the OS), so I would look around what they do.

I see. Thanks

The approach I outlined would avoid all of this and cheating… I just don’t know if the API easily supports it yet.

That will back-date to the time player left the game and will ignore the time that has passed since the player was not in the game. So I still need to add to it the time passed since the last login. (maybe I need to add a REST API to save the game session start/stop time. Similar to web API Darkchaos mentioned)

Sorry if the question was not clear enough.

Yes, to be crystal clear.