Date Time Display Saved

Hi, I’m new to Unreal Engine so I’m not very familiar with blueprints. I was trying to create an on-screen display that tells me what day it is and the hours and minutes within the game while also using a day/night cycle. However, every time I reopen the game this timer resets and the sun is also always in the default starting position. How can I save the position of the sun (I rotate it on the y axis) and the day, hour and minutes? Thank you.

This is the code of level:



Daytime displey script:

1 Like

You need to save the data to a save game object, that can then be saved when exiting, then loaded when opening. You should read through Epic’s documentation on this:

Also, it’s wasteful to utilize the tick function for this type of functionality. I don’t think this needs to run every frame, so you should also look into utilizing Timers and having it run once or twice a second (depends on how incremental the change is in time and the sun rotation).

2 Likes

But do I have to create a “SaveGame” class? Can’t I do this directly in the main class? And if I have to create it, how can I get data from my main level (for example the degree of rotation of the sunlight)? Sorry for the probably trivial question.

2 Likes

@Sklor is correct. Absolutely correct.

But here’s a video that simplifies the process.

Gorka saves the player position, but you can adjust it to save whatever you want.

1 Like

Thanks! I will try.

2 Likes

I haven’t succeeded yet. If it helps, I’m using the free “CropoutSampleProject” template and wanted to modify it to test. This is the code I wrote in the “village” layer:

1 Like

1 Like

Did you watch the entire video? Because Gorka needed to amend something.

Also, can you get the entire function on-screen? You can reduce the sidebars and zoom out a little.

Also, screen shots of your BP_Save, please.

I’ll mention, you really should be handling save information in the game instance class, rather than the level blueprint because then it would be accessible by all other classes (utilizing the Get Game Instance node and casting to the appropriate game instance class).

I’m pretty sure it isn’t working because you aren’t creating the Save Game object prior to loading the data. You can put a print string node on the “Cast Failed” execution pin to verify the cast is failing. I think the “Does Save Game Exist” node checks if there is save data in the particular slot, not if there is a Save Game Object created that can be used.

The entire function:

BP_Save:

Yeah, you are checking to see if there is a save game in that slot, but not verifying if BP_Save is valid.

When execution validates that there is saved data in that slot, it goes to load it to an invalid Save Game Object.

So how can I fix it? Like changing when I check for saving the “save1” slot with a BP_Save object?

I’d change up the order of the logic.

  1. Create the save game object and set it to the BP_Save variable.
  2. “Does Save Game Exist” node with the associated branch.
  3. If true, load from slot and set the variable (like you already have).
  4. If false, you basically just need to save to slot so the data starts with the default value you provided in the BP_Save class.
  5. Load from slot again, to set the rotation to the BP_Save default.

Ok I will try!

Do you mean something like that?

Something like this:

Remember, when you create a Save Game Object it starts with the (default) variables/values you set in your SaveGame class. You then need to change those variables in the object itself, before saving in order to be able to load those values later.

So in my example, when the map starts it creates the object, and checks if the save game exists. If so, it loads from slot, casts to the SaveGame class, sets it to the BP_Save_Ref variable (so you can access it later), then pulls the sunLightPos variable from the loaded save game reference.

If there isn’t a saved game in the slot, it simply saves one with the default values from your SaveGame class. Assuming the map default for the sun’s rotation is the same as the default in the SaveGame class, nothing more needs to be done.

Now, when you leave the map or need to save the game, you reference the BP_Save_Ref (Save Game Object) and set the sunLightPos variable to the current rotation, then save the game to slot using that same BP_Save_Ref reference. I think this is the piece that you’ve been missing, which is why when you reload, it isn’t changing from the default value.

Does this make sense?

It is helpful to create individual functions for saving and loading, so those actions can be easily called.

I have done this for now but I don’t understand why my Sun Light Pos set does not have the target to be connected to the BP_Save object.
Also, if you notice any other errors, let me know.
Blueprint code:


I always put it in the “Village” level

“I don’t understand why my Sun Light Pos set does not have the target to be connected to the BP_Save object.”

Not sure I’m understanding this one. Which node are you referring to?

You definitely don’t need to copy the events I created, I just did that as a placeholder and to help explain the function. You’ll need to make sure in the “Map_Quit” event, you are getting the actor location of the SunLight actor (as you had in the previous images), rather than the actor location of the level itself.

As for level blueprints; just be careful with how you structure your code. Using level blueprints should really only be used for logic that is specific to that individual level and isn’t something that needs to be used anywhere else. If your project calls for multiple levels, this logic will need to be duplicated in each level blueprint (compared to only doing it once in the Game Instance class, that then has global accessibility).

I’m referring to this node:
Screenshot (96)