Hiding and unhiding TSStatic based on the time of day

Discussion in 'Programming' started by Day420, Nov 20, 2020.

  1. Day420

    Day420
    Expand Collapse

    Joined:
    May 30, 2019
    Messages:
    5
    Hello, I'd like to know if it's possible to hide or unhide a TSStatic based on the time of day in a level. If it is possible, could I get a tutorial on how it's done?
     
  2. angelo234

    angelo234
    Expand Collapse
    Programmer
    BeamNG Team

    Joined:
    Aug 11, 2017
    Messages:
    540
    I'm not sure if there is an elegant way to hide a TSStatic object, and so the best way I found to "hide" it was by setting its position below the ground so you can't see it. And so the following code snippet would do just that. (I don't know exactly what you want so here's an example to start off on, which "hides" the object during the night (0.25 - 0.75 time) and makes it reappear during the day) Hope this helps! :)

    Edit: I forgot that you need to update the object's collision box after updating its position so the code becomes:

    Code:
    local M = {}
    
    local function onRaceTick(tick)
       local object = scenetree.findObject("name of object")
       local time = core_environment.getTimeOfDay().time
       
       --Its really inefficient to reload collision every single time
       --so its only done once when it switches from day-night, night-day 
       local flag = false
    
       --This just hides an object during the night (0.25 - 0.75)
       if time > 0.25 and time < 0.75 then
          --Set z position to "hide" object
          if flag == false then
             object:setPosition(Point3F(x, y, -9999))
             be:reloadStaticCollision(false)
             flag = true
          end
       else
          --Set object back to original position to "unhide" it  
          if flag == true then
             object:setPosition(Point3F(x, y, z))
             be:reloadStaticCollision(false)
             flag = false
          end
       end
    end
    
    M.onRaceTick = onRaceTick
    
    return M
    
     
    #2 angelo234, Nov 23, 2020
    Last edited: Nov 24, 2020
    • 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