Dynamic Lighting (in Prefabs)

Discussion in 'Programming' started by King Games, Mar 14, 2025.

  1. King Games

    King Games
    Expand Collapse

    Joined:
    Feb 26, 2018
    Messages:
    115
    Lighting (ONLY ON FOR NIGHT)

    I achieved this already in the base map by
    stealing beamngs Lua code for turning lights
    on and off during certain times:
    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.79 then
            value = true
        end
    
        if lastValue == value then return end
        lastValue = value
    
        if scenetree.harbour_lights then
          setAllLightsEnabled(scenetree.harbour_lights, value )
        end
    end
    
    M.onUpdate = onUpdate
    
    return M
    but how would i achieve this,
    inside of a mission, inside of a Prefab file?

    Here's MegaKnights test code, it works fine when the prefab is unpacked.
    The internal names of the objects inside the prefab are printed in the console.
    Code:
    local t = scenetree.testPrefab_unpacked:getObjects() for k, v in pairs(t) do print(scenetree[t[k]]:getInternalName()) end
    But it doesn't work when the prefab is packed.
    Which means, dynamic lights within a prefab aren't possible with lua.

    Hopefully someone can help :(
     
  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