COMMUNITY FORUM

Script for more horns

Forum Overview >> Scripting

CategoryScripting
Created26.08.2024 17:25


Carlos Lopez (CarlosDJ) 26.08.2024 17:25
Hi, I have some air horn melodies and I wanted to add them to my tractor, but since it only allows me to use one, I had to make a script. The script works, but when I try to play the sound it doesn't play anything and nothing appears in the log. Can anyone help me?

Script:

PlaySoundMod = {}
PlaySoundMod.__index = PlaySoundMod

function PlaySoundMod:new()
local instance = setmetatable({}, PlaySoundMod)
instance:init()
return instance
end

function PlaySoundMod:init()
self.soundFiles = {
"sounds/Basuri_honk1.ogg", "sounds/Basuri_honk2.ogg", "sounds/Basuri_honk3.ogg",
"sounds/Basuri_honk4.ogg", "sounds/Basuri_honk5.ogg", "sounds/Basuri_honk6.ogg",
"sounds/Basuri_honk7.ogg", "sounds/Basuri_honk8.ogg", "sounds/Basuri_honk9.ogg",
"sounds/Basuri_honk10.ogg", "sounds/Basuri_honk11.ogg", "sounds/Basuri_honk12.ogg",
"sounds/Basuri_honk13.ogg", "sounds/Sandstorm_Honk.ogg"
}
self.currentSoundIndex = 1
end

function PlaySoundMod:registerEventListeners(vehicleType)
SpecializationUtil.registerEventListener(vehicleType, "onUpdate", self)
end

function PlaySoundMod:onUpdate(dt)
if self:isActionPressed("PLAY_SOUND_ACTION") then
self:playSound()
end
if self:isActionPressed("CHANGE_SOUND_ACTION") then
self:changeSound()
end
end

function PlaySoundMod:isActionPressed(actionName)
if InputBinding.hasAction(actionName) then
return InputBinding.isPressed(actionName)
else
print("Error: Acción no definida: " .. actionName)
return false
end
end

function PlaySoundMod:playSound()
local soundFile = self.soundFiles[self.currentSoundIndex]
if soundFile then
g_soundManager:playSampleFromFile(soundFile)
else
print("Error: Archivo de sonido no encontrado: " .. soundFile)
end
end

function PlaySoundMod:changeSound()
self.currentSoundIndex = (self.currentSoundIndex % #self.soundFiles) + 1
print("Sound changed to index: " .. self.currentSoundIndex)
end

local playSoundMod = PlaySoundMod:new()

addModEventListener(playSoundMod)


Note: Log in to post. Create a new account here.