Unreal Engine 4 Save Game C++
Save game and load Game
How To:
Using SaveGame Object.
Set-up saving and load game object use SaveGame object;
SaveGame Object
- Box with different compartments.
- You set-up each compartments to store a particular information by creating variables;
- Ex : You need store score so you need transfer information into SaveGame object from other classes.
- First you need to instance or copy of your SaveGame object.
// How to i create this look down!!
// SaveGameObject.cpp
#include "SaveGameObject_Cover.h"
USaveGameObject_Cover::USaveGameObject_Cover() {
SaveSlotName = TEXT("TestSaveSlot");
UserIndex = 0;
}
// SaveGameObject.h
public:
USaveGameObject_Cover();
UPROPERTY(VisibleAnywhere, Category = "Basics")
float ScoreSave;
UPROPERTY(VisibleAnywhere, Category = "Basics")
FString SaveSlotName;
UPROPERTY(VisibleAnywhere, Category = "Basics")
uint32 UserIndex;
};
Now You can see we created SaveGameObject.h and SaveGameObject.cpp classes to save or load our game and More. We need to save ScoreSave information into in our game.
Now get Score from class (what class you want to get score from). and store them in the matching variables in the SaveGame Object.
SaveGame object store or hold variables values until it is saved to the hard drive.
Save SaveGame Object to a file on your hard drive by using SaveGameToSlot.
SaveGameToSlot : Saving document or new game in other programs since you give SaveGameToSlot a file name;
Location : Save/SaveGames/slotName.sav;
LoadGameFromSlot : Loading our saving game values from hard drive; From SaveGameToSlot file; LoadGameFromSlot creates a new copy of the SaveGame object;
And we use this copy information into our game. In this Example we load and save player Score.
Let's do it!!
Create a SaveGame Object
This SaveGame class sets up an object that can be use for saving and loading functions declared in Kismet/GameplayStatics.h;
New C++ class >> Check Box Show All Classes >> Search SaveGame >> Next >> Give your SaveGame class to a name >> Finish
Header File
In header file you declare any type of Variables you want to save a game;
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/SaveGame.h"
#include "SaveGameObject_Cover.generated.h"
/**
*
*/
UCLASS()
class COVERTRACK_API USaveGameObject_Cover : public USaveGame
{
GENERATED_BODY()
public:
USaveGameObject_Cover();
UPROPERTY(VisibleAnywhere, Category = "Basics")
float ScoreSave;
UPROPERTY(VisibleAnywhere, Category = "Basics")
FString SaveSlotName;
UPROPERTY(VisibleAnywhere, Category = "Basics")
uint32 UserIndex;
};
In this example, there are also variables declared that will be used to store default values for the SaveSlotName
and the UserIndex
, so that each class that saves to thisSaveGame
object will not have to independently set those variables. This step is optional, and will cause there to be one save slot that gets overwritten if the default values are not changed.
// From Unreal Engine Doc's
Source
#include "SaveGameObject_Cover.h"
USaveGameObject_Cover::USaveGameObject_Cover() {
SaveSlotName = TEXT("TestSaveSlot");
UserIndex = 0;
}
Saving a Variable ScroeSave
You need to create a instance of SaveGame Object;
#include "Kismet/GameplayStatics.h"
Class Name : ES_SpawanEnemy.cpp // points variable is define in this class
Where is i define this code? where and what you want to save or store you values if you want to store Players dead's or Name you need to define in your Player class. But in this case we store Score values from Enemy class If player is Hit points will increases so we define here. In Player class yes we can do it from Player class because points scores all values be stored in getScore(float getScore) function in Player Class.
USaveGameObject_Cover* SaveGameInst = Cast<USaveGameObject_Cover>(UGameplayStatics::CreateSaveGameObject(USaveGameObject_Cover::StaticClass()));
SaveGameInst->ScoreSave = points; // Points Store score points variable;
UGameplayStatics::SaveGameToSlot(SaveGameInst, SaveGameInst->SaveSlotName, SaveGameInst->UserIndex);
Loading Stored Variable
Load A Variable : UGameplayStatics::LoadGameFromSlot
Once the load variable from hard drive the variable values can be read from it and assigned to the necessary Actors or classes.
// Called when the game starts or when spawned
void AMeraPlayerAgain::BeginPlay()
{
Super::BeginPlay();
Mass = PlayerCover->GetMass();
// Load Save Score;
USaveGameObject_Cover* LoadGameInst = Cast<USaveGameObject_Cover>(UGameplayStatics::CreateSaveGameObject(USaveGameObject_Cover::StaticClass()));
LoadGameInst = Cast<USaveGameObject_Cover>(UGameplayStatics::LoadGameFromSlot(LoadGameInst->SaveSlotName, LoadGameInst->UserIndex));
Score = LoadGameInst->ScoreSave;
}
I want to Load it in My Player class so i define it here. Because you can see ScoreSave variable store in Score variable. Now we Use it Display Score in User-Interface;
Comments
Post a Comment