Solved Game World Time in Lua

Discussion in 'Mod Support' started by NiZhaZi, May 19, 2025 at 5:04 PM.

  1. NiZhaZi

    NiZhaZi
    Expand Collapse

    Joined:
    Apr 1, 2023
    Messages:
    13
    How can I get the game time of the world in 24h in Lua?
     
  2. DaddelZeit

    DaddelZeit
    Expand Collapse

    Joined:
    Jul 17, 2019
    Messages:
    3,466
    The time is available to Game Engine Lua via the time of day object.
    Code:
    local tod = core_environment.getTimeOfDay()
    local time = tod.time
    
    This time is a decimal number from 0-1 that represents 12:00 to 12:00 the next day, so you may need to do some maths.
    This works to get the time in seconds.
    Code:
    (tod.time+0.5)%1*86400
    Note that the multiplier is calculated by converting 24h into the required time unit. If you just need hours, use that.
     
  3. NiZhaZi

    NiZhaZi
    Expand Collapse

    Joined:
    Apr 1, 2023
    Messages:
    13
    Thank you for help but here's an error says core_environment is a nil value.
    Code:
    local M = {}
    
    local function init()
        
    end
    
    local function updateGFX(dt)
        local tod = core_environment.getTimeOfDay()
        local time = tod.time
        dump(time)
    end
    
    M.onInit = init
    M.onReset = init
    M.updateGFX = updateGFX
    return M
     
  4. DaddelZeit

    DaddelZeit
    Expand Collapse

    Joined:
    Jul 17, 2019
    Messages:
    3,466
    You are running in vehicle lua, not game engine lua.
    https://documentation.beamng.com/modding/programming/virtualmachines/
     
  5. NiZhaZi

    NiZhaZi
    Expand Collapse

    Joined:
    Apr 1, 2023
    Messages:
    13
  6. NiZhaZi

    NiZhaZi
    Expand Collapse

    Joined:
    Apr 1, 2023
    Messages:
    13
    I did this and it works.
    Code:
    -- ge file
    local M = {}
    
    local function onExtensionLoaded()
    
    end
    
    local function onUpdate(dt, dtSim)
        local tod = core_environment.getTimeOfDay()
    
        be:sendToMailbox("timeService", tod.time)
    end
    
    M.onExtensionLoaded = onExtensionLoaded
    M.onVehicleSwitched = onExtensionLoaded
    M.onVehicleDestroyed = onExtensionLoaded
    M.onVehicleSpawned = onExtensionLoaded
    M.onClientPostStartMission = onExtensionLoaded
    M.registerVehicle = onExtensionLoaded
    M.onUpdate = onUpdate
    return M
    Code:
    -- ve file
    local function timeStr(time)
        if time < 10 then
            return "0" .. tostring(time)
        else
            return tostring(time)
        end
    end
    
    local time = tonumber(obj:getLastMailbox("timeService"))
        local tempTime
        local timeH
        local timeM
        if time then
            if time < 0.5 then
                tempTime = 24 * time + 12
            else
                tempTime = 24 * time - 12
            end
            timeH = floor(tempTime)
            timeM = floor((tempTime - timeH) * 60)
        end
    
        electrics.values.time = (timeStr(timeH) .. ":" .. timeStr(timeM)) or ":"
     
    • Like Like x 1
  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