Allow sound.play to pass index of sound, by default play first sound

sound.play currently requires a string to be passed ( the name of the sound slot ).

I wanted a generic way to just play the first sound attached to the sound. If the user renamed the sound ( which they are taught to do with all assets, to make them more meaningful ), then just doing this doesn’t work.

entity.sound.play(“Slot 1”);

It would be great if sound.play allowed for an index, so this would work…

entity.sound.play(0);

…and to extend that further, have entity.sound.play() use by default the first sound slot, if it exists.

For anyone looking for an alternative solution, the code below works to play the first

entity.sound.play(Object.keys(entity.sound.slots)[0]);

This could be a case where, if many users might only attach a sound to an object with one sound clip, having the ability to just call sound.play() to play that one sound would make life a bit easier.

I’m just flagging this again as a useful and sensible default bahaviour.

If someone does sound.play(), detect that the first parameter is undefined and just play the first sound attached - with a check to see if there is a sound there of course :slight_smile: - as this is very likely what a new, and experienced, dev would want most of the time.

1 Like

It’s not an unreasonable request :thinking: https://github.com/playcanvas/engine/issues/3095

2 Likes