Dynamically creating lights and Performance question.

Hi, i’m currently working on a small game in which you will need to re-direct light until it hits a certain point in the level. You re-direct the light by using mirror that can be rotated around etc.
i’m currently using line tracing to calculate the reflecting light, and these line traces are updated each tick to keep the lines up to date in case the player in some way moves anything that might affect the light in some way.

Now i have a question regarding the lighting the reflecting light should give off, i’m thinking of having so when the light hits a reflecting surface and the line trace reflects in a certain direction to dynamically create a spot light at the hit location of the line trace and set the rotation of that light the the new reflected direction.
But since i update the line traces each frame this sounds quite heavy on performance, so i’m wondering if there is any alternative ways for me to do this or will i have to create and delete lights each tick?

I don’t think traces will be heavy on performance in this case.
You don’t actually need to spawn/delete lights each tick, it’s enough to spawn them once and then just update their transform according to new hit result.

Instead of spawning spotlights, try to move them and rotate it with surface normal. When the trace is invalid simply set the spotlight brightness to 0.

That could work, i’ll have to give that a shot. Thanks!

I have a follow up question about the lights, i decided to try out having a max reflect number that limits how many times the line trace can reflect and then create that amount of lights.
What i would like to know, how do you dynamically create light components? I tried the StaticConstructObject function but could not get it to work the way i wanted it to.


GWorld->SpawnActor<ASpotLight>(ASpotLight::StaticClass(), Location, Rotation);

What problems did you run into? Something like this should work (placed inside your Actor class):



USpotLightComponent* NewSpot = NewObject<USpotLightComponent>(this);
NewSpot->RegisterComponent();


NewObject is just a slightly more concise version of the ConstructObject syntax.

I managed to get creating the spotlights working the way i wanted it too although i’m running into some issues with setting the OuterConeAngle of the spotlight which you might be able to help me with.

This is my current code for creating the light but if i add in the line that sets the OuterConeAngle i get errors. It works fine without it.

void ALightOrigin::BeginPlay()
{
Super::BeginPlay();

ASpotLight* SpotLightTemp;
SpotLightTemp = GWorld-&gt;SpawnActor&lt;ASpotLight&gt;(ASpotLight::StaticClass());
SpotLightTemp-&gt;SetEnabled(true);
SpotLightTemp-&gt;SetBrightness(Brightness);

SpotLightTemp-&gt;SetOuterConeAngle(2.0f);

SpotLightTemp-&gt;SetMobility(EComponentMobility::Movable);
SpotLightTemp-&gt;AttachRootComponentToActor(this);
SpotLightTemp-&gt;SetActorLocation(GetActorLocation());
SpotLightTemp-&gt;SetActorRotation(GetActorRotation());
SpotLights.Add(SpotLightTemp);

}

This is the error i’m getting.
Error 1 error LNK2019: unresolved external symbol “public: void __cdecl ASpotLight::SetOuterConeAngle(float)” (?SetOuterConeAngle@ASpotLight@@QEAAXM@Z) referenced in function “public: virtual void __cdecl ALightOrigin::BeginPlay(void)” (?BeginPlay@ALightOrigin@@UEAAXXZ) C:\Users\Documents\Unreal Projects\Temple\Intermediate\ProjectFiles\LightOrigin.cpp.obj