I’m trying to create simple multiplayer shooter and have run in to a problem. I’ve a value, CurrentLoadout, that is set on the GameMode, that i want the clients to be able to use. I thought I should set the value to the GameState in the GameMode constructor, but when I’m building the solution, I get an error that the GameState is null. It feels like I’m missing something basic here, but I’ve looked around and can’t find a solution. Any help is appreciated! I cant seem to set the correct tag but the version I’m using is Unreal 5.0.
The error I get when building is:
Exception thrown: write access violation.
GS was nullptr.
ShooterGameMode.h
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/GameModeBase.h"
#include "ShooterGameMode.generated.h"
class ULoadout;
class AShooterGameState;
UCLASS(minimalapi)
class AShooterGameMode : public AGameModeBase
{
GENERATED_BODY()
public:
AShooterGameMode();
UPROPERTY(EditDefaultsOnly)
ULoadout* CurrentLoadout;
};
ShooterGameMode.cpp
#include "ShooterGameMode.h"
#include "ShooterHUD.h"
#include "ShooterCharacter.h"
#include "UObject/ConstructorHelpers.h"
#include "ShooterGameState.h"
AShooterGameMode::AShooterGameMode()
: Super()
{
// set default pawn class to our Blueprinted character
static ConstructorHelpers::FClassFinder<APawn> PlayerPawnClassFinder(TEXT("/Game/FirstPersonCPP/Blueprints/FirstPersonCharacter"));
DefaultPawnClass = PlayerPawnClassFinder.Class;
// use our custom HUD class
HUDClass = AShooterHUD::StaticClass();
GameStateClass = AShooterGameState::StaticClass();
AShooterGameState* GS = GetGameState<AShooterGameState>();
GS->CurrentLoadout = CurrentLoadout;
}
ShooterGameState.h
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/GameStateBase.h"
#include "ShooterGameState.generated.h"
/**
*
*/
class ULoadout;
class AShooterGameMode;
UCLASS()
class SHOOTER_API AShooterGameState : public AGameStateBase
{
GENERATED_BODY()
public:
AShooterGameState();
UFUNCTION(BlueprintCallable, Category = "GameState")
ULoadout* GetCurrentLoadout();
UPROPERTY(Replicated)
ULoadout* CurrentLoadout;
};
ShooterGameState.cpp
#include "ShooterGameState.h"
#include "Loadout.h"
#include "Shooter/ShooterGameMode.h"
#include "Kismet/GameplayStatics.h"
#include "Net/UnrealNetwork.h"
AShooterGameState::AShooterGameState()
{
bReplicates = true;
};
ULoadout* AShooterGameState::GetCurrentLoadout()
{
return CurrentLoadout;
};
void AShooterGameState::GetLifetimeReplicatedProps(TArray< FLifetimeProperty >& OutLifetimeProps) const
{
Super::GetLifetimeReplicatedProps(OutLifetimeProps);
DOREPLIFETIME(AShooterGameState, CurrentLoadout);
};