Interaction Advanced System

Im doing a interaction system, in c++ , that i want to do is make a system in that i can focus in specific objets to interact, each object contain a interact interface, and each object can should show a UI for display the specific actions to the player can make in each specific object

Im trying to make that when i view an object that contain the interface, this object make an outline of himself, like the outline image that i put

The outline system works very well,because i can focus in specific blueprints and the mesh make an outline properly but, my gpu works at 20 % before the press play , but after press play my gpu works at 93%, im using a component in my character for make the interaction, , the UI system yet implement, but i dont know where implemented this, in the tick or in another part.

The tick in my character right now is the next code:

void ACH_SimulatorCore::Tick(float DeltaTime)
{

	Super::Tick(DeltaTime);

	if (InteractComponent){InteractComponent->PerformLineTrace();}
	
}

I know I shouldn’t use it for the whole tick, but I don’t know how to make this system optimally, the interaction of components has this code:


void UC_Interaction::PerformLineTrace()
{
	AActor* Owner = GetOwner();
	if (!Owner) return;

	//Obtener la camara
	UCameraComponent* CameraComponent = Owner->FindComponentByClass<UCameraComponent>();
	if (!CameraComponent) return;

	//Configurar el inicio y el final del rayo
	FVector Start = CameraComponent->GetComponentLocation();
	FVector End = Start + (CameraComponent->GetForwardVector() * InteractionRange);

	FHitResult HitResult;
	FCollisionQueryParams Params;
	Params.AddIgnoredActor(Owner);

	//Realizar el trace
	bool bHit = GetWorld()->LineTraceSingleByChannel(HitResult, Start,End,ECC_GameTraceChannel1, Params);

	//DrawDebugLine(GetWorld(),Start,End,bHit ? FColor::Green : FColor::Red, false,0.1f,0,1.0f);

	AActor* HitActor = bHit ? HitResult.GetActor() : nullptr;

	UpdateHighlight(HitActor);
	
}

void UC_Interaction::UpdateHighlight(AActor* NewActor)
{
	//Apagar el outline del actor anterior
	if (LastHighlightedActor && LastHighlightedActor != NewActor)
	{
		UStaticMeshComponent* Mesh = LastHighlightedActor->FindComponentByClass<UStaticMeshComponent>();
		if (Mesh) Mesh->SetRenderCustomDepth(false);
	}

	//Encender el outline del nuevo actor si tiene la interfaz
	if (NewActor && NewActor->Implements<UI_Interact>())
	{

		 UStaticMeshComponent* Mesh = NewActor->FindComponentByClass<UStaticMeshComponent>();
		if (Mesh) Mesh->SetRenderCustomDepth(true);
		LastHighlightedActor =  NewActor;
		
	}else
	{
		LastHighlightedActor = nullptr;
	}

}


void UC_Interaction::PerformInteraction()
{

	if (LastHighlightedActor && LastHighlightedActor->Implements<UI_Interact>())
	{

		II_Interact::Execute_Interact(LastHighlightedActor,GetOwner());
		
	}
	
}

void UC_Interaction::EnableOutline(AActor* Actor, bool bEnable)
{

	if (!Actor) return;

	UStaticMeshComponent* Mesh = Actor->FindComponentByClass<UStaticMeshComponent>();
	if (Mesh)
	{

		Mesh->SetRenderCustomDepth(bEnable);
		Mesh->SetCustomDepthStencilValue(bEnable ? 2.0 : 0.0); 
		
	}
	
}


At the start of UpdateHighlight, if NewActor == LastHighlightedActor, you can return early to skip wasted work of unhighlighting/highlighting the same object every tick. This will ensure that you will only switch objects when a different object is found by the trace.

if (NewActor == LastHighlightedActor)
{
   return;
}

For figuring out performance problems, you should look at:

My suggestion above is just a guess. Profiling code is the only way to really figure out what is causing performance problems. It is often not what you would guess!

1 Like