How do I stop an audio tag inside a function

0 favourites
  • 7 posts
From the Asset Store
94 Inside buildings views - Isometric view - PNG transparent - 2048x2048
  • Hi everyone,

    I'm currently working on a music game for which I am using large sound files featuring many 2 seconds audio cues that I've put one after another.

    So to be clear the audio file is something like:

    0-2 sec : sound / 2-3 sec : silence / 3-5 sec : another sound / 5-6 sec : silence ... etc ...

    This is necessary to reduce the number of assets so that my server could play the game correctly.

    Now I need to create a function which :

    1) Plays the sound at time (param 0) with tag (Param 1)

    (no problem for this part)

    2) After 2.5 seconds, stops the sound playing at tag (param 1) (or else the large sound file continues playing other sounds).

    Here it becomes more problematic. I can do it as long as there is only one sound playing at a time. The problem is that I need up to 4 sounds to be played at the same time with this function from the same file, and as soon as a second sound is playing, the first sound played won't stop. Of course I'm giving different tags here, I even tried to add a random value in the tag I'm giving.

    I tried many things, such as using a return value with the tag of the audio to stop, and then stopping the audio in the event which called the function instead of in the function itself. I also created a stop function called inside the play function, but in both cases the result is not better.

    Is there any way to achieve this?

    Thanks for any help!

  • Dont understand why you want it in big chunks, but guess you have your reasons. It complicates things.

    Using a 'wait' that way in a functions is impossible. 'Function.param(n)' has a value and a meaning only inside the function. Postpone an action with 4 seconds, brings that action way out of that function, hence, the meaning for 'Function.param(n)' is gone in the wind.

    O would say, bring in an object, call it clock. Attach the timer behaviour to it. Now in the function, after the seek, start a timer with time param(1) and a timer tag. Use a new condition 'on timer' and stop the sound in it.

    But, that is a lot of work. That will be a timer for each sample.

    Other ways i can think of. If the samples are pieces definable in round seconds (should be possible because you have silence between them), then you can pre compose the samples in an array.

    Check the array in a 'every 1 second', and make decession to stop/play something according the array.

  • Thanks 99Instances2Go for the reply.

    Indeed, I already finished the game with individual samples but I need to rework it because our server can't handle that many assets, hence the big audio files. :-/

    Thanks for the ideas & explanations about functions. I'll try to go with the array, it believe it should be possible to implement that into the code.

  • You still have more chance if you ask help with the 'many assets' problem. Feel like someone could have a solution for that ( that will not be me, i know nothing about server stuff).

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Thanks for the suggestion, I didn't think there was a way to deal with it, but I'll still ask.

  • I'd say the issue comes from the wait command here when you are trying to stop the tag.

    The moment the action is actually performing, the temporary "tag" is forgotten, hence why it doesn't stop.

    A solution that seems to be working is to set the tag in a temp array :

    Example capx (r237)

    Also a note, even though you are making a big random number, in the absolute there is the possibility that you may end up with two different sounds having the same temp tag. You may want to "lock" that as well with an array too, that would allow you to prevent the same two tags from being used at the same time (before playing your sound you generate the tag thanks to a recursive function if the generated tag is found out to already be used).

  • Kyatric Thanks a lot for demonstrating this! I tried it in my code and it works perfectly! Wow!

    With all the decimals, the chances of the same 2 tags occurring at the same time is about the same as 2 players meeting each other on day one in No Man's Sky.

    Oh wait... Maybe I'll still implement your check.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)