2
\$\begingroup\$

I'm developing a Unity based game purely for fun but I'm stuck with a problem. How can I implement a system that keep my game running even when is closed? I don't want to HAVE IT running when it's closed, but I was thinking to add some kind of timer and compute the logic of the game with the time difference between (LAST_SESSION)-(ACTUAL_SESSION).

But how can I implement this? The thing is, how can I "accelerate" the game when I open it so that it's current state is equal to what would have been if I would not have closed it?

EDIT: The game is a sort of RTS. You control a base that has some number of entities that can move independently. Each entity has some kind of job like mine minerals, defend the base from enemy AI, build a structure. The mechanics are the interactions between each entity and the world.

I have a main loop where I update every entity, one at a time. I was thinking that I could do some kind of Load function that calls that main loop with a time_elapsed variable and with every iteration lower this variable by Time.deltaTime or something like that, and when it reaches zero I can start the game.

Sorry for my bad english.

\$\endgroup\$
  • \$\begingroup\$ That really depend on what you game is. Do you already have some save/load system? What do you store? \$\endgroup\$ – michalsrb Feb 27 '18 at 14:38
  • \$\begingroup\$ Different mechanics will need different techniques. If you have a resource that accumulates/drains, calculating its value when you return is just a mathematical function of the time since you last played. For more complicated interactions - especially chains and loops where event A triggers action B triggers reaction C - you have to iterate through each interaction event in chronological sequence and process its consequences. Tell us more about the specific mechanics you're trying to accelerate and we can propose solutions. \$\endgroup\$ – DMGregory Feb 27 '18 at 15:55
  • \$\begingroup\$ Edited with more infos \$\endgroup\$ – pava Feb 27 '18 at 16:13
  • \$\begingroup\$ What you describe can work, but it might take a substantial amount of real-world processing time to update all your units enough to account for hours or days of away time - meaning long waits for the player. Would you be amenable to strategies that "chunk" these mechanics into forms that are simpler to simulate over long time spans? (eg. treating a worker harvesting & delivering materials like a pipeline with a particular throughput, rather than an AI agent making decisions at every tick) \$\endgroup\$ – DMGregory Feb 27 '18 at 16:18
  • \$\begingroup\$ Yes, that's the alternative I was thinking if it was too much time-consuming. I could calculate in-game the average of the resources' throughput per hour and use it as a guideline. Do you have other advices? \$\endgroup\$ – pava Feb 27 '18 at 16:29
3
\$\begingroup\$

The proper way to do this is, when you exit the game, save the current time somewhere in your project. My .Net skills are awful, but a quick google search suggests System.DateTime.Now.

When the game opens again, check again the time with System.DateTime.Now and compare it with the saved one to calculate how much time has passed. Based on the result, apply your logic for what every object in the scene has been doing all this time.

Pseudo-code:

void OnExit() {
    now = SecondsSinceEpoch();
    write("file.save", now);
}

void OnOpen() {
    last_time = read("file.save");
    now = SecondsSinceEpoch();

    time_elapsed = now -last_time;

    // time_elapsed now holds how many seconds has passed since last time
}

Note that people can change their system time to mess with this.

\$\endgroup\$
  • 3
    \$\begingroup\$ If you really desire to make sure changing the system time doesn't mess this this logic, there is the option to ping an online source for the time. This however introduces the need for networking and handling network failures. \$\endgroup\$ – JustWannaFly Mar 8 '18 at 17:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.