x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

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

Product Version: Not Selected
Tags:
more ▼

asked Mar 20 '17 at 08:25 PM in Blueprint Scripting

Tomazzah gravatar image

Tomazzah
7 2 3

(comments are locked)
10|2000 characters needed characters left

1 answer: sort voted first

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:

alt text

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.

gi.png (177.2 kB)
more ▼

answered Mar 20 '17 at 08:56 PM

Mosel3y gravatar image

Mosel3y
1.2k 41 12 47

Tomazzah gravatar image Tomazzah 2 days ago

HI again mosel :P

alt text

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 alt text

time1.png (194.1 kB)
time2.png (108.3 kB)
Mosel3y gravatar image Mosel3y 2 days ago

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.

Tomazzah gravatar image Tomazzah 2 days ago

It isnt showing up. I tried it in my character blueprint just to see, and it didnt show up there either :/ 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

Mosel3y gravatar image Mosel3y 2 days ago

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.

Tomazzah gravatar image Tomazzah 2 days ago

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

Mosel3y gravatar image Mosel3y 2 days ago

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

Tomazzah gravatar image Tomazzah 2 days ago

thank you :) im using 4.14.3

Mosel3y gravatar image Mosel3y 2 days ago

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:

alt text

save.png (70.3 kB)
Tomazzah gravatar image Tomazzah 2 days ago

still nothing :/

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?

Mosel3y gravatar image Mosel3y 2 days ago

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.

Tomazzah gravatar image Tomazzah 2 days ago

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.

Mosel3y gravatar image Mosel3y 2 days ago

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

Tomazzah gravatar image Tomazzah 2 days ago

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

Mosel3y gravatar image Mosel3y 2 days ago

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

Tomazzah gravatar image Tomazzah 2 days ago

alt text

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

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: alt text

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

characterbp.png (177.7 kB)
characterbp2.png (90.8 kB)
characterbp3.png (84.5 kB)
Mosel3y gravatar image Mosel3y 2 days ago

What bit isn't working though?

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

alt text

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

gii.png (55.5 kB)
Mosel3y gravatar image Mosel3y 2 days ago

What bit isn't working though?

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

alt text

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

gii.png (55.1 kB)
Tomazzah gravatar image Tomazzah 2 days ago

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.

Mosel3y gravatar image Mosel3y 2 days ago

Oh, well you have to actually do something with the SecondsSinceLastPlay. I was just showing you how to get that number.

To add the minerals, something like the below would work:

alt text

This is just an example, you may have to tweak it for what you need. Also, make sure you are saving the player save when closing the game too obviosuly, I think you are doing this already.

saves.png (164.4 kB)
Tomazzah gravatar image Tomazzah 2 days ago

Oh thank you! I had been putting the secondssincelastplay into the "Eventick", which updates the minerals ingame and giving me all sorts of answers.

Yea the maths needs work but thats because of my variables.

Thank you so much, my eyes burn and i can finally sleep with a peace of mind. Hats off once again to you sir :)

(comments are locked)
10|2000 characters needed characters left
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question