Objects/AnchorPoint.lua

The AnchorPoint class stores information on anchor points, which any vehicles equipped with a RopeWinch can be attached to. The script is mainly used by RopeWinch and VehicleAnchor.

  25  AnchorPoint                     = AnchorPoint or {};
  26  local AnchorPointClass          = Class(AnchorPoint);
  27  
  28  -- set this variable for coupling to nil (commented out for hot reload reasons)
  29  --AnchorPoint.activeVehicle     = nil;

AnchorPoint.onCreate(id)

Creates a new AnchorPoint instance for a static object with id id (int).

  33  function AnchorPoint.onCreate(id)
  34      AnchorPoint:new(getChildAt(id, 0), id);
  35  end;

AnchorPoint.onCreatePlayer(id, player)

Internally called by an onCreate script attached to the player.

Do not call this from any mod.

  40  function AnchorPoint.onCreatePlayer(id, player)
  41      AnchorPoint:new(id, nil, true, player);
  42  end;

AnchorPoint:load(id, btnId, isPlayer, player)

Loads a newly created AnchorPoint instance using following arguments:

  • id (int) is the transform id of the rope target, i.e. the rope will be connected to here.
  • btnId (int) is the transform id of the control element. The player can click this control element to attach any active rope to the anchor point.
  • isPlayer (bool) should usually be false.
  • playerName (string) name of the player that owns the anchor point.
  50  function AnchorPoint:load(id, btnId, isPlayer, player)
  51      self.ropeTargetId           = id;
  52      self.controlButtonId        = btnId;
  53  
  54      self.connectedVehicle       = nil;
  55      self.player                 = player;
  56  
  57      if self.controlButtonId == nil and not isPlayer then
  58          print("Error while loading AnchorPoint: no control button id specified, rope target id is " .. tostring(id) .. ". Loading skipped");
  59          return;
  60      end;
  61      if self.controlButtonId ~= nil then
  62          ControlElement.new(self.controlButtonId);
  63  
  64          ControlElement.setCallback(self.controlButtonId, 1,
  65              function()
  66                  self:buttonCallback();
  67              end
  68          );
  69          ControlElement.setActiveCallback(self.controlButtonId,
  70              function()
  71                  return self:getControlElementActive();
  72              end
  73          );
  74  
  75          ControlElement.setName(self.controlButtonId, l10n.get("WinchAnchor_Attach"));
  76  
  77          -- register anchor point
  78          local x,y,z                 = getWorldPosition(self.ropeTargetId);
  79      
  80          self.anchorPointName        = ("anchor" .. string.format(":%s @ (%.4f, %.4f, %.4f)", getName(self.ropeTargetId), x,y,z));
  81  
  82          local origName              = self.anchorPointName;
  83  
  84          -- find a name that is not yet blocked
  85          for i=0, 100, 1 do
  86              if RopeWinch.anchorPoints[self.anchorPointName] == nil then
  87                  break;
  88              else
  89                  self.anchorPointName    = origName .. "(" .. i .. ")";
  90              end;
  91          end;
  92          
  93      else
  94          -- only player is allowed not to pass a controlButton
  95          self.anchorPointName        = self.player;--"player";
  96      end;
  97  
  98      -- ropewinch does not call delete function, therefore we add it manually
  99      RopeWinch.registerAnchorPoint(self.anchorPointName, self);
 100      addDestroyListener(id, self);
 101  end;

AnchorPoint:getControlElementActive()

Returns whether the anchor point's control element shall be activated. The element cannot be clicked if this returns false.

 104  function AnchorPoint:getControlElementActive()
 105      if self.connectedVehicle ~= nil then
 106          if AnchorPoint.activeVehicle ~= nil then
 107              -- we have a vehicle attached, but the player is also trying to attach a vehicle
 108              -- => block this
 109              return false;
 110          end;
 111          return self.connectedVehicle.getMayRopeWinchDisconnect == nil or self.connectedVehicle:getMayRopeWinchDisconnect();
 112      end;
 113      
 114      -- otherwise we're prepared for attaching
 115      return AnchorPoint.activeVehicle ~= nil;
 116  end;

AnchorPoint:buttonCallback()

Is called if the player has clicked the control element (i.e. we can assume that AnchorPoint:getControlElementActive() has returned true).

 119  function AnchorPoint:buttonCallback()
 120      if self.connectedVehicle ~= nil then
 121          self:disconnect();
 122  
 123      elseif AnchorPoint.activeVehicle ~= nil then
 124          self:attach(AnchorPoint.activeVehicle);
 125          AnchorPoint.activeVehicle   = nil;
 126      end;
 127  end;

AnchorPoint:attach(vehicle)

Attaches the vehicle vehicle (table of vehicle instance) to this anchor point.

 130  function AnchorPoint:attach(vehicle)
 131      if vehicle == nil or vehicle.attachWinchToAnchorPoint == nil then return end;
 132  
 133      self.connectedVehicle       = vehicle;
 134      self.connectedVehicle:attachWinchToAnchorPoint(self.anchorPointName);
 135  
 136      if self.controlButtonId ~= nil then
 137          ControlElement.setName(self.controlButtonId, l10n.get("WinchAnchor_Disconnect"));
 138      end;
 139  end;

AnchorPoint:disconnect(noPlayerAttaching)

Disconnects/detaches any active vehicle from this anchor point. If noPlayerAttaching is nil or false, the rope will be attached to the player controller. Otherwise (in case of true) the rope will not be connected to the player.

 142  function AnchorPoint:disconnect(noPlayerAttaching)
 143      if self.connectedVehicle == nil then return end;
 144      local connectedVehicle      = self.connectedVehicle;
 145      
 146      -- decouple that vehicle
 147      if not noPlayerAttaching then
 148          local player            = g_scenario.player;
 149          
 150          connectedVehicle:attachWinchToAnchorPoint(player);
 151      else
 152          connectedVehicle:attachWinchToAnchorPoint(nil);
 153      end;
 154  
 155      -- and set it as active vehicle
 156      AnchorPoint.activeVehicle   = connectedVehicle;
 157  
 158      self.connectedVehicle       = nil;
 159      if self.controlButtonId ~= nil then
 160          ControlElement.setName(self.controlButtonId, l10n.get("WinchAnchor_Attach"));
 161      end;
 162  end;

AnchorPoint:destroy()

Is called if the anchor point is destroyed, e.g. if the prefab is deleted in ingame editor.

 165  function AnchorPoint:destroy()
 166      self:disconnect(true);
 167      RopeWinch.unregisterAnchorPoint(self.anchorPointName);
 168  
 169      if self.controlButtonId ~= nil then
 170          ControlElement.destroy(self.controlButtonId);
 171      end;
 172  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.