We've done quests, we've done NPCs, so it's time to learn how we can save and load both!
First of all, let's create a new save game.
We'll start with saving and loading the quest
component.
So, create an event called "Save Quest Component"
with these inputs: an array of Quest Structs,
an integer that will represent the Current
Quest ID and a boolean for Can Change Quest.
Then promote all of them to variables and
call from self "Save Game to Slot" with the
slot name "QuestSave".
To load, add a new event called "Load Quest
Component" with a Quest Component input.
This will iterate over the array of quests
we just created and using the quest component
input, we will call Add Quest and supply that
with data from the for each loop.
Then, get from Quests array the element with
the index equal to the Current Quest ID and
From "Completed" call "Select Quest" with
the returned name and set Can Change Quest
to the boolean from the save function.
Now let's do the same for the NPCs, but first
we need to go to the NPC Quest class and add
a new string variable called Name that should
have instance editable turned on.
In the save game, add an event called "Save
NPC" with a string and NPC Quest Holder as inputs.
Now add them to a map between strings and NPC
Quest Holders so we can easily access the
quests when loading and then we'll call from
self "Save Game to Slot" using the same slot name.
To load NPCs, we'll use an event called "Load
NPC Quests" with an NPC Quest input.
From that, get its name and check if we can
find it in the map we created.
If that returns true, set its Quests variable
to the first return value of the Find node.
Next step is to create a Game Instance that
we will use to create the save instance as
this class is persistent over levels.
So, on Event Init, check if the save game
exists using the same slot name.
If it exists, load it, cast to our save game
class and promote it to a variable.
If it doesn't exist, create it, promote it
to a variable, then add a delay of 0 seconds
to wait for the third person character to
be created, then cast to it to get its quest
component from which we'll get the quests
and call "Save Quest Component" using default data.
From there get all NPC_Quest actors and call
"Save NPC" using their name and quests.
What's left is to save and load the data into
their actual classes.
Let's start with the quest component.
On BeginPlay, right after it
get a sequence.
From Then 1 cast to our custom game instance
and get the save game and promote it to a
variable.
Then after 2 delays of 0 seconds whose purposes
are to wait for the UI to be created, specifically
the quest list, call "Load Quest Component"
and supply the input with self.
On Event EndPlay, call "Update Cache" and
then using the save game variable call "Save
Quest Component" and plug in the Quests, Current
Quest ID and Can Change Quest variables.
In the NPC_Quest class, right after the Parent's
BeginPlay, cast to our game instance and promote
the save game to a variable.
Then on the Completed branch of the first
For Each Loop call "Load NPC Quests" with
self as NPC.
Lastly on Event EndPlay, call "Save NPC",
again, plugging in the name and quests variables.
Finally, make sure to set the newly created
game instance as the active game instance
in the project settings.
If you enjoyed this final episode, then consider
hitting that subscribe button to stay up to
date with other series and tutorials!
The files for this project are available for
free over on GitHub, so make sure to check
out the link in the description!
This has been a first for me and I got to
learn a lot about series so expect more in
the future and make sure to comment what series
you would like to see!
I want to thank everyone over on Patreon for
their support and especially to: Aviram Ifm,
Bader AlQahtani, Giovanni Pena, Golden Glow
Master, Leonardo Pereira and Realitätsverlust.
Now make sure to go and watch this video to
learn about other RPG mechanics such as a skill tree!
And as always, thanks for watching!
