6. Save your wallet balance and restore it
save one actual balance and restore it in another session.
On this page
Goal: save one actual balance and restore it in another session. Before you start: the Normal wallet and widget from lesson 3. Safe and Packed are not prerequisites.
The idea you will use
Wallet lives in the current game session. A SaveGame object carries a copy of its balance into a file. Text such as 1.25K is presentation; save a Scientific Number, not that text.
Predict: start with 1000, produce 25 and buy for 100. Which value should be saved? Can you restore it if BeginPlay saves 1000 again before loading?
Create the save object
- In Blueprints create Blueprint Class → All Classes → SaveGame, named
BP_MiPartidaCartera. - Add
Balanceof type Scientific Number, enable Save Game in its advanced details and leave it accessible from other Blueprints. Compile and save. - In
BP_MiCartera, create functionsGuardarSaldoandCargarSaldo, each with a Boolean outputSuccess. Return false on every failure path. These are functions you define on your Actor, not new plugin nodes.
Save the current value
- In
GuardarSaldo, readWallet → Get Valueand checkIs Successwith a Branch. Return Success false on failure. - On true, execute Create Save Game Object, Class
BP_MiPartidaCartera. Keep its Return Value in a local variablePartidaof that type. - Drag from Get Partida to create Set Balance. Target is Partida; the value is the valid Wallet read. Execute it after creating and retaining the object.
- Then call Save Game to Slot. Save Game Object = Partida, Slot Name =
MiCarteraPractica, User Index =0. Connect its result bool to Success on the Return Node.
Until Save Game to Slot runs, you have only filled an in-memory object. Its bool reports whether Unreal wrote it successfully.
Restore the saved value
- In
CargarSaldo, check Does Save Game Exist with the same slot and index. If absent, return false without changing Wallet. - If present, execute Load Game from Slot and pass Return Value to Cast To BP_MiPartidaCartera. Return false on Cast Failed.
- Read
Balancefrom the cast object and pass it to Wallet’s Try Set Value. Execute that assignment from the successful cast path. - Return the Try Set Value bool as Success; use Out Error for diagnostics on failure. Do not initialize 1000 again after loading.
Add buttons and check another session
- In
WBP_MiCartera, addBTN_GuardarandBTN_Cargar, labelled Save and Load. - Save OnClicked calls
Cartera → GuardarSaldo. Check Success and setTXT_Estadoto “Game saved” or “Could not save”. - Load OnClicked calls
Cartera → CargarSaldo. Only on success, callActualizarSaldoand show “Game loaded”; on failure show “Could not load” while keeping the current balance. - Do not call GuardarSaldo from BeginPlay. Retain new-game initialization and save only when the button is pressed.
- Run: produce once, buy once and save 925. Stop Play, close the editor and reopen the level. You see the initial 1000 before loading; pressing Load must show 925.00, or the equivalent presentation from your formatter.
Check your reasoning
See the explanation
1000 + 25 − 100 = 925. Saving the initial balance into the same slot before loading would overwrite the value you wanted to recover. The Load button reads the object from disk, not the Actor’s initial value.
Your turn: attempt to load a different slot that you have never created. Loading should fail without changing Wallet. Restore the original name afterwards.
Ready to continue when: you have recovered a balance from another session and can distinguish load failure, initial balance and saved balance.
If your project uses other types
This first path uses a Normal Scientific Number. The saving-other-types extension covers Packed and Prepare/Restore for Safe and Context-Bound. SaveGame stores the file; this example neither encrypts nor authenticates it. Safe memory protection does not establish the authenticity of a loaded file.
Progress is saved only in this browser and carries over when you switch language.