LUADOC - Farming Simulator 22

Script v1_7_1_0

Engine v1_7_1_0

Foundation Reference

GuiSoundPlayer

Description
This class loads known GUI sound samples as non-spatial samples to be played in the GUI (menu and HUD).
Functions

loadSounds

Description
Load GUI sound samples from definitions.
Definition
loadSounds()
Code
100function GuiSoundPlayer:loadSounds(sampleDefinitionXmlPath)
101 local samples = {}
102
103 local xmlFile = loadXMLFile("GuiSampleDefinitions", sampleDefinitionXmlPath)
104 if xmlFile ~= nil and xmlFile ~= 0 then
105
106 for _, key in pairs(GuiSoundPlayer.SOUND_SAMPLES) do
107 if key ~= GuiSoundPlayer.SOUND_SAMPLES.NONE then
108 local sample = self.soundManager:loadSample2DFromXML(xmlFile, GuiSoundPlayer.SOUND_SAMPLE_DEFINITIONS_XML_ROOT, key, "", 1, AudioGroup.GUI)
109 if sample ~= nil then
110 local sampleList = {}
111 samples[key] = sampleList
112
113 -- Build a whole queue so we can play multiple sounds at once
114 table.insert(sampleList, sample)
115 for i = 2, GuiSoundPlayer.NUM_SAMPLES_PER_SOUND do
116 table.insert(sampleList, self.soundManager:cloneSample2D(sample))
117 end
118
119 else
120 print("Warning: Could not load GUI sound sample [" .. tostring(key) .. "]")
121 end
122 end
123 end
124
125 delete(xmlFile)
126 end
127
128 return samples
129end

new

Description
Create a new GuiSoundPlayer instance.
Definition
new(table soundManager)
Arguments
tablesoundManagerSoundManager reference
Code
80function GuiSoundPlayer.new(soundManager)
81 local self = setmetatable({}, GuiSoundPlayer_mt)
82
83 self.soundManager = soundManager
84 self.soundSamples = self:loadSounds(GuiSoundPlayer.SOUND_SAMPLE_DEFINITIONS_PATH) -- name -> sample
85
86 return self
87end

playSample

Description
Play a GUI sound sample identified by name. The sample must have been loaded when the GUI was created.
Definition
playSample(string sampleName)
Arguments
stringsampleNameName of the sample to play, use one of the identifiers in GuiSoundPlayer.SOUND_SAMPLES.
Code
135function GuiSoundPlayer:playSample(sampleName)
136 if sampleName == GuiSoundPlayer.SOUND_SAMPLES.NONE then
137 return
138 end
139
140 local sampleList = self.soundSamples[sampleName]
141
142 if sampleList ~= nil then
143 if sampleList.lastTime ~= nil and g_time - sampleList.lastTime < GuiSoundPlayer.SAMPLE_REPLAY_TIMEOUT then
144 return
145 end
146
147 -- Get first free
148 local sample
149 for i = 1, #sampleList do
150 if not self.soundManager:getIsSamplePlaying(sampleList[i]) then
151 sample = sampleList[i]
152 break
153 end
154 end
155
156 if sample ~= nil then
157 self.soundManager:playSample(sample)
158 sampleList.lastTime = g_time
159 end
160 else
161 print("Warning: Tried playing GUI sample [" .. tostring(sampleName) .. "] which has not been loaded.")
162 end
163end