Skip to content

Initialization of the ASC where the pawn is the owner and the avatar actor might be redundant #154

Description

@gaurang-ingale

Hello @tranek, thanks again for this treasure of a repo!

While going through it for a new project I am working on, I found that the initialization part of the ASC might be redundant when the Owner and Avatar Actors are the same object which the ASC is a member of. The reason being the function below (as of UE 5.7.0):

void UAbilitySystemComponent::InitializeComponent()
{
	Super::InitializeComponent();

	// Look for DSO AttributeSets (note we are currently requiring all attribute sets to be subobjects of the same owner. This doesn't *have* to be the case forever.
	AActor *Owner = GetOwner();
	InitAbilityActorInfo(Owner, Owner);	// Default init to our outer owner

	// cleanup any bad data that may have gotten into SpawnedAttributes
	for (int32 Idx = SpawnedAttributes.Num()-1; Idx >= 0; --Idx)
	{
		if (SpawnedAttributes[Idx] == nullptr)
		{
			SpawnedAttributes.RemoveAt(Idx);
		}
	}

	TArray<UObject*> ChildObjects;
	GetObjectsWithOuter(Owner, ChildObjects, false, RF_NoFlags, EInternalObjectFlags::Garbage);

	for (UObject* Obj : ChildObjects)
	{
		UAttributeSet* Set = Cast<UAttributeSet>(Obj);
		if (Set)  
		{
			SpawnedAttributes.AddUnique(Set);
		}
	}

	SetSpawnedAttributesListDirty();
}

This function belongs to AActor and is overriden by the ASC as it is by most component classes and the initialization already occurs here. As this function will run on both, the server and the client, it becomes unnecessary to have to call InitAbilityActorInfo on the ASC again, unless either the Owner and/or Avatar Actors have changed.

Do you think it would make sense to note this in the documentation in Section 4.1.2?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions