Before we kick off, I wanted to mention, that I’m fairly new to programming in Unreal. Right now I’m just looking at source code to get an understanding of how things are meant to be done.
Let me give you two excerpts from ACharacter and ACharacterMovementComponent, before asking my question.
In the ACharacterMovementComponent class there is a pointer to the owerning ACharacter.
Lets call the following line Example 1:
UPROPERTY(Transient, DuplicateTransient)
TObjectPtr<ACharacter> CharacterOwner;
So far so good.
This pointer is not set in the constructor, but rather through these two methods of the ACharacterMovementComponent class:
ENGINE_API virtual void SetUpdatedComponent(USceneComponent* NewUpdatedComponent) override;
and
ENGINE_API virtual void PostLoad() override;
On the other hand, there are many pointers to components in the ACharacter class.
I’m just going to pick one and call the following line Example 2:
UPROPERTY(Category=Character, VisibleAnywhere, BlueprintReadOnly, meta=(AllowPrivateAccess = "true"))
TObjectPtr<UCapsuleComponent> CapsuleComponent;
This pointer is set directly in the constructor via
CreateDefaultSubobject<UCapsuleComponent>(ACharacter::CapsuleComponentName)
As far as I know, for every UCLASS the constructor is called exactly once, when creating the corresponding CDO (Class Default Object), which then serves as a template for every instance created further down the line.
- Question 1
So for my first question, wouldn’t Example 2 imply, that all character instances share a pointer to the same capsule component instance, since the pointer is set in the constructor, which is only being used once when creating the CDO as a template?
- Question 2
And for my second question, why would Example 1 be marked as Transient, while Example 2 would not.
The way I understand Transient as a property specifier, it prevents saving to and loading from disk. So why would it be a good idea to prevent Example 1 from being saved and loaded, but not Example 2, since both are just pointers. Saving and loading pointers instead of actual objects sounds like a bad idea to begin with.
If someone can recomend a good read on the matter, I will gladly get the answers myself. After days it feels like the more I look, the less I find on the matter.