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                      networkId       = #self.masterAttachers + 1,
  47                      type            = v.type or "",
  48                      isAttached      = false,
  49                      vehicle         = self,
  50                  };
  51                  table.insert(self.masterAttachers, attacher);
  52              end;
  53          end;
  54      end;
  55  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.

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

Attacher:attach(master, slave, noEvent)

Attach a slave vehicle slave (table with the vehicle instance) to the master attacher master (attacher table). noEvent (bool) specifies whether the multiplayer event shall be suppressed.

The corresponding network events is EventAttachVehicle (to be found in EventAttacherSystem).

 100  function Attacher:attach(master, slave, noEvent)
 101      if master == nil or slave == nil then return end;
 102  
 103      if g_isClient and not noEvent then
 104          EventAttachVehicle:send(master, slave);
 105          return;
 106      end;
 107  
 108      if master.isAttached or slave.isAttached then return end;
 109  
 110      master.isAttached               = true;
 111      slave.isAttached                = true;
 112      master.attachedSlave            = slave;
 113      slave.attachedMaster            = master;
 114  
 115      FixedAttacher.attach(master.masterId, slave.slaveParentId, slave.slaveId);
 116      slave.vehicle:onAttach(master.vehicle, master);
 117  
 118      if g_isServer then
 119          EventAttachVehicle:send(master, slave);
 120      end;
 121  end;

Attacher:detach(master, noEvent)

Detaches the slave attacher that is currently attached to master (attacher table). noEvent (bool) specifies whether the multiplayer event shall be suppressed.

The corresponding network events is EventDetachVehicle (to be found in EventAttacherSystem).

 127  function Attacher:detach(master, noEvent)
 128      if master == nil or not master.isAttached or master.attachedSlave == nil then return end;
 129  
 130      if g_isClient and not noEvent then
 131          EventDetachVehicle:send(master);
 132          return;
 133      end;
 134  
 135      local slave                     = master.attachedSlave;
 136  
 137      if slave ~= nil then
 138          slave.isAttached            = false;
 139          slave.attachedMaster        = nil;
 140  
 141          FixedAttacher.detach(master.masterId);
 142          slave.vehicle:onDetach(master.vehicle, master);
 143      end;
 144  
 145      master.isAttached               = false;
 146      master.attachedSlave            = nil;
 147  
 148      if g_isServer then
 149          EventDetachVehicle:send(master);
 150      end;
 151  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.