[Solved] Understanding Variables - New to Corona

–Some Sample Code from Current Project - abbreviated

–This Works for me perfectly

-declare some variables- two sounds
local Sound2 = audio.loadSound(“2.wav”)
local Sound3 = audio.loadSound( “3.wav” )
local SoundMatch = Sound2

–Play sound
audio.play ( SoundMatch )

– this plays Sound2 Perfectly or “2.wav” file


–This does not work for me, unsure why

-declare some variables- two sounds
local Sound2 = audio.loadSound(“2.wav”)
local Sound3 = audio.loadSound( “3.wav” )
local SoundMatch = Sound2

–Play Sound
SoundMatch = “Sound” … 3
audio.play ( SoundMatch )

– This plays nothing! I can see that SoundMatch is now Sound3 so why doesn’t it play.

Thanks for the Help. Haven’t programmed in 20 years and just starting to relearn everything again.

[import]uid: 115952 topic_id: 20108 reply_id: 320108[/import]

SoundMatch = “Sound” … 3
by this line you assign a string “Sound 3” to variable SoundMatch, so it will not play anything, because string replaced reference to audio.loadSound [import]uid: 16142 topic_id: 20108 reply_id: 78557[/import]

Hey davidapezzato8!

You have a couple of problems in your code -
When you use SoundsMatch = “Sound” … 3 you are actually turning it into a string.
Also, in order to combine numbers to strings you need to use the “tostring( 2 )” function.

You can use a table instead -

local sounds = {};  
sounds['sound2'] = audio.loadSound( "2a.wav" );  
sounds['sound3'] = audio.loadSound( "3a.wav" );  
  
-- Play Sound  
SoundMatch = 'sound' .. tostring(2);  
audio.play(sounds[SoundMatch]);  

Hope this helps.

BTW - don’t feel bad, these are little bugs that you learn from practice. [import]uid: 13553 topic_id: 20108 reply_id: 78560[/import]

Guys -thanks for the help.

Shararzrihen - I implemented your code and it worked just perfectly. Thank you. Time for me to go read up on a few things.

Appreciate the quick response!
[import]uid: 115952 topic_id: 20108 reply_id: 78567[/import]

You’re welcome in our community) [import]uid: 16142 topic_id: 20108 reply_id: 78573[/import]