Every time I call SaveGame() game crashes. Any idea why?
MouseGameInstance.h
#pragma once
#include "CoreMinimal.h"
#include "Engine/GameInstance.h"
#include "MouseGameInstance.generated.h"
UCLASS()
class AMAZINGMOUSERUNNER_API UMouseGameInstance : public UGameInstance
{
GENERATED_BODY()
public:
UMouseGameInstance();
class UMouseSaveGame* MouseSaveGame;
FString SaveSlot = "Save1";
UFUNCTION(BlueprintCallable)
void LoadGame();
UFUNCTION(BlueprintCallable)
void SaveGame();
int Coins= 0;
int Shields = 0;
int FenceType = 0;
bool FenceLockState[3] = {true, false, false};
UFUNCTION(BlueprintPure)
int GetCoins() { return Coins; }
UFUNCTION(BlueprintPure)
int GetFenceType() { return FenceType; }
UFUNCTION(BlueprintCallable)
void SetFenceType(int NewType) { FenceType = NewType; }
void AddCoin() { Coins++; }
};
MouseGameInstance.cpp
#include "MouseGameInstance.h"
#include "MouseSaveGame.h"
#include "Kismet/GameplayStatics.h"
UMouseGameInstance::UMouseGameInstance()
{
MouseSaveGame = Cast<UMouseSaveGame>(UGameplayStatics::LoadGameFromSlot(SaveSlot, 0));
UE_LOG(LogTemp, Error, TEXT("Working..."));
if(MouseSaveGame == nullptr)
{
MouseSaveGame = Cast<UMouseSaveGame>(UGameplayStatics::CreateSaveGameObject(UMouseSaveGame::StaticClass()));
MouseSaveGame->Coins = 0;
MouseSaveGame->FenceType = 0;
UGameplayStatics::SaveGameToSlot(MouseSaveGame, SaveSlot, 0);
}
LoadGame();
}
void UMouseGameInstance::LoadGame()
{
Coins = MouseSaveGame->Coins;
FenceType = MouseSaveGame->FenceType;
}
void UMouseGameInstance::SaveGame()
{
if(MouseSaveGame)
{
MouseSaveGame->Coins = Coins;
MouseSaveGame->FenceType = FenceType;
UGameplayStatics::SaveGameToSlot(MouseSaveGame, SaveSlot, 0);
}
}
Crash log
Unhandled Exception: EXCEPTION_ACCESS_VIOLATION reading address 0xffffffffffffffff
UnrealEditor_Engine
UnrealEditor_Engine
UnrealEditor_AmazingMouseRunner_0033!UMouseGameInstance::SaveGame() [D:\UE5\nauka\AmazingMouseRunner\Source\AmazingMouseRunner\MouseGameInstance.cpp:43]
UnrealEditor_AmazingMouseRunner_0033!AMouseController::Death() [D:\UE5\nauka\AmazingMouseRunner\Source\AmazingMouseRunner\MouseController.cpp:37]
UnrealEditor_AmazingMouseRunner_0033!AMouseCharacter::Death() [D:\UE5\nauka\AmazingMouseRunner\Source\AmazingMouseRunner\MouseCharacter.cpp:205]
UnrealEditor_AmazingMouseRunner_0033!ABasePlatform::DeathOverlap() [D:\UE5\nauka\AmazingMouseRunner\Source\AmazingMouseRunner\BasePlatform.cpp:114]
UnrealEditor_AmazingMouseRunner_0033!ABasePlatform::execDeathOverlap() [D:\UE5\nauka\AmazingMouseRunner\Intermediate\Build\Win64\UnrealEditor\Inc\AmazingMouseRunner\UHT\BasePlatform.gen.cpp:37]
UnrealEditor_CoreUObject
UnrealEditor_CoreUObject
UnrealEditor_Engine
UnrealEditor_Engine
UnrealEditor_Engine
UnrealEditor_Engine
UnrealEditor_Engine
UnrealEditor_Engine
UnrealEditor_Engine
UnrealEditor_Engine
UnrealEditor_Engine
UnrealEditor_AmazingMouseRunner_0033!AMouseCharacter::Diving() [D:\UE5\nauka\AmazingMouseRunner\Source\AmazingMouseRunner\MouseCharacter.cpp:113]
UnrealEditor_AmazingMouseRunner_0033!AMouseCharacter::Tick() [D:\UE5\nauka\AmazingMouseRunner\Source\AmazingMouseRunner\MouseCharacter.cpp:49]
UnrealEditor_Engine
UnrealEditor_Engine
UnrealEditor_Engine
UnrealEditor_Engine
UnrealEditor_Core
UnrealEditor_Core
UnrealEditor_Core
UnrealEditor_Engine
UnrealEditor_Engine
UnrealEditor_Engine
UnrealEditor_Engine
UnrealEditor_UnrealEd
UnrealEditor_UnrealEd
UnrealEditor
UnrealEditor
UnrealEditor
UnrealEditor
UnrealEditor
UnrealEditor
kernel32
ntdll