|
| 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
|
  |
| 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 |
|
|
|
| 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
|
  |
| 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 |
|
|
|
| 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 |
|
|
|
| 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 |
|
|
|
| 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
|
  |
| 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 |
|
|