Set Intensity of a Directional Light C++

Hi!

In my project, I have a directional light (My sun).

Actually I’m trying to modify it by code…

In my Level Script (Level blueprint c++ class), I have something like:


UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Level Lights") 
AActor* MyLight;

So, I can go to my level blueprint and reference the Directional Light of my level, and I can acces it to modify his values:


MyLight->AddActorLocalRotation(0.f,1.f,0.f)

It works perfectly… But my problem, is when I try to modify for example the Intensity value (which is part of the inherited Directional Light, not of the actor…)

PD: MyLight->SetIntensity, doesn’t work, because Intensity isn’t a member of my actor.

How can I do this?

Thanks.

My recent code. Confirmed working in 4.13 . Where SunLight is ADirectionalLight(Light Actor in the scene).

If you are trying to access UDirectionalLightComponent(Component) in an AActor(Blueprint that was placed in the scene), try the following:


if (TryLight->IsValidLowLevel()) {
	UDirectionalLightComponent * GetTryLight = Cast<UDirectionalLightComponent>(TryLight->GetComponentByClass(UDirectionalLightComponent::StaticClass()));
	if (GetTryLight->IsValidLowLevel()) {
		FString TExt = GetTryLight->GetName();
		UE_LOG(LogTemp, Warning, TEXT("GetTryLight =  %s"), *TExt);
	}
}

Where TryLight is AActor * with UPROPERTY(EditAnywhere)

Wow! It works perfectly!

Absolutly gratefull with you :smiley:

Thanks :slight_smile:

Hi there, is someone can show how to do it in level blueprint ? thanks a lot :slight_smile:

hi,

I have the same issue since 4.20. I try to vary my light intensity in fonction of the Arkit light parameters. It works fine in 4.19 but not in 4.20. I just do a setIntensity on my light component. I use bluePrint.

@pickersZ – can you clarify the following formula? It seemingly works but I am trying to understand the rationale behind it.
float SunIntensity = SunLightRot.Pitch*-0.05f;
Why multiply by -0.05?