Hey guys, I am having some issues with getting a game to load. I have seen many tutorials out there but none of them I have found address loading from different BPs. I have 2 widget BPs for menus. One is the main menu and the other the save menu. Of course it is senseless to have a save option in the main menu, but a load option for both menus would be ideal. I would like to be able to save variables from the character BP, such as location, spawned AI, destroyed AI actors and inventory, So, what I want to do is have the variables be saved onClick, and then be able to load them onClick. Does anyone know how to do that? I can’t imagine that nobody knows
Thanks!
It’s a multi-step process:
-
Create a BP_SaveGame class to save (and later load) the data you want. When creating this BP, click “All classes” and search for “SaveGame” to inherit from the required class
-
Inside this BP_SaveGame, add variables (float, structure, etc) you want to be saved
-
The magical nodes you are then searching for are in GameInstance. If you have not created one, UE4 always uses a default one. Create a BP_GameInstance (same thing, click “All classes” and search for GameInstance)
-
In Project Settings > Map & Nodes, assign your BP_GameInstance to the field Game Instance Class
-
Inside your BP_GameInstance, you have access to three magical functions:
- DoesSaveGameExist(Name, Index): use this to check if your system has previously saved your data (based on the filename Name provided)
- LoadGamefromSlot(Name, Index): use this to load your data using the Name key (your filename). It returns a SaveGame class that you can cast to your BP_SaveGame
- SaveGameToSlot(SaveGame, Name, Index): use this to save the data you store in your BP_SaveGame (first parameter)
Additional Note: you can access the GameInstance in your other BP using the node GetGameInstance
That is a great answer. Since we are on a public Q&A for all UE4 users, would you be so kind as to send pics of what you are instructing to do? I think it would help many others as well. Implementation pics would be awesome.
Here are some screenshots to complete the explanation:
BP_SaveGame and 3 variables to be saved/restored (PlayerName, PlayerLevel, CurrentMap):
Configure your Project to use BP_GameInstance:
LoadData function in BP_GameInstance:
SaveData function in BP_GameInstance:
LoadMyData and SaveMyData functions in Character blueprint (it could be in any BP):
Excellent! Thank you for your prompt response and the visual representations. I hope this helps anyone who comes by this thread.
How can i call the custom event LoadMyData in another Level? Lets say i have a main menu level and i want to call that custom event from there . Thank you in advance!
in you main menu in the graph on event construct cast to your game instance the object is gonna be get game instance and the return value promote to a variable and now you can call the function anywhere in the graph by dragging in the variable to the graph and call the loadmydata function
Thanks you for the fast response but that doesn’t help me. I still can’t call the custom event LoadMyData, because it is in the third person blueprint. The custom event LoadMyData in the third person blueprint is different from the LoadData function in the Game Instance blueprint. Or didn’t i understand your anwser correctly, in that case please let me know.