Action denied

Objects/NightLight.lua

NightLight allows you to activate/deactivate objects depending on daytime.

If you want to use this script on your own objects, please mind following hierarchy:

  • The parent object must be equipped with the OnCreateLuaScript calling NightLight:onCreate().
    • The first child object (index 0) will be activated if the light is turned off. If you do not need this, place an empty GameObject at this index.
    • The second child object (index 1) will be activated if the light is turned on. Please put all light sources and emissive objects in here!

Technical remarks

For performance reasons, the script is called by an internal system called JobDistribution, which will call the script only every 100th frame. Therefore, the script can be used on custom objects with almost no performance impact.

You can actually use JobDistribution for customs cripts as well, but in most cases, you will not need this.

  35  
  36  NightLight                  = NightLight or {};
  37  local NightLightClass       = Class(NightLight);

NightLight:onCreate(...)

Initializes a new NightLight by calling NightLight:new(). This will usually be called by the object's onCreate Component.

  40  function NightLight:onCreate(...)
  41      NightLight:new(...);
  42  end;

NightLight:load(id, minOn, maxOn, minOff, maxOff)

Creates a new instance of NightLight and initializes it using the following parameters:

  • id (int): transform id of the parent transform
  • minOn (int) and maxOn (int): the minimum and maximum daytime, at which the NightLight will be turned on
  • minOff (int) and maxOff (int): the minimum and maximum daytime, at which the NightLight will be turned off again

All daytime values (minOn, maxOn, minOff, maxOff) are defined in minutes since the start of the day.

  50  function NightLight:load(id, minOn, maxOn, minOff, maxOff)
  51      -- if nil, use some default values
  52      minOn                           = minOn or (16*60);
  53      maxOn                           = maxOn or (16.5*60);
  54      minOff                          = minOff or (7.0*60);
  55      maxOff                          = maxOff or (7.5*60);
  56  
  57      self.id                         = id;
  58      self.daytimeOff                 = minOff == maxOff and 60 * minOff or math.random(60 * minOff, 60 * maxOff);
  59      self.daytimeOn                  = minOn  == maxOn  and 60 * minOn  or math.random(60 * minOn,  60 * maxOn);
  60      self.currentState               = false;
  61  
  62      setActive(getChildAt(id, 0), self.currentState == false);
  63      setActive(getChildAt(id, 1), self.currentState == true);
  64  
  65      -- JobDistribution will also call the destroy() function if the given object id is destroyed.
  66      JobDistribution:addLowImpactUpdateable(self, id);
  67  end;

NightLight:update(dt)

Is called by JobDistribution every 100th frame. Checks whether the NightLight shall be on or off, and depending on that, the first and second child element will be activated or deactivated if necessary.

  70  function NightLight:update(dt)
  71      local newState;
  72      local daytime                   = g_scenario.environment.daytime;
  73      if self.daytimeOff < self.daytimeOn then
  74          newState                    = daytime < self.daytimeOff or daytime >= self.daytimeOn;
  75      else
  76          newState                    = daytime >= self.daytimeOn and daytime < self.daytimeOff;
  77      end;
  78  
  79      if newState ~= self.currentState then
  80          self.currentState           = newState;
  81          setActive(getChildAt(self.id, 0), self.currentState == false);
  82          setActive(getChildAt(self.id, 1), self.currentState == true);
  83      end;
  84  end;

NightLight:destroy()

Removes the NightLight, e.g. if the object was deleted in the ingame editor.

  87  function NightLight:destroy()
  88      -- remove our object
  89      JobDistribution:removeLowImpactUpdateable(self);
  90  end;

All contents of this page may be used for modding use for Winter Resort Simulator only. Any use exceeding this regulation is not permitted.

Copyright (C) HR Innoways, 2020. All Rights Reserved.