Game Instance
Introduction
A base game instance with support for saving and loading game data using multiple save slots
Dependencies
The USKGameInstance
relies on other components of this plugin to work:
- Logger: Used to log useful information to help you debug any issues you might experience
Using the Game Instance
You need to create a blueprint using the USKGameInstance_Implementation
as a parent before using the game instance. The input indicators feature is already configured if you use this base class. If you prefer to set this up manually, you can use USKGameInstance
instead. After creating your own game instance blueprint, set this as the default game instance:
- Open the Project Settings
- Go to Project > Maps & Modes
- Change the
Game Instance Class
value to your own blueprint
Save Data
You need to create a USK Save Game
object before you can save/load data. This object contains all the data that you want to save. Just add the data you want to save as variables to the blueprint. The Game Instance
will handle the rest. You also need to set the following properties before you can save/load data:
- Save Game Class: A reference to the
USK Save Game
class that contains the data you want to save
NB: You are required to set the save slot before you can save/load data. If not, you will get a nullptr
and might cause your game to crash
Input Indicators
The Game Instance will automatically detect input events and update the current input device if needed. If the input device is changed, other classes will be notified through the OnInputDeviceUpdated
event
API Reference
Properties
Property | Description | Type | Default Value |
---|---|---|---|
LogConfigEditor | The log configuration used when running the game in the editor | ULogConfig* | nullptr |
LogConfigDebug | The log configuration used by debug builds | ULogConfig* | nullptr |
LogConfigDevelopment | The log configuration used by development builds | ULogConfig* | nullptr |
LogConfigShipping | The log configuration used by shipping builds | ULogConfig* | nullptr |
SaveGameClass | The class that holds the data that should be saved/loaded | TSubclassOf<UUSKSaveGame> | |
SettingsConfig | The configuration for the settings | USettingsConfig* | nullptr |
IsInputIndicatorsEnabled | Is the input indicators feature enabled? | bool | true |
InputMappingContext | The input mapping context used to extract the keys based on specific input actions | UInputMappingContext* | nullptr |
KeyboardMouseInputMappings | A map of all keyboard/mouse keys and the texture displayed in the indicator | TMap<FKey, UTexture2D*> | |
GenericControllerInputMappings | A map of all generic controller keys and the texture displayed in the indicator | TMap<FKey, UTexture2D*> | |
MxControllerInputMappings | A map of all Console MX controller keys and the texture displayed in the indicator | TMap<FKey, UTexture2D*> | |
SpControllerInputMappings | A map of all Console SP controller keys and the texture displayed in the indicator | TMap<FKey, UTexture2D*> | |
NsControllerInputMappings | A map of all Console NS controller keys and the texture displayed in the indicator | TMap<FKey, UTexture2D*> | |
MessagePopupWidgetClass | The widget class used to display message popups | TSubclassOf<UMessagePopupWidget> | |
FpsCounterWidgetClass | The widget class used to display the FPS counter | TSubclassOf<UFpsCounter> |
Events
Name | Description | Params |
---|---|---|
OnDataLoadedEvent | Event used to notify other classes when the save data is loaded | |
OnInputDeviceUpdated | Event used to notify other classes when the current input device is updated | |
OnGamePaused | Event used to notify other classes when the game is paused | |
OnGameUnpaused | Event used to notify other classes when the game is unpaused | |
OnDifficultyUpdated | Event used to notify other classes when the difficulty is updated | Difficulty (int) The new difficulty value |
Functions
Name | Description | Params | Return |
---|---|---|---|
GetSaveData | Get the save data that is currently loaded | UUSKSaveGame* A reference to the current save data |
|
SaveData | Save the modified data currently in memory | ||
SetCurrentSaveSlot | Set the current save slot | Index (int) The index of the save slot |
|
IsSaveSlotUsed | Check if a save slot is used | Index (int) The index of the save slot to check |
bool A boolean value indicating if the save slot is used |
EnableInputIndicators | Enable the input indicators feature | ||
DisableInputIndicators | Disable the input indicators feature | ||
GetInputIndicatorIcon | Get the input indicator icon for a specific action | InputAction (UInputAction*) The input action Amount (int) The amount of icons to retrieve |
TArray<UTexture2D*> An array of input indicator icons for the specified action |
GetInputIndicatorIconForKey | Get the input indicator icon for a specific key | Key (FKey) The key used to retrieve the input indicator icon InputDevice (EInputDevice) The input device used to retrieve the input indicator icon |
UTexture2D* The input indicator icon for the specified key |
GetKeyForInputAction | Get the key used by a specific input action | Context (UInputMappingContext*) The input mapping context InputAction (UInputAction*) The input action MappableName (FName) The player mappable name for the action |
FKey The key used by the specified input action |
UpdateKeyBindings | Update the key bindings that was changed by the player | ||
PauseGame | Pause the game | ||
UnpauseGame | Unpause the game | ||
UpdateDifficulty | Update the difficulty | Difficulty (int) The new difficulty |
|
GetDifficulty | Get the current difficulty | int The current difficulty |
|
ShowMessagePopup | Show a message popup | Data (FMessagePopupData) The data displayed in the message popup |
UMessagePopupWidget* A reference to the message popup |
HideMessagePopup | Hide the message popup |
Blueprint Usage
You can use the USKGameInstance
using Blueprints by adding one of the following nodes:
- Ultimate Starter Kit > Save Data > Get Save Data
- Ultimate Starter Kit > Save Data > Save Data
- Ultimate Starter Kit > Save Data > Set Current Save Slot
- Ultimate Starter Kit > Save Data > Is Save Slot Used
- Ultimate Starter Kit > Input > Enable Input Indicators
- Ultimate Starter Kit > Input > Disable Input Indicators
- Ultimate Starter Kit > Input > Get Input Indicator Icon
- Ultimate Starter Kit > Input > Get Input Indicator Icon For Key
- Ultimate Starter Kit > Input > Get Key For Input Action
- Ultimate Starter Kit > Input > Update Key Bindings
- Ultimate Starter Kit > Pause > Pause Game
- Ultimate Starter Kit > Pause > Unpause Game
- Ultimate Starter Kit > Difficulty > Update Difficulty
- Ultimate Starter Kit > Difficulty > Get Difficulty
- Ultimate Starter Kit > UI > Show Message Popup
- Ultimate Starter Kit > UI > Hide Message Popup
C++ Usage
Before you can use the plugin, you first need to enable the plugin in your Build.cs
file:
PublicDependencyModuleNames.Add("USK");
The USKGameInstance
can now be used in any of your C++ files:
#include "USK/Core/USKGameInstance.h"
void ATestActor::Test()
{
// USKGameInstance is a pointer to the UUSKGameInstance
UUSKSaveGame* SaveData = USKGameInstance->GetSaveData();
USKGameInstance->SaveData();
USKGameInstance->SetCurrentSaveSlot(Index);
bool IsSaveSlotUsedValue = USKGameInstance->IsSaveSlotUsed(Index);
USKGameInstance->EnableInputIndicators();
USKGameInstance->DisableInputIndicators();
TArray<UTexture2D*> InputIndicatorIcon = USKGameInstance->GetInputIndicatorIcon(InputAction, Amount);
UTexture2D* InputIndicatorIconForKey = USKGameInstance->GetInputIndicatorIconForKey(Key, InputDevice);
FKey KeyForInputAction = USKGameInstance->GetKeyForInputAction(Context, InputAction, MappableName);
USKGameInstance->UpdateKeyBindings();
USKGameInstance->PauseGame();
USKGameInstance->UnpauseGame();
USKGameInstance->UpdateDifficulty(Difficulty);
int Difficulty = USKGameInstance->GetDifficulty();
UMessagePopupWidget* ShowMessagePopupValue = USKGameInstance->ShowMessagePopup(Data);
USKGameInstance->HideMessagePopup();
}