If you create a new actor class but don’t use it, the constructor is still called by the system somehow. Holds true for Standalone Game too. This means you can’t make any normal assumptions about the construction order of classes.
Why is this?
Thanks
Um, never mind, I asked ChatGPT:
" In Unreal Engine, the constructors of classes, including actor classes, are automatically invoked during the engine’s initialization process. This behavior occurs even if you don’t explicitly use the class in your game code. The Unreal Engine runtime system automatically initializes all classes during startup to ensure that they are ready for use if needed. This behavior is part of the engine’s reflection system and is common in many object-oriented game engines and frameworks.
The reason for this automatic initialization lies in Unreal Engine’s need for reflection, serialization, and various other engine features. Unreal Engine uses the information gathered during the construction of these classes for a variety of purposes, including:
- Reflection: Unreal Engine needs to know about the properties, functions, and other members of classes to support features like Blueprint exposure, serialization, and replication. This information is gathered during class construction.
- Garbage Collection: Unreal Engine’s garbage collector needs to understand the memory layout and references of all objects. Class construction provides essential information for proper garbage collection.
- Editor Support: In the Unreal Editor, classes and their properties need to be visible and accessible even if they are not explicitly used in game code. The editor relies on the constructed classes to provide a rich user experience.
Due to these requirements, Unreal Engine initializes all classes during startup, leading to the invocation of constructors. This means you can’t make assumptions about the construction order of classes, especially if you’re relying on side effects in constructors. It’s a good practice to minimize side effects in constructors and use other initialization methods like BeginPlay
for actors to ensure predictable behavior."
It was called to create ClassDefaultObject.
It’s a prototype pattern.
I don’t know the exact implementation but every time you spawn the actor, it need to copy the template/archetype/defaultobject.
Those property system that ChatGPT mention should already be in UClass which is in generated header.
It’s different than the actual actor class.
UClass own the ClassDefaultObject.
They also abstract away the blueprint node ( includes every function, new variable, the defualt value in blueprint ) unless you explicitly load.
The blueprint node is stored in UBlueprintGeneratedClass.
You can access default native C++ initialized value in ClassDefaultObject.
But for the blueprint, You need to iterate through UBlueprintGeneratedClass.