Acccessiing GameMode from AActor constructor

hi,

A short time ago I have written the following code:

AMyActor::AMyActor(const FObjectInitializer& ObjectInitializer)
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
BaseCollisionComponent = ObjectInitializer.CreateDefaultSubobject<USphereComponent>(this, TEXT("BaseCollisionComponent"));
	RootComponent = BaseCollisionComponent;
	if (GEngine)
	{
		UWorld* World = GEngine->GetWorld();
		AMyGameMode *GameMode = (AMyGameMode*)World->GetAuthGameMode();
    }
}  

When I try to compile this, i shoes me the crash reporter:

Access violation - code c0000005 (first/second chance not available)  

It says something’s wrong with accessing the game mode.

Hope someone knows the answer.

Thanks
My question is, is there a known engine bug related to accessing game mode, or i am doing something wrong? If that’s my error, please provide the correct solution.

Thanks

Hey, the GameMode as well as World might be NULL at the time the constructor is called. Because the constructor is called during EditorLoad to create the Class Default Object (read more about it here)

So use BeginPlay() instead of the constructor for objects spawned in the world.

  1. You are accessing World pointer without null-checking. This is why Your code probably crashed
  2. Move Your code to PostInitializeComponents or BeginPlay functions. Contructor may be to early for GameMode object to exist.

Hi all,
I must also add some more important information.

  1. This constructor is ONLY called from inside the widget, which
    appears a while after everything is
    initialized. Before this the game
    mode is already being accessed from
    the other actor. The same code is
    working there, it shows no signs
    something is wrong.
  2. I really NEED to access game mode from the actor, because it contains
    some information which is essential
    to create components(It has an array
    of static meshes for static mesh
    components). So, i don’t understand
    how to move the code to BeginPlay()
    function if the object creators
    don’t work there.

You should move your static mesh info from GameMode to somewhere else then.
A good place would be an UDataAsset OR
A custom asset.

This constructor is ONLY called from
inside the widget, which appears a
while after everything is initialized

You are wrong. Constructor in UE4 is very special function. It is called multiple times and most of those times You are unaware of that. For example constructor of an UObject (and AActor is an UObject) is called every time You run editor (not the game, just when You open editor for work) so CDO object can be created.

Move this code to PostInitializeComponents function. This function is created exactly for object initialization purposes.

hi,
I have watched a tutorial 1 above and found out an interesting thing.
Judging by the usage of “GENERATED_UCLASS_BODY” and “ConstructObject<>()”, it has been made on the 4.8 version or less.
But in 4.8(correct me if I’m wrong) some macros and functions have been changed. ConstructObject<>(), for example, became NewObject<>().
After i changed everything necessary, it didn’t work any more. Asset was still visible in content browser, but I could’t create an instance of it, though it didn’t threw any error message.
So, could somebody kindly make a new tutorial on version 4.11?

Yes, this is true, some macros and function names changed. To create a new UObject use NewObject<>().

If you uses UDataAsset however, you don’t need to create a new instance, you can just reference the asset that exists int he content browser.