Vehicles/VehicleScripts/Attachable.lua

Attachable is a vehicle script that is closely linked to Attacher. 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).

Note that for visual reasons, the vehicles are not connected by a physical joint. Instead, the slave vehicle's collision will be disabled and it will be carried by the master vehicle. This is necessary to avoid strange physics behaviour.

  26  Attachable                          = Attachable                or {};
  27  Attachable.attachers                = Attachable.attachers      or {};
  28  Attachable.attachRangeSqr           = 1.5*1.5;
  29  

Attachable.findAttachableInRange(master)

Called by Attacher:update() to show the player whether an attachable is in range.

  33  function Attachable.findAttachableInRange(master)
  34      if master == nil or master.masterId == nil then return end;
  35  
  36      for k, slave in pairs(Attachable.attachers) do
  37          if slave.type == master.type and not slave.isAttached then
  38              local sqrDist           = Utils.sqrDistanceBetween(master.masterId, slave.slaveId);
  39  
  40              if sqrDist < Attachable.attachRangeSqr then
  41                  return slave;
  42              end;
  43          end;
  44      end;
  45  
  46      -- no attacher found
  47      return nil;
  48  end;

Attachable:load(dataTable)

Loads the vehicle's configuration on slave attachers. Note that similar to Attacher, multiple slaveAttachers can be specified per vehicle.

  52  function Attachable:load(dataTable)
  53      self.onAttach                   = VehicleManager:newFunction("onAttach");
  54      self.onDetach                   = VehicleManager:newFunction("onDetach");
  55  
  56      self.attacherMasterVehicle      = nil;
  57      self.masterAttacher             = nil;
  58  
  59      self.slaveAttachers             = {};
  60  
  61      if dataTable.slaveAttachers ~= nil then
  62          for k, v in pairs(dataTable.slaveAttachers) do
  63              -- load them
  64              local id                = getChild(self.id, v.index or "");
  65  
  66              if id ~= 0 then
  67                  local attacher      = {
  68                      slaveId         = id,
  69                      slaveParentId   = self.mainId,
  70                      type            = v.type or "",
  71                      isAttached      = false,
  72                      vehicle         = self,
  73                  };
  74                  table.insert(self.slaveAttachers, attacher);
  75                  Attachable.attachers[id]    = attacher;
  76              end;
  77          end;
  78      end;
  79  end;

Attachable:onAttach(masterVehicle, masterAttacher)

Is called when the vehicle is attached by a master vehicle.

  83  function Attachable:onAttach(masterVehicle, masterAttacher)
  84      self.attacherMasterVehicle      = masterVehicle;
  85      self.masterAttacher             = masterAttacher;
  86  end;

Attachable:onDetach(masterVehicle, masterAttacher)

Is called when the vehicle is detached from a master vehicle.

  90  function Attachable:onDetach(masterVehicle, masterAttacher)
  91      self.attacherMasterVehicle      = nil;
  92      self.masterAttacher             = nil;
  93  end;

Attachable:destroy()

Detaches the vehicle in case the slave vehicle is sold while being attached to a master vehicle.

 106  function Attachable:destroy()
 107      if self.attacherMasterVehicle ~= nil and self.masterAttacher ~= nil then
 108          self.attacherMasterVehicle:detach(self.masterAttacher);
 109      end;
 110      
 111      for k, v in pairs(self.slaveAttachers) do
 112          -- load them
 113          Attachable.attachers[v.slaveId] = nil;
 114      end;
 115  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.