Warning C4996: 'AActor::GetComponentsByClass': Use one of the GetComponents implementations as appropriate

Hello.
I’m having difficulty getting my project to build, and noticed that AActor::GetComponentsByClass is obsolete in UE4 version 4.26.
I have followed a tutorial, and i’m not quite shure how to update my code for it to work.

The code is:

if (!GetOwner()->HasAuthority())
	{
		for (auto& VisualComp : GetOwner()->GetComponentsByClass(UPrimitiveComponent::StaticClass()))
		{
			if (UPrimitiveComponent* Prim = Cast<UPrimitiveComponent>(VisualComp))
			{
				Prim->SetRenderCustomDepth(true);
			}
		}
	}

I have tried to modify the code as seen below:

if (!GetOwner()->HasAuthority())
	{
		TArray<UPrimitiveComponent*> Components;

		GetOwner()->GetComponents(UPrimitiveComponent::StaticClass(), Components);

		for (auto VisualComp : Components)
		{
			if (UPrimitiveComponent* Prim = Cast<UPrimitiveComponent>(VisualComp))
			{
				Prim->SetRenderCustomDepth(true);
			}
		}
	}

but this gives and error “C++ no instance of overloaded function “AActor::GetComponents” matches the argument list. argument types are: (UClass *, TArray) object type is: AActor”

Does anyone have a solution for this?

Thanks!

1 Like

Try changing

TArray<UPrimitiveComponent*> Components;

To

TArray<UActorComponent*> Components;

Thank you, it works now!