# How to structure a game that continue even when closed?

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.

• That really depend on what you game is. Do you already have some save/load system? What do you store? – michalsrb Feb 27 '18 at 14:38
• 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. – DMGregory Feb 27 '18 at 15:55
• Edited with more infos – pava Feb 27 '18 at 16:13
• 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) – DMGregory Feb 27 '18 at 16:18
• 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? – pava Feb 27 '18 at 16:29

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() {