TSoftObjectPtr or TObjectPtr

Hi!

I have this attribute:

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;

	UPROPERTY(EditDefaultsOnly, Category = "Components")
	TSoftObjectPtr<class ADoorLever> DoorLever;

Which I use here:

void ADoor::BeginPlay()
{
	Super::BeginPlay();

	if (DoorLever)
	{
		UDoorInteractionComponent* InteractionComp = 
			DoorLever->FindComponentByClass<UDoorInteractionComponent>();

		if (InteractionComp)
		{
			UE_LOG(LogTemp, Warning, TEXT("[ ADoor::BeginPlay() ] : Get Interaction Component"));

			InteractionComp->OnInteraction.BindUObject(this, &ADoor::HandleOnInteraction);
		}
	}
}

Do I really need to set the DoorLever attribute as TSoftObjectPtr?

I think I have copied and now I’m not sure if I really need to use TSoftObjectPtr. Probably, the right choice is TObjectPtr.

Thanks!

Hi!
TSoftObjectPtr is used to reference objects that might not be loaded yet, and it gives you the ability to load them asynchronously, via its path or other methods. What you need is TObjectPtr which is a strong pointer to a reference, and it should be loaded when you are accessing it, just like in your case. So, unless you need some kind of asynchronous loading, or referencing an object that might not be available yet, simply use the TObjectPtr.

BTW check this out; it’ll give you a pretty decent idea about the whole concept: All about Soft and Weak pointers | Epic Developer Community (epicgames.com)

1 Like