[Resolved] How do I get a string to be read as an object/ function?

I think there’s some simple syntax I’m unaware of. Here’s what I’m trying to do:

I have a number of sound files that are preloaded and called sound1, sound2 … sound10. And I want to play a random sound.

However if I generate a random number and stick it on the end of “sound” to create a string like “sound3”, then that doesn’t work.

What I’m after is the PHP equivalent of ${“sound” . $i}

On a related note, working the other way round how do I get an object or function name treated as a string?

Thanks! [import]uid: 132606 topic_id: 31221 reply_id: 331221[/import]

Concatenating a random number to the string should work - I use it all the time. Put up some code and I should be able to help.

As for converting a function or object into a string I believe you’ll face problems. Lua has tostring(), however this will merely return the hex memory address and not the function/object name - there may be another way but I’m uncertain. [import]uid: 33275 topic_id: 31221 reply_id: 124852[/import]

Hi,

Thanks for your very quick reply. And sorry for not pasting the code to begin with … here’s what I’m trying to do:

[blockcode]
local function playsound(soundtype)
local thesound = nil
local number = math.random(10)
thesound = “sound”…number
audio.play( thesound, { channel=1, loops=2, fadein=50 } )
end
[/blockcode]

If I replace “thesound” in the audio.play statement with “sound7” or whatever, it works. I’m probably doing something very obviously wrong!

Edit: ignore the “soundtype” argument - there’s a check I’ve left out of the function for now. [import]uid: 132606 topic_id: 31221 reply_id: 124854[/import]

You’re loaded sound is an object:

somesound01 = audio.loadSound("somefilename01.wav")  
somesound02 = audio.loadSound("somefilename02.wav")  

You can’t later come along and overwrite that with a string:

thesound = "somesound" .. number  

At that point “thesound” is a string, not an object that you can pass to audio.play.

The solution to this is to use a table:

sounds = {}  
sounds[1] = audio.loadSound("somesound01.wav")  
sounds[2] = audio.loadSound("somesound02.wav")  
sounds[3] = audio.loadSound("somesound03.wav")  
  
number = 1  
  
audio.play(sounds[number])  

[import]uid: 19626 topic_id: 31221 reply_id: 124871[/import]

Hi Rob,

Thank you very much, I haven’t really grasped what tables are *for*. Am I wrong to view them like arrays in PHP?

Anyway, your solution works beautifully, thanks!

Cheers,
Andrew [import]uid: 132606 topic_id: 31221 reply_id: 124876[/import]

Yes, they are very much like arrays in PHP.

They can be either indexed by a number like above or they can can be accessed using keys like PHP:

myTable = {} $myTable = Array();  
myTable["fred"] = "Wilma" $myTable["fred"] = "Wima";  

But they can also be accessed using “dot” notation, like Javascript:

myTable.fred = "Wilma"  

[import]uid: 19626 topic_id: 31221 reply_id: 124880[/import]

Concatenating a random number to the string should work - I use it all the time. Put up some code and I should be able to help.

As for converting a function or object into a string I believe you’ll face problems. Lua has tostring(), however this will merely return the hex memory address and not the function/object name - there may be another way but I’m uncertain. [import]uid: 33275 topic_id: 31221 reply_id: 124852[/import]

Hi,

Thanks for your very quick reply. And sorry for not pasting the code to begin with … here’s what I’m trying to do:

[blockcode]
local function playsound(soundtype)
local thesound = nil
local number = math.random(10)
thesound = “sound”…number
audio.play( thesound, { channel=1, loops=2, fadein=50 } )
end
[/blockcode]

If I replace “thesound” in the audio.play statement with “sound7” or whatever, it works. I’m probably doing something very obviously wrong!

Edit: ignore the “soundtype” argument - there’s a check I’ve left out of the function for now. [import]uid: 132606 topic_id: 31221 reply_id: 124854[/import]

You’re loaded sound is an object:

somesound01 = audio.loadSound("somefilename01.wav")  
somesound02 = audio.loadSound("somefilename02.wav")  

You can’t later come along and overwrite that with a string:

thesound = "somesound" .. number  

At that point “thesound” is a string, not an object that you can pass to audio.play.

The solution to this is to use a table:

sounds = {}  
sounds[1] = audio.loadSound("somesound01.wav")  
sounds[2] = audio.loadSound("somesound02.wav")  
sounds[3] = audio.loadSound("somesound03.wav")  
  
number = 1  
  
audio.play(sounds[number])  

[import]uid: 19626 topic_id: 31221 reply_id: 124871[/import]

Hi Rob,

Thank you very much, I haven’t really grasped what tables are *for*. Am I wrong to view them like arrays in PHP?

Anyway, your solution works beautifully, thanks!

Cheers,
Andrew [import]uid: 132606 topic_id: 31221 reply_id: 124876[/import]

Yes, they are very much like arrays in PHP.

They can be either indexed by a number like above or they can can be accessed using keys like PHP:

myTable = {} $myTable = Array();  
myTable["fred"] = "Wilma" $myTable["fred"] = "Wima";  

But they can also be accessed using “dot” notation, like Javascript:

myTable.fred = "Wilma"  

[import]uid: 19626 topic_id: 31221 reply_id: 124880[/import]