Multiple spotlights cause flicker in C++ class

Hey guys,

Here is my problem. I am trying to learn UE4 C++, so I decided to start by creating a class which has a couple scene components in it. It then moves a spotlight to track the player along a predefined path. The problem is, when I create more than one instance of this class, the spotlights seem to fight eachother. I can only see one at a time, and sometimes even the one I can see flickers like crazy!

Here is my class code. Any help is appriciated.

#TranslatingLight.h

// Fill out your copyright notice in the Description page of Project Settings.
#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "TranslatingSpotlight.generated.h"

class USpotLightComponent;

UCLASS()
class WALLACE_API ATranslatingSpotlight : public AActor
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	ATranslatingSpotlight();

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

private:
	APlayerCameraManager* mCamera;

	TArray<FVector> pathNodes;

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

	UPROPERTY(EditAnywhere)
		USceneComponent* root;
	UPROPERTY(EditAnywhere)
		USceneComponent* pos1;
	UPROPERTY(EditAnywhere)
		USceneComponent* pos2;
	
	UPROPERTY(EditAnywhere)
		USpotLightComponent* mLight;

	UPROPERTY(EditAnywhere, Category = "TranslateLight", meta = (ClampMin = "2", UIMin = "2"))
		int pathNodeCount = 3;

	UPROPERTY(EditAnywhere, Category = "TranslateLight")
		float speed = 1.0f;
	
};

#TranslatingLight.cpp

// Fill out your copyright notice in the Description page of Project Settings.

#include "TranslatingSpotlight.h"
#include "Kismet/GameplayStatics.h"
#include "Kismet/KismetMathLibrary.h"
#include "Engine/World.h"
#include "Camera/PlayerCameraManager.h"
#include "GameFramework/Actor.h"
#include "Components/SphereComponent.h"
#include "Components/StaticMeshComponent.h"
#include "Components/SpotLightComponent.h"


// Sets default values
ATranslatingSpotlight::ATranslatingSpotlight()
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

	this->root = CreateDefaultSubobject<USphereComponent>(TEXT("root"));
	this->RootComponent = this->root;

	this->mLight = CreateDefaultSubobject<USpotLightComponent>(TEXT("Spotlight"));
	this->mLight->SetIntensity(65000);
	this->mLight->SetInnerConeAngle(60.0f);
	this->mLight->SetOuterConeAngle(65.0f);
	this->mLight->AttachTo(this->root);

	this->pos1 = CreateDefaultSubobject<USphereComponent>(TEXT("Position 1 Marker"));
	this->pos1->AttachTo(this->root);

	this->pos2 = CreateDefaultSubobject<USphereComponent>(TEXT("Position 2 Marker"));
	this->pos2->AttachTo(this->root);
}

// Called when the game starts or when spawned
void ATranslatingSpotlight::BeginPlay()
{
	Super::BeginPlay();
	this->mCamera = UGameplayStatics::GetPlayerCameraManager(GetWorld(), 0);

	//Generate pathNodes
	for (int i = 0; i < this->pathNodeCount; i++) {
		FVector mPos = this->pos2->GetComponentLocation() - this->pos1->GetComponentLocation();
		mPos = mPos / (this->pathNodeCount-1);
		mPos = mPos * i;
		mPos += this->root->GetComponentLocation();
		this->pathNodes.Emplace(mPos);
	}
}

// Called every frame
void ATranslatingSpotlight::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

	if (this->mCamera) {

		int selectedPos = 0;
		for (int i = 0; i < this->pathNodes.Num(); i++) {
			if (FVector::Distance(this->pathNodes[i], this->mCamera->GetCameraLocation()) < FVector::Distance(this->pathNodes[selectedPos], this->mCamera->GetCameraLocation()))
				selectedPos = i;
		}

		FVector newLoc = FMath::Lerp(this->mLight->GetComponentLocation(), this->pathNodes[selectedPos], DeltaTime*speed);
		this->mLight->SetWorldLocation(newLoc);

		//FRotator lookRot = UKismetMathLibrary::FindLookAtRotation(mLight->GetComponentLocation(), mCamera->GetCameraLocation());
		//mLight->SetWorldRotation(lookRot);
	}
}

As I am still fairly new, I am open to any extra advice you guys might have on my code as well.

Thanks!

Daniel Jackson

I just discovered this issue too. In Blueprint I put Spotlight components on some guns to act as flashlights. When the spotlights come near eachother they start flickering. They flicker faster the closer they are. I disabled “Cast Shadows” in the Spotlight settings and the flickering stopped but of course they no longer cast shadows. I hope a real solution comes soon.