How do I stop an audio tag inside a function

Get help using Construct 2

Post » Mon Oct 17, 2016 12:45 pm

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.

Image

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! :)
B
33
S
6
G
1
Posts: 355
Reputation: 2,850

Post » Mon Oct 17, 2016 1:40 pm

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.
B
33
S
18
G
28
Posts: 2,493
Reputation: 20,950

Post » Mon Oct 17, 2016 2:45 pm

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.
B
33
S
6
G
1
Posts: 355
Reputation: 2,850

Post » Mon Oct 17, 2016 4:16 pm

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).
B
33
S
18
G
28
Posts: 2,493
Reputation: 20,950

Post » Tue Oct 18, 2016 7:57 am

Thanks for the suggestion, I didn't think there was a way to deal with it, but I'll still ask.
B
33
S
6
G
1
Posts: 355
Reputation: 2,850

Post » Tue Oct 18, 2016 9:49 am

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 :
Image

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).
New to Construct ? Where to start

Image Image

Image Image

Please attach a capx to any help request or bug report !
Moderator
B
295
S
118
G
96
Posts: 7,327
Reputation: 71,246

Post » Tue Oct 18, 2016 10:26 am

@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.
B
33
S
6
G
1
Posts: 355
Reputation: 2,850


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 9 guests