Recompile, hot reload executes constructor of AGameMode

Because constructors in UObjects work differently, in order to make editable and readable default varables to work and to create new objects a little faster, UE4 creates class default objects (CBO) of each UObject class, which is master copy of object and UE4 spawn new object instince just by copying CBO to other place in memory. Because of that constructors are called only when CBO is created to load default variables and because of that constructors are suitable for any code execution. So the reason why constructor is called on hot reaload is because on hot reload CBO are recreated. Instead of constructors you should use initiation events like PostInitializeComponents or BeginPlay.

Hello !

I have a C++ project where int the AGameMode constructor I start a TCP server with Poco. The TCP server is shut down in the AGameMode destructor.
The problem is that when I do development and change the code in VS and then push the ‘Recompile’ button in the Editor, the new code compiles but somehow the AGameMode constructor is called twice even without starting the game !!! While the destructor is called only once! As a result there will be a stale TCP server hanging around and occupying the specific port which results that when I push the ‘Play’ button and would really like to start the game I cannot instantiate a TCP server on that port. My questions then,

Why the constructors are called when pushing Recompile ?
What should be the way around this problem ?
Is the AGameMode constructor/destuctor is the best place to start and shut down a TCP server ?

Thanks in advance for any help,

Thanks a lot for the explanation, it is now much more clear ! OK, I will have a look at BeginPlay() and co…
Cheers,
G

Thanks a lot for the explanation, it is now much more clear ! OK, I will have a look at BeginPlay() and co…
Cheers,
G