Creating Save/Load functionality in Unreal Engine 4 is really easy. I want to test out weapon upgrades and without save / load I can’t.
<strong>This Tutorial has been created using Unreal Engine 4.8.3</strong>
Make sure you are working on the same version of the engine.
Every time when l am starting working on save/load I’m opening this Epic documentation page. It’s really straightforward.
Creating Save Game Object
Create new blueprint extending from SaveGame. Name it Save_Weapons. We need to save variables which can be upgraded so add those to Save_Weapons:
- S_MaxAmmoInMag (int),
- S_MaxAmmoInBackpack (int),
- S_ReloadTime (float),
- S_SpreadMin (float),
- S_SpreadMax (float),
- S_SpreadDecreaseTime (float),
- S_MinWeaponDamageModifier (float),
- S_MaxWeaponDamageModifier (float),
- S_CritDamageModifier (float),
- S_BurstCount (int),
- S_RiflesFireRate (float),
- S_isUnlocked (bool),
Save the file. Basically it contains all variables needed for upgrades.
UPDATING UMG / BaseWeapon
USEFUL TIP: There is one rule when working on UI. UI is about showing data. It shouldn’t make changes to other classes variables. It shouldn’t have any mechanics. UI should be used only for drawing things.
I specially broke this rule in last tutorial. Upgrading AmmoInMag and AmmoInBackpack was calculated in UMG. I will start fixing this.
Open BP_BaseWeapon and go to SetCurrentAmmoInMag function:
- Change its name to UpgradeAmmoInMag,
- Delete input,
- Copy calculations from UMG to this function,
After compiling it should automatically be replaced in UMG. Make sure to check this out.
Do the same with SetCurrentAmmoInBackpack. Change name to UpgradeAmmoInBackpack and do the same thing.
Now I’m sure UMG isn’t doing any game functionalities.
There is one bug in UMG in previous tutorial. Functions Upd_AmmoInMag and Upd_AmmoInBackpack are using CurrentAmmo* and not MaxAmmo* I needed to replace it.
You are ready to go forward now.
Save / Load Functions
Open BP_BaseWeapon and create SaveWeaponData function with one local variable extending from Save_Weapons named SaveInstance.
This is just reuse of what Epic documentation page says. I’m using weapon name as Slot Name – thanks to this I have only one function used by all weapons. Be sure your weapons doesn’t have the same name! (btw. what you could do you can add possibility to change weapon name in UMG)
Create another function LoadWeaponData with one local variable extending from Save_Weapons named SaveInstance.
Calling Save / Load
In BP_BaseWeapon Begin Play add LoadWeaponData function.
Now in every Upgrade* function add SaveWeaponData in the end. Those functions are:
As example here’s UpgradeSpread function:
And you are done!
There is one problem in UI_Weapons actor which is spawning all weapons and managing them. UI is spawned BEFORE spawning all weapons so you need to update UI (by clicking next weapon) to see loaded data.
To fix this open UI_Weapons and go to Begin Play. Iterate thru all weapons and make sure they have loaded their data. Then let UI know to update.
And now you are done.
USEFUL TIP: In your project folders there is SavedGames folder (UnrealProjectsShooterTutorialSavedSaveGames) which is storing all saves. To test if save is working you can delete those files to get back to point where you have clear game.
Implementing game is taking time but writing about it is taking much more effort!