Objects/ToggleObject.lua

ToggleObjects are used whenever you want to enable/disable an object depending on a control switch or a control button.

  24  ToggleObject                        = ToggleObject or {};
  25  local ToggleObjectClass             = Class(ToggleObject); 

ToggleObject:load(switchId, objectId, inverseId, switchName, switchOff, switchOn, isButton)

Gets called by onCreate events and initializes a new toggle object using the following parameters:

  • switchId (transform id): Object id of the control switch or control button
  • objectId (transform id): Object id of the object that shall be enabled/disabled.
  • inverseId (transform id): Object id of the object that shall be disabled/enabled inversely to objectId (useful if you want to switch between two different states of a single object)
  • switchName (string): Name of the switch

If you want to initialize a control switch, you can use following parameters:

  • switchOff (string): Labelling of the control switch position, at which the object will be invisible
  • switchOn (string): Labelling of the control switch position, at which the object will be visible
  • isButton (optional bool): must be nil or false

If you want to initialize a control button, following parameters are used instead:

  • switchOff: unused (i.e. nil)
  • switchOn: unused (i.e. nil)
  • isButton (bool): must be true
  45  function ToggleObject:load(switchId, objectId, inverseId, switchName, switchOff, switchOn, isButton)
  46      -- first store the main id
  47      self.switchId                   = switchId;
  48      self.objectId                   = objectId;
  49      self.inverseId                  = inverseId;
  50      
  51      self.textOn                     = textOn;
  52      self.textOff                    = textOff;
  53      
  54      self.state                      = false;
  55      self.isButton                   = isButton;
  56      
  57      if self.isButton then
  58          -- this is a button -> in this case we only need switchName
  59          ControlElement.new(self.switchId);
  60          ControlElement.setCallback(self.switchId,   1, function()
  61              self:setState(not self.state);
  62          end);
  63          ControlElement.setName(self.switchId,       l10n.getDollar(switchName or "Unknown"));
  64          
  65      else
  66          -- this is a control switch
  67      
  68          ControlSwitch.new(self.switchId);
  69          ControlSwitch.addPosition(self.switchId,    0,  l10n.getDollar(switchOff or "Unknown"));
  70          ControlSwitch.addPosition(self.switchId,    60, l10n.getDollar(switchOn or "Unknown"));
  71          ControlSwitch.setCallback(self.switchId,    function(pos) self:setState(pos == 1); end);
  72          ControlSwitch.setName(self.switchId,        l10n.getDollar(switchName or "Unknown"));
  73      end;
  74      
  75      self:setState(false, true);
  76  
  77      -- prepare loading/saving
  78      local x,y,z                     = getWorldPosition(switchId);
  79      self.savegameKey                = "toggleObject" .. string.format(":%s @ (%.4f, %.4f, %.4f)", switchName or "unnamed", x,y,z);
  80  
  81      -- this will call our destroy event
  82      g_scenario:addObjectAndLoad(self, switchId, self.savegameKey);
  83  end;

ToggleObject:destroy()

Destroys the lua instance if the object is destroyed (e.g. deleted using the in-game editor).

  87  function ToggleObject:destroy()
  88      -- as always: unregister from objects
  89      if g_scenario ~= nil then
  90          g_scenario:removeObject(self, self.savegameKey);
  91      end;
  92  
  93      if self.isButton then
  94          ControlElement.destroy(self.switchId);
  95      else
  96          ControlSwitch.destroy(self.switchId);
  97      end;
  98  end;

ToggleObject:saveToTable(savegame)

Saves the information from this toggle object into the savegame table.

 102  function ToggleObject:saveToTable(savegame)
 103      if savegame[self.savegameKey] ~= nil then
 104          print("Warning while saving to table: Multiple ToggleObjects are using the key " .. tostring(self.savegameKey) .. ". All of them will have the same state when loading the savegame. Please avoid ToggleObjects that are named the same and placed at the same spot.");
 105          return;
 106      end;
 107      savegame[self.savegameKey]      = self.state;
 108  end;

ToggleObject:loadFromTable(savegame)

Retrieves information from the savegame.

 112  function ToggleObject:loadFromTable(savegame)
 113      if savegame == nil then return end;
 114  
 115      local state                     = savegame[self.savegameKey];
 116      if state ~= nil then
 117          self:setState(state, true);
 118      end;
 119  end;

ToggleObject:setState(state, noEvent)

Sets the state of the toggle object to either true or false, depending on state. noEvent must be set to true when the function is called by a MP event.

 123  function ToggleObject:setState(state, noEvent)
 124      self.state                      = state or false;
 125      
 126      if self.objectId ~= nil then    setActive(self.objectId, self.state);       end;
 127      if self.inverseId ~= nil then   setActive(self.inverseId, not self.state);  end;
 128  
 129      if not noEvent then
 130          EventToggleObject:send(self, state);
 131      end;
 132  
 133      -- adjust switch position in case it was not yet correct (but ignore callbacks)
 134      if not self.isButton then
 135          ControlSwitch.setPosition(self.switchId, state and 1 or 0, false, true);
 136      end;
 137  end;

ToggleObject:writeResync()

Resynchronizes all variables when a new player joins the game. The data sent by writeResync will be received by readResync.

 141  function ToggleObject:writeResync()
 142      streamWriteBool(self.state);
 143  end;

ToggleObject:readResync()

Resynchronizes all variables when a new player joins the game. The data sent by writeResync will be received by readResync.

 147  function ToggleObject:readResync()
 148      local state                     = streamReadBool();
 149      self:setState(state, true);
 150  end;

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

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