First of all, your English is perfectly understandable
Here are the times I’ve found in which the constructor is called (I’m assuming we are talking about an AActor or UActorComponent subclass):
- When the engine is starting up, the constructor is called to make the CDO (Class Default Object)
- When the class is hot-reloaded, the constructor is called again to remake the CDO
- When you load a level (in game or in editor), the constructor is called on every instance of the class in the level.
- When you play a level, the constructor is called on every instance of the class in the level before BeginPlay is called.
So when you load the level in the editor, that is the first time your object is created. When you click “Play”, the object’s
constructor get called again. So as a general rule, put code that you want run in the editor (before you hit play) and in the game in the constructor and put the code that you want run only in the game in the BeginPlay function.
(I hope this makes sense)
The CDO is a bit tricky to understand. It is not placed in a level and has nothing to do with you. However, the CDO is made before UWorld is made. If you want to use GetOwner or GetWorld in the constructor,
put the code using it inside this if statement:
if (!HasAnyFlags(RF_ClassDefaultObject)) { /* GetWorld() */ }
What this does is it makes that section only run on your “real” objects that you placed in the level and not on the CDO, which gets built during engine startup before the UWorld is created.
Summary: Use the constructor for code that only has to do with setting up your object, use BeginPlay for gameplay actions.
If this doesn’t fully solve your problem and your object is a UActorComponent, try looking into overriding PostInitProperties.
Hope this helps.