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.

  • \$\begingroup\$ That really depend on what you game is. Do you already have some save/load system? What do you store? \$\endgroup\$
    – michalsrb
    Commented Feb 27, 2018 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
    Commented Feb 27, 2018 at 15:55
  • \$\begingroup\$ Edited with more infos \$\endgroup\$
    – pava
    Commented Feb 27, 2018 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
    Commented Feb 27, 2018 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
    Commented Feb 27, 2018 at 16:29

1 Answer 1


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.


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.

  • 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\$ Commented Mar 8, 2018 at 17:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .