Vehicles/VehicleScripts/WarningSound.lua

This rather simple vehicle script plays a warning sound either if

  • the vehicle is reversing
  • the player has manually activated the warning sound.

Both of these features are only activated if specified in the vehicle's dataTable (depending on dataTable.sounds.warningSound.enabledByKey and dataTable.sounds.warningSound.enableReverse).

Note that this script is not multiplayer-synchronized on purpose to avoid annoying sounds.

  31  WarningSound                        = WarningSound or {};

WarningSound:load(dataTable)

Reads the configuration and loads the warning sound.

  35  function WarningSound:load(dataTable)
  36      self.setWarningSoundState       = VehicleManager:newFunction("setWarningSoundState");
  37  
  38      if dataTable.sounds ~= nil and dataTable.sounds.warningSound ~= nil then
  39          self.warningSound           = self:loadSingleSound(dataTable.sounds.warningSound);
  40          self.warningSoundState      = false;
  41  
  42          self.enableWarningSoundKey      = dataTable.sounds.warningSound.enabledByKey;
  43          self.warningSoundWhenReversing  = dataTable.sounds.warningSound.enableReverse;
  44          self.lastWarningAudioState  = false;
  45      end;
  46  end;

WarningSound:update(dt)

Checks if the player wants to switch the warning sound state. Finally, if starts or stops playing the sound if necessary.

  50  function WarningSound:update(dt)
  51      if not self.isActive then return end;
  52      
  53      if self.warningSound == nil then
  54          return;
  55      end;
  56      
  57      if self:getIsInputActive() and self:getIsLocalPlayerEntered() then
  58          if self.enableWarningSoundKey and InputMapper:getKeyDown(InputMapper.Vehicle_WarningSound) then
  59              self:setWarningSoundState(not self.warningSoundState);
  60              self:playOnOffSound(self.warningSoundState, self.switchSound1, self.switchSound0);
  61          end;
  62      end;
  63  
  64      if self:getIsActive() then
  65          -- check whether we are reversing
  66          local beepActive            = self.warningSoundState or self.isReversing and self.warningSoundWhenReversing;
  67          
  68          if beepActive ~= self.lastWarningAudioState then
  69              if beepActive then
  70                  VehicleMotor.applySpatialBlend(self, self.warningSound);
  71                  AudioSource.play(self.warningSound);
  72              else
  73                  AudioSource.stop(self.warningSound);
  74              end;
  75              self.lastWarningAudioState  = beepActive;
  76          end;
  77      end;
  78  end;

WarningSound:setWarningSoundState(state)

Sets whether the warning sound is active or not, depending on state (bool).

  82  function WarningSound:setWarningSoundState(state)
  83      self.warningSoundState          = state or false;
  84  
  85      if self.warningSound ~= nil then
  86          if state and self.anyPlayerEntered then
  87              VehicleMotor.applySpatialBlend(self, self.warningSound);
  88              AudioSource.play(self.warningSound);
  89          else
  90              AudioSource.stop(self.warningSound);
  91          end;
  92          self.lastWarningAudioState  = state;
  93      end;
  94  end;

WarningSound:onLeave(player, isLocalPlayer)

We always turn off the warning sound if the player leaves the vehicle. Otherwise, it would be too annoying with more than one vehicle.

  98  function WarningSound:onLeave(player, isLocalPlayer)
  99      -- always safely disable warning sound state
 100      if self.warningSound ~= nil then
 101          AudioSource.stop(self.warningSound);
 102          self.lastWarningAudioState  = false;
 103      end;
 104  end;

WarningSound:onEnter(player, isLocalPlayer)

 106  function WarningSound:onEnter(player, isLocalPlayer)
 107      if self.warningSoundState and self.warningSound ~= nil then
 108          VehicleMotor.applySpatialBlend(self, self.warningSound);
 109          AudioSource.play(self.warningSound);
 110          self.lastWarningAudioState  = true;
 111      end;
 112  end;

WarningSound:saveToTable(tbl)

 114  function WarningSound:saveToTable(tbl)
 115      if tbl == nil then return end;
 116  
 117      tbl.warningSoundState           = self.warningSoundState;
 118  end;

WarningSound:loadFromTable(tbl)

 120  function WarningSound:loadFromTable(tbl)
 121      if tbl == nil then return end;
 122  
 123      self:setWarningSoundState(self.warningSoundState);
 124  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.