generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

unsure of how to use recursion

Previous Thread :: Next Thread 
ColorfulBrendon is not online. ColorfulBrendon
Joined: 28 Jul 2010
Total Posts: 23556
27 Sep 2015 02:12 AM
i've created a very large map with models within models within models and dynamic lights of all kinds throughout the place

what i'd like to do is make the lights be lit accordingly to what time of day it is so that they get dim in the morning and go off in the day, then fully light up at night

my attempts at simple scripts using recursion in the past have never successfully worked, even when no output is provided

how would i achieve this?
Report Abuse
Aokz is not online. Aokz
Joined: 02 Aug 2009
Total Posts: 11284
27 Sep 2015 02:47 AM
function recurseLights(childToSearch)
if not #childToSearch:GetChildren() < 1 then
for i,v in pairs(childToSearch:GetChildren()) do
if v:IsA("SpotLight") or v:IsA("SurfaceLight") or v:IsA("PointLight") then
v.Enabled = true
else
recurseLights(v)
end
end
end
end

that should work, idk tho
Report Abuse
buildersteven4 is not online. buildersteven4
Joined: 08 May 2009
Total Posts: 1325
27 Sep 2015 04:39 AM
slight improvement of above (haven't tested):

function enableLightsRecursive(parent, enable)
for _,child in next, parent:GetChildren() do
if child:IsA("Light") then
child.Enabled = enable
else
enableLightsRecursive(child, enable)
end
end
end
Report Abuse
eLunate is not online. eLunate
Joined: 29 Jul 2014
Total Posts: 13268
27 Sep 2015 05:09 AM
Please don't use recursion. It's not worth it and you people never do proper tail-calls
Report Abuse
warspyking is not online. warspyking
Joined: 15 Nov 2011
Total Posts: 13947
27 Sep 2015 06:50 AM
slighter improvement to above (haven't tested):


function EnableLights(parent, enabled, rec)
for _,child in next, parent:GetChildren() do
if child:IsA("Light") then
child.Enabled = enabled
end
if rec then
enableLightsRecursive(child, enabled, true)
end
end
end
Report Abuse
warspyking is not online. warspyking
Joined: 15 Nov 2011
Total Posts: 13947
27 Sep 2015 07:01 AM
Out of pure curiosity would a method like this work?

function EnableLights(parent, enabled, i)
i = i or 1
local child = parent:GetChildren()[i]
if child:IsA("Light") then
child.Enabled = enabled
end
EnableLights(parent, enabled, i+1)
return EnableLights(child, enabled, 1)
end


--I didn't test, there might be an error in the script itself, but I'm sure you understand the logic behind it
Report Abuse
warspyking is not online. warspyking
Joined: 15 Nov 2011
Total Posts: 13947
27 Sep 2015 07:02 AM
function EnableLights(parent, enabled, i)
i = i or 1
local child = parent:GetChildren()[i]
if child:IsA("Light") then
child.Enabled = enabled
end
EnableLights(parent, enabled, i+1)
return EnableLights(child, enabled)
end


Took out an unnecessary argument in my last function call.
Report Abuse
eLunate is not online. eLunate
Joined: 29 Jul 2014
Total Posts: 13268
27 Sep 2015 07:29 AM
You may appreciate a version not using recursion, just for completeness.

local function GetDescendants(o)
local task = {};
local ret = {};
local cur = o;
while cur do
local ch = cur:GetChildren();
for i=1,#ch do
local v = ch[i]
task[#task+1] = v;
ret[#ret+1] = v;
end;
cur = task[#task]; task[#task] = nil;
end;
return ret;
end;

And then you can cut it down by checking which of the descendants are in fact lights. Keep that table to save you the recursion, and iterate over it to change the cached lights whenever you need.
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image