Hello,
I’m setting up the game instance class in order to manage room generation.
I have two objects, URoom and UBiome that are UObject blueprint implementable that define the logic for generating the world.
I’m setting up two pointers on my GameInstance class and I have a function InitializingBiome that generate them by calling NewObject.
When I launch the first time I launch the editor everything is fine. When I play it a second time, my two objects are fine on the initialization (InitializeBiome() and GenerateRoom() functions) but when I call the function GetEnemiesToSpawn() the object become invalid. Like if the objects was garbage collected but I didn’t missed UPROPERTY. I tried using TSoftObjectPtr with no success.
My class is setup like this :
DungeonGameInstance.h
UCLASS()
class CUBEFACTORYGAME_API UDungeonGameInstance : public UGameInstance
{
GENERATED_BODY()
public:
UDungeonGameInstance();
virtual void Init() override;
public:
UFUNCTION(BlueprintCallable)
void InitializeBiome();
UFUNCTION(BlueprintCallable)
void GetEnemiesToSpawn(TArray<UEnemyData*> &OutEnemies);
UFUNCTION(BlueprintCallable)
void GetSlots(TArray<USlotData*> &Slots);
UFUNCTION(BlueprintCallable)
void GetChoicesForNextRoom(TArray<FRoomInfo> &OutChoices);
UFUNCTION(BlueprintCallable)
URoom *GetCurrentRoom();
UFUNCTION(BlueprintCallable)
void GoToNextRoom(int Choice);
UFUNCTION(BlueprintCallable)
void GenerateEndBattle();
protected:
UFUNCTION(BlueprintCallable)
void GenerateRoom(const FRoomInfo &Info);
UPROPERTY(VisibleAnywhere)
URoom *CurrentRoom;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly)
UBiome *CurrentBiome;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly)
TArray<FRoomInfo> NextChoices;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly)
int CurrentRoomId;
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly)
TArray<TSubclassOf<UBiome>> Biomes;
UPROPERTY(VisibleAnywhere)
int CurrentBiomeId;
};
DungeonGameInstance.cpp
void UDungeonGameInstance::InitializeBiome()
{
if (Biomes.Num() > CurrentBiomeId)
{
CurrentBiome = NewObject<UBiome>(this, Biomes[CurrentBiomeId].Get());
CurrentBiome->GenerateRoomPattern();
// CurrentBiome is valid here
TArray<FRoomInfo> Infos = CurrentBiome->GetRoomPatternFromId(0);
if (Infos.Num() > CurrentRoomId)
{
GenerateRoom(Infos[CurrentRoomId]);
}
}
}
void UDungeonGameInstance::GenerateRoom(const FRoomInfo& Info)
{
CurrentRoom = NewObject<URoom>(this, Info.RoomClass.Get());
CurrentRoom->SetType(Info.Type);
CurrentRoom->GenerateRoomAssets();
// CurrentRoom is valid here.
}
void UDungeonGameInstance::GetEnemiesToSpawn(TArray<UEnemyData*> &OutEnemies)
{
if (CurrentRoom == nullptr) // At the second time, CurrentRoom is not null but is an invalid pointer
{
return;
}
if (!CurrentRoom->IsEnemyGenerated())
{
CurrentRoom->GenerateEnemyPool();
}
if (!CurrentRoom->Enemies.IsEmpty())
{
OutEnemies.Append(CurrentRoom->Enemies); // Crash here on the append.
}
}
The debugger on CurrentRoom inside the function GetEnemiesToSpawn():

Maybe I’m missing something about the gameinstance? I search around internet and the documentation but found nothing explaining my problem.
Thanks in advance if you can help me on that !