playMaker

Author Topic: Random music from array  (Read 1599 times)

PolyMad

  • Hero Member
  • *****
  • Posts: 545
Random music from array
« on: October 01, 2018, 10:10:12 AM »
I want to play a random piece from an array.
So I have populated an array with game objects that are music clips.
I generate a random number, then I get one of these.
But there's no action to play game objects, I can only play "audio clips" (AUDIO PLAY and SET AUDIO CLIP), and I can't find a way to refer to these audio clips after I extract the object from the array, I can't play it.
In fact, for AUDIOCLIP variables, there doesn't seem to be a way to set them up.

The only way I found is to use a RANDOM EVENT and populate all the states manually, not nice really.
« Last Edit: October 01, 2018, 10:21:09 AM by megmaltese »

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7616
    • jinxtergames
Re: Random music from array
« Reply #1 on: October 02, 2018, 01:49:05 AM »
Hi.
You can place audio files directly into an array.

So you would only need 1 object with an audio listener, then if you would need tp play it on a certain position.

You can make a second array for the positions using vector 3, game objects and get/set the position.

Thore

  • Sr. Member
  • ****
  • Posts: 480
Re: Random music from array
« Reply #2 on: October 05, 2018, 08:00:25 PM »
1) create an array in variables pane.
2) Array Type set to Object.
3) Object Type, press 4x U to quickly reach "Unity Engine" and then in the submenu "AudioClip" (not to be confused with AnimationClip).
4) Place Array Get Random action, store result in a new variable (which will be of type AudioClip).
5) Place Play Sound action with the variable of step 4. Or use some other play sound action (most, but not all, allow to pass in clips).

I also have a fix for Play Random Sound No Repeat, attached below. It allows to pass in clips (the standard does not).

Also See:
http://hutonggames.com/playmakerforum/index.php?topic=14217.msg65978;topicseen#msg65978
« Last Edit: October 05, 2018, 08:13:51 PM by Thore »

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7616
    • jinxtergames
Re: Random music from array
« Reply #3 on: October 06, 2018, 12:42:12 AM »
Hi.
Actually it not so good to use play sound,
it creates a gameobject with an audio source each time its triggered.
its ok to use if you need to play only once.

else its better to use audio play, which uses a dedicated audio source.

i usually use a random int or random weighted int then get from array and audio play.