How do I reset/initialize a static variable between PiE/SiE sessions?

Hi,

I spawn “Balloons” in my map. Balloons belongs to a class that keeps a count of how many of them are in the level through a static variable:

in the header file:

class BALLOONGAME_API ABalloon : public AActor
{
  ABalloon();
 ~ABalloon();

 static int ballooncount;
}

in the source file:

    #include "Balloon.h"
    
     ABalloon::ballooncount = 0;
    
     ABalloon::ABalloon() : Super()
     {
       ABalloon::ballooncount++;
     }
    
     ABalloon::~ABalloon() 
     {
       ABalloon::ballooncount--;
       this->Destroy();
     }

Everything works fine the first time I launch my level, either through “Play In Editor” or “Simulate in Editor”. But if I stop and then try to either PiE or SiE again, my static variable ballooncount doesn’t get initialized back to zero. How can I make sure that happens, and that it happens at the right time? (ie. before spawning the balloon, before their count begins)

Thanks!
f

Aaaand I found a solution.

Looks like there’s a function that gets called before anything else happens; that’s InitGame (AGameMode::InitGame | Unreal Engine Documentation) from your Game Mode.

Problem solved by overriding that function and resetting there my static variable:

void AMyGameMode::InitGame(const FString& MapName, const FString& Options, FString& ErrorMessage)
{
	Super::InitGame(MapName, Options, ErrorMessage);
	ABalloon::ballooncount = 0;
}