Better way for global variables/saving/loading, need advice

So right now Im making my first UE5 game as a hobby in my spare times, and I need some advice

But uh, im like 10% done at a good estimate and my code starts to look very…messy?

Ofc, if I was using a script heavy game engine it would be insanely messy, UE5 is really neat compared to that but still it feels like I am doing a lot of things wrong.

Wanted to ask if everyones code look similar or if there is a better way to do these below:

I setup global variables on an actor I spawned in a world. Like:
Gold, hp, collected items, information about which actors are destroyed, if an event triggered already or not for other actors to know, statuses and difficulty settings.

I setup save game BP, where I copied half the things in global variables to be saved in.

I have 2 different buttons to save and load the game, they are starting to look very packed, like this:


Saving looks something like this:

Loading looks something like this:

I am just starting to code my game, and I can imagine when its done just saving and loading will have at least 5 to 10 times more variables I will need to save and load.

Not even counting the global variables I will have at the end, will probably be easly over 100 of them.

I can imagine I will miss adding stuff or add it wrong eventually and will be really hard to figure out what went wrong in the future. Does anyone have any advice how I can make global varibels/save/load system better so its less likely to bug out later in a project?
How similar is this situation when others code save/load/global variables systems?

One world ‘local’.

If you’re trying to do this globally, then you have to manage all variables of all objects, which isn’t easy.

Much better, to let each object manage its own variables. That’s what blueprints are for.

So if I just have a door mesh, I have to remember to save its state in the level bp, and load its state etc. But if I make a door BP, it can know to save a load its own variables, only when a change has occurred.

Also, you don’t need to save every variable, every time. Surely, you only need to save what has changed? Another good reason to keep everything local inside each blueprint :slight_smile:

1 Like

Thanks for the response, I would love to do everything local but I dont’ know how something local can tell everyone outside of its BP whats going on.

Wait, there is a function called “a variable changed at blueprint” or something ? I am new so I wouldn’t know.

But if Im keeping everything local, how do other BP’s know something is changing or what is happening? With me connecting every actor to one “global variable” actor, I can just change something on the “global variable” actor and since everyone references that actor with “get all actors of class” they all know if something changed.

Can you explain more or give an example?

Global variables are a bad thing :slight_smile:

It’s why they’re difficult to do, people are trying to stop it happening.

The door is a great example. Only the door needs to know if it was last open/closed or locked/unlocked, and so can save/read its own status. Why would something else need to know about the door status?

Which actors are destroyed is another good example. If they are blueprints, you only need to write ONE actor that knows if it was destroyed in the last run. Then pepper them all over the map, no problem. You don’t have to keep track of anything.

1 Like

I recommend going through this learning path : BeginPlay | Learning path
Mainly this chapter : Begin Play | Blueprints | Tutorial

1 Like

I have an array for basically every variable in my game. I have it as a float array. I can change anything from bullet speed to bullet size to bullet drop to how many bullets are shot at once and fire rate and damage and explosion radius and impulse force. And I do that for everything. All the character variables like acceleration etc. All my timers how long before things spawn. It’s like 170 entries.

I have a string array next to it telling me what each index is. So 170 is SniperBulletSpeed lets say. So then when the bullet spawns, it pulls the value from the array and uses that.

Even if guns are allowed. If it’s >1 then IsAllowed = true. else it’s false.

I can then iterate over the array and create a UI with every single entry. So if I add more entries, the UI will just automatically create them. That way my Host server can change any value in the game before hosting the server. It’s much easier.

If I had a mass of locations, I’d use a vector array and have a string array telling me what each index is. Like index 20 Forest. And in the vector array, Index 20 would be the location.

I have functions for “SaveValues” and “GetValues”

Not sure if that helps but it’s the only way I could handle so many values and have them easy to work with.

1 Like

Thats a very cool way to do it, I like it but I think that would still get confusing when you have 170~ values in it, how would you know if you setup one of them wrong or miss one. It would get really confusing when you make a mistake I feel like.

But that would solve my saving and loading issue, I might just use it now. Thanks a bunch!

I think I get what you mean, or not. When a character wants to check if my character made a certain choice in the past, I can either reference that choice in a “global choice storage” or
this is what you mean I think?
reference the “door” where choice action has happened.

But that would make it so I would have to reference a lot of stuff every time I want to have consequences of my previous choices.
Instead I was making a big reference bank where I get all my choice past back, but I would be getting a lot of unnecessary information too.
Is that bad? Maybe thats how its suppose to be done and I am complaining for nothing.

In any case, I like Steven’s solution, at least I can save and load way faster with it. No need to setup dozens of variables and set them to each other one by one, when I can just set the array up.

I don’t have the attention span for a 30 min slow video lol

Scalar parameters or/and Save Vars objects could work too.

1 Like

The only way this can make sense, is if you’re making some sort of decision tree algorithm.

Otherwise, the fact that you unlocked the door three weeks ago, means that when you walk up to it today, it will open.

1 Like