PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience

Glowing Dynamic Lights

Glowing Dynamic Lights

Jul 03 2018, 10:36 AM PST 5 min

With dynamic lighting builders are able to achieve a new level of realism. However, in real life lights don’t just sit at a standstill. Think about something as simple as a camp fire or a lamp dimming, lights are always moving. In this tutorial you will learn a simple method that both brightens and dims lights creating a glowing effect.

Creating a light

First thing’s first. You obviously need some kind of dynamic lighting object. This tutorial will use a PointLight, but keep in mind this would work the exact same way with a
SpotLight. You’ll also need a script to control the the glow effect.

Example camp fire:

ExampleCampFire

Writing the code

A lot of what you’ll be looking at today involves Articles/Roblox Coding Basics Loops|loops. If you’re not familiar with loops, specifically the Articles/For Loops, then it’s highly recommended
you review and then come back to this tutorial.

Now onto the code. The first thing you’re going to do is create a function with what seems like a ridiculously long list of parameters! In reality however, each and every one of these parameters is extremely important to the function as a whole, specifically the main loop. Let’s take a look!

function AdjustLights(Start, End, Interval, Time, Table_of_lights)
	for Index = Start, End, Interval do
		wait(Time)
	end
end
  • Start: This will be the starting number for the main loop.
  • End: This will be the ending number for the main loop.
  • Interval: This will be the number by which the main loop will count. Keep in mind this may also include positive or negative direction.
  • Time: The time between each cycle of the loop.
  • Table_of_lights: All the lights that this function will be controlling in a table.

The above code is great and all but with one problem, it doesn’t actually do anything yet! Let’s break this down. In the main loop the value Index represents the for loop’s current value. That value is highly dependent on the parameters that were discussed earlier. This gives the function a lot of flexibility because it can change what the function does in just a few key strokes. Let’s look at an example.

Recall what just happened. The first loop counted from zero to three by an interval of one every 0.1 seconds. The second loop counted from six to two by an interval of negative two every 0.4 seconds. If you’re still confused play around with the above function by changing the parameters, things will become clear with more examples.

Now that you understand what you can do with the function and what Index represents, you can harness that value and gradually change either the PointLight/Range|Range or Lighting/Brightness of a dynamic light. To do that you’ll simply have to use another loop to gather all the values of Table_of_lights and set their range or brightness property to Index.

function AdjustLights(Start, End, Interval, Time, Table_of_lights)
	for Index = Start, End, Interval do
		for Light_Index = 1, #Table_of_lights do
			Table_of_lights[Light_Index].Range = Index -- Or Table_of_lights[Light_Index].Brightness = Index
		end
		wait(Time)
	end
end

Now all that has to be done is add an infinite loop to keep the objects glowing.

Recall the example camp fire and location of the script.

function AdjustLights(Start, End, Interval, Time, Table_of_lights)
	for Index = Start, End, Interval do
		for Light_Index = 1, #Table_of_lights do
			Table_of_lights[Light_Index].Range = Index -- Or Table_of_lights[Light_Index].Brightness = Index
		end
		wait(Time)
	end
end

while true do
	AdjustLights(14, 4, -0.5, 0.1, {script.Parent.PointLight})
	AdjustLights(4, 14, 0.5, 0.1, {script.Parent.PointLight})
end

Enjoy! And don’t forget to turn on Lighting/GlobalShadows.

Glowing_Campfire.gif

See also

Roblox Coding Basics Loops|Loops
PointLight
SpotLight
Table

Tags:
  • lighting
  • fire
  • campfire
  • glow
  • fx