GameInstance::Init() is being called when running the program in the Unreal Editor, but not in the standalone program or packaged project

I have created a custom Game Instance that spawns a Ball class I made at the start of the game. This works completely fine in the Unreal Editor; however, when I run it as the standalone program, my Ball object doesn’t spawn. I tried adding a console log output to the start of the Init() function, and it’s never outputted. I also even put a 5 second sleep delay in the Init() function and that never happens. This is leading me to believe that MyGameInstance.Init() is never called in the packaged project, however it is called when hitting play in the unreal editor. I’m not sure why this is the case. I do have MyGameInstance set as the Game Instance in the UE4 project settings and in DefaultEngiine.ini. Any help? Thanks. Also, everything I’m doing is in C++, not blueprintst.

MyGameInstance.h

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "Engine/GameInstance.h"
#include "MyGameInstance.generated.h"

/**
 * 
 */
UCLASS()
class MYPROJECT_API UMyGameInstance : public UGameInstance
{
	GENERATED_BODY()
	
public:
	virtual void Init() override;    	
};    

MyGameInstance.cpp
// Fill out your copyright notice in the Description page of Project Settings.

#include "MyProject.h"
#include "MyGameInstance.h"
#include "Ball.h"

void UMyGameInstance::Init() {
	UE_LOG(LogTemp, Warning, TEXT("Made it to Init"));
	UGameInstance::Init();
	const FVector* MyLocation = new FVector(500.0, 0.0, 0.0);
	ABall* SpawnedActor1 = (ABall*)GetWorld()->SpawnActor(ABall::StaticClass(), MyLocation);
}

DefaultEngine.ini
[URL]

[/Script/HardwareTargeting.HardwareTargetingSettings]
TargetedHardwareClass=Desktop
AppliedTargetedHardwareClass=Desktop
DefaultGraphicsPerformance=Maximum
AppliedDefaultGraphicsPerformance=Maximum

[/Script/EngineSettings.GameMapsSettings]
GameInstanceClass=/Script/MyProject.MyGameInstance
EditorStartupMap=/Game/NewMap.NewMap
GameDefaultMap=/Game/NewMap.NewMap
1 Like

Have you had any luck figuring it out? I’m stuck in the same boat.

1 Like

We had to do a weird work around. I don’t 100% remember how it worked, but we had to create a dummy actor class that we placed in our world through the UE4 level editor. In that dummy actor’s constructor we called the GameInstance function.

Game instance can’t spawn actors since there’s no world present. It “works” in editor probably because the init order of objects is different. But you shouldn’t do it to begin with. Only actors in the world (GM,PC,Actor etc.) should spawn things.

ABall* SpawnedActor1 = (ABall*)GetWorld()->SpawnActor(ABall::StaticClass(), MyLocation);

Is circular logic. The ball does not exists so it has no world context, and without it, you can’t spawn it. :slight_smile: