GML how to play a sound from string name

jobjorgos

Member
Code:
snd_string = 'snd_szymat_echo_party'
audio_play_sound(snd_string,10,true);
Why does the above code not work?
Do I have to convert the string to something else first?
 

jobjorgos

Member
Ah so that first argument has to be an index, I already though something like that was missing.

But strange enough it is still not working for me, I have the following code now:

Code:
snd_string = asset_get_index(snd_szymat_echo_party);
audio_play_sound(snd_string,10,true);
am I still doing anything wrong? :eek:
 
But strange enough it is still not working for me, I have the following code now:

Code:
snd_string = asset_get_index(snd_szymat_echo_party);
audio_play_sound(snd_string,10,true);
asset_get_index() takes a string. It should be
Code:
snd_string = asset_get_index("snd_szymat_echo_party");
The manual included with GMS2 explains what type each argument of built-in functions should be. Make it your first stop when you're having issues; you'll be surprised at how often it will be your last stop!
 

jobjorgos

Member
ah yes, it finaly works now!

The manual included with GMS2 explains what type each argument of built-in functions should be. Make it your first stop when you're having issues; you'll be surprised at how often it will be your last stop!
I knew there are strings, numbers, and all that kind of arguments, but was not aware yet that each function requires specify type or arguments until now. Thats really good to be aware of thanks!
 

rIKmAN

Member
ah yes, it finaly works now!

I knew there are strings, numbers, and all that kind of arguments, but was not aware yet that each function requires specify type or arguments until now. Thats really good to be aware of thanks!
When you are in the code editor, if you middle click the function name it will open the manual on the page for that function where you can check the required arguments, read about the function and also check the code example at the bottom of the page to see how it is used.
 

jobjorgos

Member
When you are in the code editor, if you middle click the function name it will open the manual on the page for that function where you can check the required arguments, read about the function and also check the code example at the bottom of the page to see how it is used.
Amazing, that middle mouse click on functions is really good!
 

FrostyCat

Redemption Seeker
You should make a clearer distinction between resource IDs (which are numbers) and resource names (which are strings).

Either you do this (notice there are NO quotes):
Code:
snd_to_play = snd_szymat_echo_party;
audio_play_sound(snd_to_play, 10, true);
Or this (notice there ARE quotes):
Code:
snd_string = "snd_szymat_echo_party";
audio_play_sound(asset_get_index(snd_string), 10, true);
Unless you are piecing together a name from string fragments, you should use the first form whenever you can.
 

xDGameStudios

GameMaker Staff
GameMaker Dev.
But in this case it looks like something pointless as you can just use:

Code:
audio_play_sound(snd_szymat_echo_party, 10, true);
snd_szymat_echo_party is already the index
 

FrostyCat

Redemption Seeker
But in this case it looks like something pointless as you can just use:

Code:
audio_play_sound(snd_szymat_echo_party, 10, true);
snd_szymat_echo_party is already the index
It may be pointless in this particular case, but there are other cases where storing resource IDs in variables and then referencing them later is a valid technique. That's why it's important to master the distinction and learn how to do this properly.

Here is an example:
Code:
enum ANNOUNCER {
  ALICE,
  BOB,
  CAITLYN
}
ini_open(working_directory + "settings.ini");
global.announcer = int64(ini_read_real("sfx", "announcer", 0));
ini_close();
global.snd_letsgo = [snd_alice_letsgo, snd_bob_letsgo, snd_caitlyn_letsgo];
Code:
audio_play_sound(global.snd_letsgo[global.announcer], 10, false);
 

xDGameStudios

GameMaker Staff
GameMaker Dev.
It may be pointless in this particular case, but there are other cases where storing resource IDs in variables and then referencing them later is a valid technique. That's why it's important to master the distinction and learn how to do this properly.

Here is an example:
Code:
enum ANNOUNCER {
  ALICE,
  BOB,
  CAITLYN
}
ini_open(working_directory + "settings.ini");
global.announcer = int64(ini_read_real("sfx", "announcer", 0));
ini_close();
global.snd_letsgo = [snd_alice_letsgo, snd_bob_letsgo, snd_caitlyn_letsgo];
Code:
audio_play_sound(global.snd_letsgo[global.announcer], 10, false);
Yes I meant in this particular case it seamed a little pointless ;)
 

Yal

šŸ§ *penguin noises*
GMC Elder
The use case most people want to get asset indexes from strings for is for something like
room_goto(asset_get_index("level_" + string(global.level))) ...I'm surprised to see it wasn't the case here.

Three things worth keeping in mind:
  • asset_get_index is slow, consider using a list/array with enums for lookup if you have issues with slowdown (noticeable if you need to get indices hundreds of times per second)
  • if no asset has the correct index, you get a negative number back. Check that the asset exists before using it.
  • the asset might exist but be a different resource type than expected - you can check if it's the right resource type by using sound_get_name (or whichever category you wanna check for) on the index to see if the name matches. If you didn't get the same name back, you've got an index of a different asset type.
 

jobjorgos

Member
But in this case it looks like something pointless as you can just use:

Code:
audio_play_sound(snd_szymat_echo_party, 10, true);
snd_szymat_echo_party is already the index
the reason is because I have a room with a jukebox where players can choose to play a disk of choice out of 100+ unique tracks.
it saves me alot of time if I have to typ all the 100+ tracknames just one time as a variable in 1 object, and then use this variable for all disks.
 

Nidoking

Member
This sounds like a perfect application for a map.
music_map = ds_map_create();
Then, for each song, you can do this:
ds_map_set(music_map, song_name, song_id);
where song_name is the string with the name of the song, and song_id is the index of the song. Then, given a song name as a string, you can use
song_index = ds_map_get(music_map, song_name);
to get the index of the song back.
 
Top