Change custom depth setting on client only

Hi,

I’m currently creating an interaction system in C++ and am having trouble making the outline only show on the client that can interact with the object.
My code looks like this:

CPP

void AHiderCharacter::Tick(float DeltaTime)
{
	HidePropOutline();
	LastProp = nullptr;

	FHitResult HitResult;
	bool bDidHit = GetPropInView(HitResult);
	if (bDidHit)
	{
		UStaticMeshComponent* Component = Cast<UStaticMeshComponent>(HitResult.GetComponent());
		LastProp = Component;
		ShowPropOutline();
	}
}

// ...

bool AHiderCharacter::GetPropInView(FHitResult& OutHitResult) const
{
	FVector Start = CameraBoomComponent->GetComponentLocation();
	FVector End = Start + (DetectPropDistance * CameraBoomComponent->GetForwardVector());
	FCollisionShape Sphere = FCollisionShape::MakeSphere(DetectPropRadius);

	return GetWorld()->SweepSingleByChannel(
		OutHitResult,
		Start, End,
		FQuat::Identity,
		ECollisionChannel::ECC_GameTraceChannel2,
		Sphere
	);
}

void AHiderCharacter::ShowPropOutline_Implementation()
{
	FString Name = GetActorNameOrLabel();
	GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, Name);
	if (IsValid(LastProp))
	{
		LastProp->SetCustomDepthStencilValue(1);
		LastProp->SetRenderCustomDepth(true);
	}
}

void AHiderCharacter::HidePropOutline_Implementation()
{
	if (IsValid(LastProp))
	{
		LastProp->SetRenderCustomDepth(false);
	}
}

H

// ...

	UPROPERTY(VisibleAnywhere)
	UCameraComponent* CameraComponent;

// ...

private:
	UPROPERTY(EditAnywhere)
	float DetectPropDistance = 300;

	UPROPERTY(EditAnywhere)
	float DetectPropRadius = 20;

	UStaticMeshComponent* LastProp;

	bool GetPropInView(FHitResult& OutHitResult) const;

	UFUNCTION(Client, Reliable)
	void ShowPropOutline();

	UFUNCTION(Client, Reliable)
	void HidePropOutline();

When this runs I get both characters in the scene printing their name to the screen even when only one of them is looking at an object that can be interacted with.

Hi all,

Was able to get it working by checking if the tick function was being run on the character’s client. Here’s how I did it:

CPP

void AHiderCharacter::Tick(float DeltaTime)
{
	UWorld* World = GetWorld();
	bool bIsDedicatedServer = UKismetSystemLibrary::IsDedicatedServer(World);

	if (!bIsDedicatedServer && GetController() == UGameplayStatics::GetPlayerController(World, 0))
	{
		HidePropOutline();
		LastProp = nullptr;

		FHitResult HitResult;
		bool bDidHit = GetPropInView(HitResult);
		if (bDidHit)
		{
			UStaticMeshComponent* Component = Cast<UStaticMeshComponent>(HitResult.GetComponent());
			LastProp = Component;
			ShowPropOutline();
		}
	}
}

bool AHiderCharacter::GetPropInView(FHitResult& OutHitResult) const
{
	FVector Start = CameraBoomComponent->GetComponentLocation();
	FVector End = Start + (DetectPropDistance * CameraBoomComponent->GetForwardVector());
	FCollisionShape Sphere = FCollisionShape::MakeSphere(DetectPropRadius);

	return GetWorld()->SweepSingleByChannel(
		OutHitResult,
		Start, End,
		FQuat::Identity,
		ECollisionChannel::ECC_GameTraceChannel2,
		Sphere
	);
}

void AHiderCharacter::ShowPropOutline()
{
	FString Name = GetActorNameOrLabel();
	if (IsValid(LastProp))
	{
		LastProp->SetCustomDepthStencilValue(1);
		LastProp->SetRenderCustomDepth(true);
	}
}

void AHiderCharacter::HidePropOutline()
{
	if (IsValid(LastProp))
	{
		LastProp->SetRenderCustomDepth(false);
	}
}

H

// ...

	UPROPERTY(VisibleAnywhere)
	UCameraComponent* CameraComponent;

// ...

private:
	UPROPERTY(EditAnywhere)
	float DetectPropDistance = 300;

	UPROPERTY(EditAnywhere)
	float DetectPropRadius = 20;

	UStaticMeshComponent* LastProp;

	bool GetPropInView(FHitResult& OutHitResult) const;

	void ShowPropOutline();

	void HidePropOutline();

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.