Keep variables running (like income) while game is closed

Hi again

Genuinely think this is the last piece of coding ill need to learn for my project (hoping)

The player has a source of income (variable called income) that is constantly being added to his total money (total).
It is an incremental game so i would like for the income to continue adding to the players total even when they have closed the game. This is added every second by just using the Delay node.

Ive heard of the “Get accurate real time” node but Ive no idea how to use it

So, question is, how would i keep the variables running even when the game is closed?

Example, the player has 100 money, and the income is 1 per second. If the player closed the game for 60 seconds, they would have 160 money when they open it again

Something like this using GameInstance should work.

It will require creating a game instance class, and setting it as the default in project settings.
Then add the following nodes:

EventShutdown, and EventInit are not custom events, right click and type their names to find them.
CurrentTime and LastCloseTime are DateTime type.

You will also need to create a save game class, do that and add a DateTime variable in it, CloseTime.
In the create save game object, put you custom save class.

You’ll just need to account for things like when it is first run and their is no save, and also, if the game crashes or closes unexpectedly I doubt EvenShutdown will be called. So you could have it periodically update the LastClosed time, even when the game is still open, by overwriting that save.

HI again mosel :stuck_out_tongue:

Thought i had it there. When i drag off the create save game object, i don’t get the “Close Time” variable that i created in the Saves class. I tried casting it but it didnt work

I also have it set that it creates the save file on startup, not sure if this would interfere. This is in the character blueprint

Hey, ha. Drag off from the return value on create save game object, and it should come up (Don’t drag from the exec pin). And make sure you have the right save class selected there too.

It shouldn’t interfere if you use a different slot name.

It isnt showing up. I tried it in my character blueprint just to see, and it didnt show up there either :confused: not sure what im doing wrong

The variable is made in my “saves”, which is what my save blueprint class is, throwing everything at it but it doesnt want to work

Hm, not really sure then. You have just one save classs do you? Double check that you named it correctly, re-compile and save the class. Maybe try making the variable editable in your save class by checking the eye symbol beside its name too, though you shouldn’t have to do that.

Yea, deffo only 1 save class
The variable is there with the eye symbol on. None of the variables in my save appear though, in any blueprint.
Im at a loss

What engine version are you using? I’ll see if I can reproduce this.

thank you :slight_smile: im using 4.14.3

Ah, looks like you do have to Cast to your save type.
In 4.15 you don’t have to apparently. Can you try that?

So it will look like this now:

still nothing :confused:

in my time2 picture, they are running of an “eventbeginplay” node in my character blueprint. It is also creating the save AND loading it. would they be interfering perhaps?

Very weird. The stuff in time2 screenshot shouldn’t affect it, no. Although you should change the slot name in the GameInstance save, because otherwise it will overwrite the other save data. The gameinstance save is just for checking the time.

Just to confirm - you can no longer get any of your custom variables from your save class even when you cast to it? And you recompiled the save class and saved it? If so, I would say just, restart the editor if you haven’t already.

Apologies, i explained that wrong. The casting works and i can replicate your blueprints, but i meant the game does not run after i close it.

Have you changed the name of the save slot in game instance?

Yea, it looks exactly like yours, i changed it to “Time” as well.
The save slot in my character blueprint is Player.
Both are using the same savegame blueprint

Maybe make a new save game class for the CloseTime variable. I’m confused as to what isn’t working for you now though, your game won’t open at all?

I think while they were both saving in the same slot, the save overwrote everything in your save, so if you are trying to load a save and do something depending on what is in that save, that isn’t going to work now. I’m not really sure on that since I can’t see all of that blueprint

This is running of eventbeginplay in the character blueprint. This works fine

Underneath, ive put this so that when i press L, minerals and income saves. This works okay also.

I have the exact same blueprint as you in my gameinstance. Ive even tried to add the “Seconds since last play” variable to the blueprint that adds the income to the minerals, which is wrote like this:

Im at a total loss at this

EDIT: the game opens fine and saving the minerals and income by pressing L works, its just quitting the game and reopening does not give the minerals that should have been earned while the game was off

What bit isn’t working though?

If you want to get the SecondsSinceLastPlay from game instance, you do it like this:

And you can do that from anywhere. I presume you’ve set your game instance as the default in poject settings?

What bit isn’t working though?

If you want to get the SecondsSinceLastPlay from game instance, you do it like this:

And you can do that from anywhere. I presume you’ve set your game instance as the default in poject settings?

Yea i have
The problem was that when i closed the game and opened it again, it wasn’t giving me the minerals that i should have earned while the game was closed
Like in the example i used, the player has 100 money, and the income is 1 per second. If the player closed the game for 60 seconds, they would have 160 money when they open it again, BUT when i open it again i still have 100

And that BP in your last message works, it tells me how long its been since i closed the game.