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                      networkId       = #self.slaveAttachers + 1,
  70                      slaveParentId   = self.mainId,
  71                      type            = v.type or "",
  72                      isAttached      = false,
  73                      vehicle         = self,
  74                  };
  75                  table.insert(self.slaveAttachers, attacher);
  76                  Attachable.attachers[id]    = attacher;
  77              end;
  78          end;
  79      end;
  80  end;

Attachable:onAttach(masterVehicle, masterAttacher)

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

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

Attachable:onDetach(masterVehicle, masterAttacher)

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

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

Attachable:destroy()

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

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