meta data for this page
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
).
29 WarningSound = WarningSound or {};
WarningSound:load(dataTable)
Reads the configuration and loads the warning sound.
33 function WarningSound:load(dataTable) 34 self.setWarningSoundState = VehicleManager:newFunction("setWarningSoundState"); 35 36 if dataTable.sounds ~= nil and dataTable.sounds.warningSound ~= nil then 37 self.warningSound = self:loadSingleSound(dataTable.sounds.warningSound); 38 self.warningSoundState = false; 39 40 self.enableWarningSoundKey = dataTable.sounds.warningSound.enabledByKey; 41 self.warningSoundWhenReversing = dataTable.sounds.warningSound.enableReverse; 42 self.lastWarningAudioState = false; 43 end; 44 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.
48 function WarningSound:update(dt) 49 if not self.isActive then return end; 50 51 if self.warningSound == nil then 52 return; 53 end; 54 55 if self:getIsInputActive() then 56 if self.enableWarningSoundKey and InputMapper:getKeyDown(InputMapper.Vehicle_WarningSound) then 57 self:setWarningSoundState(not self.warningSoundState); 58 self:playOnOffSound(self.warningSoundState, self.switchSound1, self.switchSound0); 59 end; 60 end; 61 62 if self:getIsActive() then 63 local isReversing = self.isReversing; 64 65 -- check whether we are reversing 66 local beepActive = self.warningSoundState or isReversing and self.warningSoundWhenReversing; 67 if beepActive ~= self.lastWarningAudioState then 68 if beepActive then 69 AudioSource.play(self.warningSound); 70 else 71 AudioSource.stop(self.warningSound); 72 end; 73 self.lastWarningAudioState = beepActive; 74 end; 75 end; 76 end;
WarningSound:setWarningSoundState(state)
Sets whether the warning sound is active or not, depending on state
(bool).
80 function WarningSound:setWarningSoundState(state) 81 self.warningSoundState = state or false; 82 83 if self.warningSound ~= nil then 84 if state and self.isEntered then 85 AudioSource.play(self.warningSound); 86 else 87 AudioSource.stop(self.warningSound); 88 end; 89 self.lastWarningAudioState = state; 90 end; 91 end;
WarningSound:onLeave()
We always turn off the warning sound if the player leaves the vehicle. Otherwise, it would be too annoying with more than one vehicle.
95 function WarningSound:onLeave() 96 -- always safely disable warning sound state 97 if self.warningSound ~= nil then 98 AudioSource.stop(self.warningSound); 99 self.lastWarningAudioState = false; 100 end; 101 end;
WarningSound:onEnter()
103 function WarningSound:onEnter() 104 if self.warningSoundState and self.warningSound ~= nil then 105 AudioSource.play(self.warningSound); 106 self.lastWarningAudioState = true; 107 end; 108 end;
WarningSound:saveToTable(tbl)
110 function WarningSound:saveToTable(tbl) 111 if tbl == nil then return end; 112 113 tbl.warningSoundState = self.warningSoundState; 114 end;
WarningSound:loadFromTable(tbl)
116 function WarningSound:loadFromTable(tbl) 117 if tbl == nil then return end; 118 119 self:setWarningSoundState(self.warningSoundState); 120 end;
Copyright
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.