Distributing audio.loadSound (over time) to enable smoother scene changes

Hi

I am trying to replace the audio.loadSound function with a new one to delay the audio loading (using timer.performWithDelay. The following code doesn´t work but demonstrates what I am trying to do (its just an example):

-- define some globals that can pass parameters  
auli=0  
posi=0  
auls= {}  
poso={}  
-- function to distribute audio loading over time (to prevent transitions from hanging)  
function audioloadSound(auli)  
 -- audioloadSound(sound,handler)  
 auls[#auls+1]=auli  
 poso[#poso+1]=timer.performWithDelay ( #auls\*100,  
 function(event)  
 local param=event.source.poso  
 poso[param]=audio.loadSound(auls[1])  
 print("loaded "..auls[1].." in "..param)  
 table.remove ( auls,1 )  
 return poso[param]  
 end , 1)  
 poso[#poso].poso=#poso  
 print("called:"..#poso)  
 return poso[#poso]  
end  
  
mySound[1]=audioloadSound("explosion.wav")  
mySound[2]=audioloadSound("popsound.wav")  
mySound[3]=audioloadSound("bird.wav")  

The main problem here is that the audioloadSound function needs to pass back a parameter which is the sound handle/reference. When the function is finished, the audio isn´t loaded yet, so there is no handler to pass. My though was that “return poso[#poso]” would return a reference to the variable which later would be updated as “return poso[param]” is executed, but it is not happening.

So the question is: Is there a way to pass the variable reference “mySound[1]” to the function so that I can update that variable on a later time? Do I need a timer event to do that or can it be done in the loading function? I rather not hardcode any variable, so a way to directly reference handles (or copy the reference) would be nice…

Hope anyone can help.
[import]uid: 191865 topic_id: 35748 reply_id: 335748[/import]