x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

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

Product Version: UE 4.17
Tags:
more ▼

asked Oct 20 '17 at 03:17 AM in C++ Programming

avatar image

dajack05
6

avatar image Xnar Oct 21 '17 at 06:10 PM

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.

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

0 answers: sort voted first
Be the first one to answer this question
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question