Toggleable / Dynamic Lights?

Discussions about Coding and Scripting
1337GameDev
Experienced
Posts: 85
Joined: Thu Apr 16, 2020 3:23 pm
Personal rank: GameDev

Toggleable / Dynamic Lights?

Post by 1337GameDev » Wed Jan 27, 2021 6:45 am

I am curious on making a light that follows an arbitrary function. I have a basic light subclass Actor, and a function to change it's Brightness via a count of time via a "Tick" function (which currently uses a basic Sin wave function), and the brightness value changes (0-255), but the light doesn't seem to change in brightness at all in game (but i can see it changes via logfile Log calls outputting the value). Is there an issue with dynamic lights? I know it's possible because of TriggerLight, but unsure if there's some "gotcha" im missing.

ExpEM
Skilled
Posts: 172
Joined: Wed Nov 09, 2016 1:48 am

Re: Toggleable / Dynamic Lights?

Post by ExpEM » Wed Jan 27, 2021 7:53 am

Having messed with this before...
If I remember correctly, you need to mess with the alpha value of the light. Have a look at the defalt triggerlight code.
However, I found doing this would crash some renderers...

1337GameDev
Experienced
Posts: 85
Joined: Thu Apr 16, 2020 3:23 pm
Personal rank: GameDev

Re: Toggleable / Dynamic Lights?

Post by 1337GameDev » Thu Jan 28, 2021 4:28 am

Instead of brightness, it's the alpha value?

I found TriggerLight, and it's code that applies the change. The Alpha value is a locally declared float.

Code: Select all

// Called whenever time passes.
function Tick( float DeltaTime )
{
	Alpha += Direction * DeltaTime / ChangeTime;
	if( Alpha > 1.0 )
	{
		Alpha = 1.0;
		Disable( 'Tick' );
		if( SavedTrigger != None )
			SavedTrigger.EndEvent();
	}
	else if( Alpha < 0.0 )
	{
		Alpha = 0.0;
		Disable( 'Tick' );
		if( SavedTrigger != None )
			SavedTrigger.EndEvent();
	}
	if( !bDelayFullOn )
		LightBrightness = Alpha * InitialBrightness;
	else if( (Direction>0 && Alpha!=1) || Alpha==0 )
		LightBrightness = 0;
	else
		LightBrightness = InitialBrightness;
}
Essentially, the code of importance is here:

Code: Select all

LightBrightness = Alpha * InitialBrightness;
So in the end, the alpha value is blended with the initial brightness, to get a 0-255 value and changes the

Code: Select all

LightBrightness

User avatar
Feralidragon
Godlike
Posts: 5300
Joined: Wed Feb 27, 2008 6:24 pm
Personal rank: Work In Progress
Location: Liandri

Re: Toggleable / Dynamic Lights?

Post by Feralidragon » Thu Jan 28, 2021 3:20 pm

It could be a wide range of things: it could be the renderer you're using (some renderers do heavy caching on things like lights, although dynamic lights should be largely unaffected), it could be that you have dynamic lights disabled to begin with in the game (have you checked your settings?), etc.

If you have dynamic lights enabled, and the same happens regardless of the renderer you use, then it could be the code itself: you say you'e logging this, but are you really logging the current light value, or the variable that is going to change it?

There could also be a mistake in the code itself of this kind, especially around type conversions since sin functions require the usage of floating point, but the end result is being stored in a byte (LightBrightness type), which depending on how it's done it could lead to some iffy behavior like a value always staying at zero (although if you have logged the 0-255 range, I doubt this to be the case, but still...), meaning that you should generally explicitly cast each value to the type you need when you're dealing with a mix of types, since the language may try to wrongly convert a value to byte or int, wrongly zeroing your values at some point if values between 0.0 and 1.0 are used for example (in the case of a sin function).