Int32 Variable Not Incrementing Properly

I am in Unreal 4.6.1. As my basic setup, I have a variable docCount in my GameMode class. In the constructor I set it to zero (I’ve also tried setting it in BeginPlay to the same result). From my controller class, I have a function that on picking up a document, will call a function in my GameMode class to increment the docCount variable and spawn a new document.

Here’s where it gets tricky. I’m using an int32, but it’s not incrementing as expected (or consistently for that matter. I.E. I would expect a variable initialized at 0 to increment to 1. Instead it is return any range of integers - positive or negative - and only sometimes behaves as expected. There’s no way int32 should be doing this. My code is below:

BanditGameMode.h

int32 docCount;

BanditGameMode.cpp

ABanditGameMode::ABanditGameMode(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer){
	docCount = 0;
	UE_LOG(YourLog, Warning, TEXT("Document Count is %i"), docCount);
}

void ABanditGameMode::SpawnDocument(){
	UE_LOG(YourLog, Warning, TEXT("Document Count is %i"), docCount);
	docCount++; // have also tried +=1
	UE_LOG(YourLog, Warning, TEXT("Document Count is %i"), docCount);
	if (docCount < 3){
            // stuff happens
        }
}

BanditController.h

ABanditGameMode* game;

BanditController.cpp

void ABanditController::PostInitializeComponents(){
	Super::PostInitializeComponents();

	if (GetWorld()){
		game = (ABanditGameMode*)GetWorld()->GetGameInstance();
	}
}

void ABanditController::Use(){
    // stuff happens
    game->SpawnDocument();
}

I tried closing Unreal, running “Clean Solution” and then “Rebuild Solution”. This caused it to be more reliable, but it will still gives the wrong numbers 1/5 times. It even caused Unreal to crash once. Next step it to reboot my machine.

Set a UPROPERTY() macro on top of your variable like this:

UPROPERTY()
int32 docCount;

(Note: some speculation in this)
UE4 creates default objects for you classes. Your game mode instance is then copied from that default object. But only UProperties get copied. This leads to docCount not being initialized. Making docCount a UProperty fixes this.

I assume your constructor isn’t called on the instance of the class in your game.

I missed something else:

game = (ABanditGameMode*)GetWorld()->GetGameInstance();

The way you cast in UE4 is like this:

game = Cast<ABanditGameMode>(GetWorld()->GetGameInstance());

The Cast macro will check if the cast is valid. In your case it isn’t (GameMode is not a subclass of GameInstance)
Edit: If the cast is invalid it returns a null pointer.

You want to call GetWorld()->GetAuthGameMode() on the server and cast that to ABanditGameMode.

I tried that and the returned value still fails pretty consistently. Thank you for the explanation though, I wasn’t aware of that.

And no, I’m not calling the constructor in the controller class, just the get game instance function.

Here is the error message for when it occasionally crashes (note it doesn’t always happen when docCount is correct):

[2015.03.08-17.43.10:418][590]LogWorld: Game class is 'BanditGameMode'
[2015.03.08-17.43.10:418][590]YourLog:Warning: Document Count is 0
[2015.03.08-17.43.10:421][590]LogAIModule: Creating AISystem for world AI_Test_Map
[2015.03.08-17.43.10:423][590]LogWorld:Warning: *** WARNING - PATHS MAY NOT BE VALID ***
[2015.03.08-17.43.10:423][590]LogWorld: Bringing World /Game/Maps/UEDPIE_0_AI_Test_Map.AI_Test_Map up for play (max tick rate 60) at 2015.03.08-13.43.10
[2015.03.08-17.43.10:424][590]LogWorld: Bringing up level for play took: 0.002207
[2015.03.08-17.43.10:426][590]PIE: Info Play in editor start time for /Game/Maps/UEDPIE_0_AI_Test_Map -0.013
[2015.03.08-17.43.14:372][824]YourLog:Warning: Document is UsableActor_C_6
[2015.03.08-17.43.14:373][824]YourLog:Warning: Document Count is 0
[2015.03.08-17.43.14:373][824]YourLog:Warning: Document Count is 1
[2015.03.08-17.43.14:373][824]YourLog:Warning: Inside Target Point Loop
[2015.03.08-17.43.14:374][824]YourLog:Warning: Target Point is ?????4??d
[2015.03.08-17.43.14:374][824]YourLog:Warning: Inside Target Point Loop
[2015.03.08-17.43.14:374][824]YourLog:Warning: Target Point is ?????4??d
[2015.03.08-17.43.14:375][824]YourLog:Warning: Inside Target Point Loop
The thread 0x16e8 has exited with code 0 (0x0).
First-chance exception at 0x000007FEE79C228A (UE4Editor-Core.dll) in UE4Editor.exe: 0xC0000005: Access violation reading location 0x00000001122E4290.
Unhandled exception at 0x000007FEE79C228A (UE4Editor-Core.dll) in UE4Editor.exe: 0xC0000005: Access violation reading location 0x00000001122E4290.

Please provide full header file.

Hi, between one SpawnDocument and another the game level is reset? Because in this case almost every class is reinitialized… except GameInstance and some others :-/

I ran the game 10 times through (because usually an error would appear before then), and that solved the problem. I guess that’s what I get for sifting through the autocomplete of GetWorld()'s list of functions and saying “get game instance? that seems right” and then not checking the documentation.

And as for the casting method, I have to sheepishly admit that I did it correctly elsewhere in my code and then just forgot to do it here. Chalk it up to the joys of a new to me framework.

Now that I’m looking at the GetGameInstance stub, it’s not clear to me what it does.

Thank you MulleDK19 and pixelcidio for responding, 's answer was correct. I am giving you both plus one for at least reading it through.