Vehicles/VehicleScripts/Attacher.lua

Attacher is a vehicle script that is closely linked to Attachable. While the Attacher script (“master attacher”) is activated on the vehicle that is controlled by the player (i.e. a vehicle that has the Seats vehicle script activated), the Attachable script (“slave attacher”) can be activated on any vehicle (e.g. snow cannons, implements or similar things).

  26  Attacher                            = Attacher              or {};

Attacher:load(dataTable)

Every master vehicle can have multiple masterAttachers, , i.e. it can have multiple slaves attached to itself.

  30  function Attacher:load(dataTable)
  31      self.attach                     = VehicleManager:newFunction("attach");
  32      self.detach                     = VehicleManager:newFunction("detach");
  33  
  34      self.attachedTo                 = nil;
  35  
  36      self.masterAttachers            = {};
  37  
  38      if dataTable.masterAttachers ~= nil then
  39          for k, v in pairs(dataTable.masterAttachers) do
  40              -- load them
  41              local id                = getChild(self.id, v.index or "");
  42  
  43              if id ~= 0 then
  44                  local attacher      = {
  45                      masterId        = id,
  46                      type            = v.type or "",
  47                      isAttached      = false,
  48                      vehicle         = self,
  49                  };
  50                  table.insert(self.masterAttachers, attacher);
  51              end;
  52          end;
  53      end;
  54  end;

Attacher:update(dt)

Checks whether there is an attachable in range of this attacher, and if yes, enables the player to attach it. Also, if any attachable is connected to the vehicle right now, it can be detached.

  58  function Attacher:update(dt)
  59      if self:getIsInputActive() then
  60          local canAttachAny          = false;
  61  
  62          for k, master in pairs(self.masterAttachers) do
  63              if not master.isAttached then
  64                  -- allow for coupling
  65                  local slaveAttacher     = Attachable.findAttachableInRange(master);
  66  
  67                  if slaveAttacher ~= nil then
  68                      canAttachAny        = true;
  69                      g_GUI:addKeyHint(InputMapper.Vehicle_Attach, l10n.get("Vehicle_Attach"));
  70  
  71                      if InputMapper:getKeyDown(InputMapper.Vehicle_Attach) then
  72                          -- attach
  73                          self:attach(master, slaveAttacher);
  74                      end;
  75                      break;
  76                  end;
  77              end;
  78          end;
  79  
  80          -- allow for decoupling of vehicles as well (but only if we can't attach one)
  81          if not canAttachAny then
  82              for k, master in pairs(self.masterAttachers) do
  83                  if master.isAttached then
  84                      -- allow for decoupling
  85                      if InputMapper:getKeyDown(InputMapper.Vehicle_Attach) then
  86                          self:detach(master);
  87                          break;
  88                      end;
  89                  end;
  90              end;
  91          end;
  92      end;
  93  end;

Attacher:attach(master, slave)

Attach a slave vehicle slave (table with the vehicle instance) to the master attacher master (attacher table).

  97  function Attacher:attach(master, slave)
  98      if master == nil or slave == nil then return end;
  99      if master.isAttached or slave.isAttached then return end;
 100  
 101      master.isAttached               = true;
 102      slave.isAttached                = true;
 103      master.attachedSlave            = slave;
 104      slave.attachedMaster            = master;
 105  
 106      FixedAttacher.attach(master.masterId, slave.slaveParentId, slave.slaveId);
 107      slave.vehicle:onAttach(master.vehicle, master);
 108  end;

Attacher:detach(master)

Detaches the slave attacher that is currently attached to master (attacher table).

 112  function Attacher:detach(master)
 113      if master == nil or not master.isAttached or master.attachedSlave == nil then return end;
 114  
 115      local slave                     = master.attachedSlave;
 116  
 117      master.isAttached               = false;
 118      slave.isAttached                = false;
 119      master.attachedSlave            = nil;
 120      slave.attachedMaster            = nil;
 121  
 122      FixedAttacher.detach(master.masterId);
 123      slave.vehicle:onDetach(master.vehicle, master);
 124  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.