I tried searching and found Violation Error on Add to Tarray - Programming & Scripting - Epic Developer Community Forums but this seems to not apply here? I am getting super confused here.
So I have “ScoreStructItem.h”
include “CoreMinimal.h”
include “ScoreStructItem.generated.h”
USTRUCT(BlueprintType)
struct FScoreStructItem
{
GENERATED_USTRUCT_BODY()
UPROPERTY(VisibleAnywhere, BlueprintReadWrite)
int32 Score;
UPROPERTY(VisibleAnywhere, BlueprintReadWrite)
FString DateTime;
FScoreStructItem() {};
};
And I have a class extending “AGameModeBase”:
UPROPERTY(Category = Gameplay, VisibleAnywhere, BlueprintReadOnly)
USaveScore* ScoreInstance;
And finally I have “SaveScore.h” which has
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Basic)
TArray<FScoreStructItem> RecentScores;
void AddScore(int Score);
In my “SaveScore.cc” I have
void USaveScore::AddScore(int Score)
{
FScoreStructItem ScoreStruct;
FDateTime Today = FDateTime::Today();
ScoreStruct.DateTime = FString::Printf(
TEXT("%d-%d-%d"),
Today.GetYear(),
Today.GetMonth(),
Today.GetDay());
ScoreStruct.Score = Score;
RecentScores.Add(ScoreStruct);
}
But whenever I call “AddScore(10)”, it crashes with the following message.
Access violation - code c0000005 (first/second chance not available)
Which points to my “RecentScores.Add(ScoreStruct);” as the cause.
I am very confused because I don’t even have pointers anywhere and as far as I understand, structs I can just pass by value in Unreal and it will create a copy for me that doesn’t go out of scope?
Any help I will be super grateful, thanks!
Edit
Here is the full stack
Access violation - code c0000005
(first/second chance not available)UE4Editor_HigherPlaneSnake_5085!USaveScore::AddScore()
[c:\users\documents\unreal
projects\higherplanesnake\source\higherplanesnake\savescore.cpp:26]
UE4Editor_HigherPlaneSnake_5085!AHigherPlaneSnakeGameMode::StoreScore()
[c:\users\documents\unreal
projects\higherplanesnake\source\higherplanesnake\higherplanesnakegamemode.cpp:34]
UE4Editor_HigherPlaneSnake_5085!AHigherPlaneSnakePawn::GameOver()
[c:\users\documents\unreal
projects\higherplanesnake\source\higherplanesnake\higherplanesnakepawn.cpp:269]
UE4Editor_HigherPlaneSnake_5085!ASnakeSegment::OnOverlapBegin()
[c:\users\documents\unreal
projects\higherplanesnake\source\higherplanesnake\snakesegment.cpp:31]
UE4Editor_HigherPlaneSnake_5085!ASnakeSegment::execOnOverlapBegin()
[c:\users\documents\unreal
projects\higherplanesnake\source\higherplanesnake\snakesegment.h:13]
I don’t think it’s ScoreInstance is null because this is the code I have and check where the log doesn’t crash the engine:
ScoreInstance = LoadScore();
if (ScoreInstance == NULL){
ScoreInstance = Cast<USaveScore>(UGameplayStatics::CreateSaveGameObject(USaveScore::StaticClass()));
}
UE_LOG(LogTemp, Warning, TEXT("%s"), *ScoreInstance->SaveSlot);
Where LoadScore is
USaveScore * AHigherPlaneSnakeGameMode::LoadScore()
{
return Cast<USaveScore>(UGameplayStatics::LoadGameFromSlot("DefaultSave", 0));
}