Point lights that come on at night

Discussion in 'Content Creation' started by jammin2222, May 27, 2020.

  1. jammin2222

    jammin2222
    Expand Collapse

    Joined:
    Sep 15, 2013
    Messages:
    386
  2. Elvisnake

    Elvisnake
    Expand Collapse

    Joined:
    Aug 6, 2013
    Messages:
    206
    Like for the map Utah: in the main file of your map, create a mainLevel.lua

    Inside put something like this

    Code:
    local M = {}
    
    local function setAllLightsEnabled(group, value)
        for i = 0, group.obj:getCount(), 1 do
            local id = group.obj:idAt(i)
            local obj = scenetree.findObjectById(id)
            if obj and obj.obj:isSubClassOf('LightBase') then
                obj.obj:setLightEnabled( value )
            end
        end
    end
    
    local lastValue = nil
    
    local function onUpdate()
        local tod = scenetree.tod
        if not tod then return end
    
        local value = false
        if tod.time > 0.21 and tod.time < 0.77 then
            value = true
        end
    
        if lastValue == value then return end
        lastValue = value
    end
    
    M.onUpdate = onUpdate
    
    return M
     
    • Like Like x 2
  3. jammin2222

    jammin2222
    Expand Collapse

    Joined:
    Sep 15, 2013
    Messages:
    386
    That's some awesome info, I will have a fiddle about. I was unaware Utah had lights that came on at night. I can at least see how its done now hopefully.
    I will probably be back in a few hours with a bunch of questions though. haha.
    Thank you so much!
     
  4. Elvisnake

    Elvisnake
    Expand Collapse

    Joined:
    Aug 6, 2013
    Messages:
    206
    Sure, let me know if it's work;)
     
    • Like Like x 1
  5. jammin2222

    jammin2222
    Expand Collapse

    Joined:
    Sep 15, 2013
    Messages:
    386
    Ok here comes the questions lol.
    So I tried putting your code into the mainLevel.lua and no lights came on at night. So I looked at the lua in Utah and it was a little different.
    It looks like its pointing to the folder in the scene tree where the lights are.
    My lights are in a folder called dynamiclights and in the Utah map they are in one called auto_repair_lights.

    So I changed auto_repair_lights to dynamiclights in the levelMain.lua and it still didn't work.

    Here is the code from Utah
    Code:
    local M = {}
    
    local function setAllLightsEnabled(group, value)
        for i = 0, group.obj:getCount(), 1 do
            local id = group.obj:idAt(i)
            local obj = scenetree.findObjectById(id)
            if obj and obj.obj:isSubClassOf('LightBase') then
                obj.obj:setLightEnabled( value )
            end
        end
    end
    
    local lastValue = nil
    
    local function onUpdate()
        local tod = scenetree.tod
        if not tod then return end
    
        local value = false
        if tod.time > 0.21 and tod.time < 0.77 then
            value = true
        end
    
        if lastValue == value then return end
        lastValue = value
    
        if scenetree.auto_repair_lights then
          setAllLightsEnabled(scenetree.auto_repair_lights, value )
          setAllLightsEnabled(scenetree.tourist_center_lights, value )
        end
    end
    
    M.onUpdate = onUpdate
    
    return
    and here is what I tried changing it to
    Code:
    local M = {}
    
    local function setAllLightsEnabled(group, value)
        for i = 0, group.obj:getCount(), 1 do
            local id = group.obj:idAt(i)
            local obj = scenetree.findObjectById(id)
            if obj and obj.obj:isSubClassOf('LightBase') then
                obj.obj:setLightEnabled( value )
            end
        end
    end
    
    local lastValue = nil
    
    local function onUpdate()
        local tod = scenetree.tod
        if not tod then return end
    
        local value = false
        if tod.time > 0.21 and tod.time < 0.77 then
            value = true
        end
    
        if lastValue == value then return end
        lastValue = value
    
        if scenetree.dynamiclights then
          setAllLightsEnabled(scenetree.dynamiclights, value )
        end
    end
    
    M.onUpdate = onUpdate
    
    return
    I am obviously missing something here, any ideas?
    --- Post updated ---
    Ignore me I noticed I was missing the M at the very end. I'm such a noob.
    Thanks for pointing me in the right direction, I'm so pleased to have it working again :D
     
    • Like Like x 1
  6. Elvisnake

    Elvisnake
    Expand Collapse

    Joined:
    Aug 6, 2013
    Messages:
    206
    You re welcome
     
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice