How to fire an event when save game value changes?

I created a blueprint class SavedGameData which extends SaveGame:

I added a string variable CurrentCategory. I want to save the game data every time it changes so I set it to RepNotify.

Now, when the RepNotify is called, I want to fire an event so I can save the game data (I want to be able to fire the event from multiple places).

I created a custom event in my GlobalVars blueprint:

But, I can’t see how to fire the event. I tried to use an event dispatcher, but then I have to get a reference to the global vars object which is not available.

I must be missing something simple, but search the web and this forum did not help.

Edit:
This is a single player game, but other threads said I can still use RepNotify to detect when a variable changes.

Hey @NeilAgg

you could:

  • Use the GameInstance for your global variables / load and save functions / etc
  • Use the event that should change your CurrentCategory to also save your game instead of changing the name and then execute the save function

Here is an example

UE52_SaveGameData.zip (175.4 KB)

When you hit a coin the save function gets executed.
:slight_smile:

Thank you for the response. I was thinking it would be less error prone to fire an event on the variable being changed than to keep track of all the different places that change it. Is there no good way to do that?

You can use the

  • GameInstance
  • PlayerController
  • PlayerPawn
  • PlayerCharacter

They are always accessible, but you will have to cast to your current blueprint like BP_GameInstance. Then you can change variables in this blueprint, e.g.:

And inside the BP_GameInstance - as an example- I set the variable to RepNotify.

Only GameInstance survives map changes.

1 Like

That’s why you change it via a Setter. A function or a custom event that sets the variable.

And this is the only way to change the var. Everybody must call it, they cannot just grab a var and set its value - that’s unsafe. Go further and:

image

Now no can access it directly, they must call:

image

And since everyone has to call the event… there’s only one place now that can fire the save game logic.

1 Like

If I understand this correctly, you are setting Rep Notify on the struct and there is a Custom Event which will save the data.

But, how do I hook the Rep Notify to call the custom event?

This seems like a good solution, thank you for the idea!

1 Like

I decide to implement my code this way. Thanks for the tip!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.