Scripts / Sound volume relative to player distance

Author
Message
Avram
GameGuru TGC Backer
17
Years of Service
User Offline
Joined: 3rd Sep 2006
Location: Serbia
Posted: 14th Jun 2014 22:42 Edited at: 14th Jun 2014 23:06
Per request, here's a simple piece of code that (sort of) works for me:







How to use?



1) Place your entity on the map

2) Assign this script (soundvolume.lua) as it's main script

3) Change TriggerDist variable to suit your needs. It's the distance at which the sound will start and will have lowest volume

4) Assign your sound to entity's Sound0 property



How it works?



It simply gets your distance from entity and calculate it as a percentage related to TriggerDist. So if TriggerDist is 500 and player is 500 units away, then it's 100%, if 400 it's 80%, 300 is 60%, 200 is 40%, 100 is 20% and so on...



Once we have this percentage, we simply set volume to 100 - our_percentage, so as we approach the entity the volume gets higher and when we get away it goes down.



However, there are two problems with this one:



1) I can't hear anything below 60% on my machine. Guess it's either FPSC:R problem or problem related to my sound card. Can someone check this pls?

2) The volume never reaches 100% with some entities as you can't get within distance of 0 because of their thickness. This probably could be improved if we could get entity thickness or hardcode the difference in the script. Or use entity without collision (and hidden?)



edit: It seems that FPSC:R doesn't check if the sound is already playing so I had to introduce global SndPlaying to store state of sound for each entity. WIthout this check I could hear only noise just like it's playing audio file from start over and over really fast.

Sparrowhawk
GameGuru TGC Backer
13
Years of Service
User Offline
Joined: 12th Apr 2011
Location: Planet Magrathea
Posted: 14th Jun 2014 22:51
Cheers

Its not just you - smallg was looking at it specifically for zones and he found the same sort of minimum value. For ambient sound (Sea shore) it makes sense to have a fade in zone, whereas for my river entity it makes sense to have the river sections hold the sound - especially as the zones are not my favourite part of the engine right now.


I will let you know how this goes in my level, thanks again for your time

(And as I keep saying on various threads, my WIP thread will be up soon)

Passmark detects an incorrect GPU! I have 2x FirePro D500's 3GB VRAM each

Avram
GameGuru TGC Backer
17
Years of Service
User Offline
Joined: 3rd Sep 2006
Location: Serbia
Posted: 14th Jun 2014 23:05
You're welcome! I have updated the code now as my StopSound(e, 0) function was missing sound slot index (0), but it worked without it too (at least it didn't throw any errors). Maybe it's assuming slot 0 or I just couldn't hear it because of the volume [below 60%] and thought it was stopped

Sparrowhawk
GameGuru TGC Backer
13
Years of Service
User Offline
Joined: 12th Apr 2011
Location: Planet Magrathea
Posted: 14th Jun 2014 23:46 Edited at: 14th Jun 2014 23:51
I must admit to being a little confused, it works well (I've put it onto a waterfall) however, I've also put a separate instance on a nearby river, but all I ever hear is the waterfall.



If you try attaching two different sounds to entities and put them in each others range do you hear both sounds if you put the player between them or does it only play one?

Regards,

S

Passmark detects an incorrect GPU! I have 2x FirePro D500's 3GB VRAM each

Avram
GameGuru TGC Backer
17
Years of Service
User Offline
Joined: 3rd Sep 2006
Location: Serbia
Posted: 15th Jun 2014 00:13 Edited at: 15th Jun 2014 00:15
I have just tested with two entities and it works fine when they are far away, but when they are near I could hear only one sound. However, I can't see any problems with the code itself - it should play both sounds if their distances overlap (and volumes are above 60%).



The only thing that might be the problem is SetSoundVolume() function as it doesn't accept a parameter that defines which sound volume we want to control. Even it's description in the sticky thread says "set the volume of the last sound handled 0 to 100". I think, because both entity functions are called constantly in game loop, and the sound is played only once when you approach the entity, this function messes up something and doesn't control volume as expected.



To get a more clear picture, imagine two entities with this script and different sounds. Let's name them entity A and entity B. When you approach entity A it will play sound file, and internally set sound0 from entity A as a *last played sound* so SetSoundVolume() should control volume as expected. But then you go away and approach entity B - it also plays it's sound and sets sound0 from entity B as a *last played sound* and SetSoundVolume() should work as expected, but it isn't as far as I can see, maybe because you get within reach of the both at the almost same time and both of them and both of them get played, but the script will control volume only of the last played sound. Why? Read on: If you go to entity A again, assuming it's sound is still playing, the engine will still control previously set sound (from B entity) but based on player's distance from entity A, because it won't change internal pointer to *last played sound* until the sound gets played again (player goes farther than TriggerDist and then comes near again).



At least this is how I understand this problem, and this may not be true. But if it is the issue, I cant see how can we fix this in LUA. It would be better if SetSoundVolume() would accept entity number e, sound slot number, and volume level. Something like SetSoundVolume(e, sndslot, volume)...

Avram
GameGuru TGC Backer
17
Years of Service
User Offline
Joined: 3rd Sep 2006
Location: Serbia
Posted: 15th Jun 2014 00:17
You could try commenting out lines "if not SndPlaying[e] then" and "end" below it, or deleting them (and all other references to SndPlaying variable), but for some reason I couldn't get it to work, although it would be logical for FPSC to check if the sound is playing and don't play it again (until it finishes, if using LoopSound)... :/

Sparrowhawk
GameGuru TGC Backer
13
Years of Service
User Offline
Joined: 12th Apr 2011
Location: Planet Magrathea
Posted: 15th Jun 2014 00:22
Yeah don't worry, thanks for trying anyway. I'm going to submit as is and not all the issues, most of which I can only blame on the engine at this stage. The level suffers from:
-Sound zones are haywire
-AI walk off and through water
-AI walk through solid items (rocks etc.)
-AI usually wait 10 seconds between shots at the player
-Win zone doesn't end the game
-Running out of lives doesn't end the game

Passmark detects an incorrect GPU! I have 2x FirePro D500's 3GB VRAM each

Avram
GameGuru TGC Backer
17
Years of Service
User Offline
Joined: 3rd Sep 2006
Location: Serbia
Posted: 15th Jun 2014 00:38
Most of those are already known issues and you shouldn't bother about them. TGC will fix them eventually.

Sparrowhawk
GameGuru TGC Backer
13
Years of Service
User Offline
Joined: 12th Apr 2011
Location: Planet Magrathea
Posted: 15th Jun 2014 00:46 Edited at: 15th Jun 2014 00:55
Oh ok, well at least they'll take that into account then. I know they know about my AI being crazy! haha

I think I'm sort of at the end of what I can actually do in level now - apart from those things. If you use the sniper you can take out lots of the AI before they start being odd.



I was hoping to get sound effects in and a voiceover just to fine tune but they went from sounding terrible (looping without fade) to working fine with a combination of your scrip and smallg's and then I loaded out of test level and half my remaining sound zones have deleted themselves!



I'm probably best to quit while I'm a bit ahead and note all the things to be improved than trying to hack in more scripts as the level deletes itself...



Anyway, your help has been particularly appreciated, thanks

I think I'll wait to the next beta and then try sound again. I'm still impressed by what I can get out of Reloaded even if it still has a way to go!



Edit: Just come out a test level, and my win zone has expanded slightly...:



I must have jynxed myself the other day by saying I'd had no bugs.. been an exponential increase since I said that



There have been home hilarious ones as well, I must admit, I have a video somewhere of the time a chain-link fence became part of the player for no readily apparent reason. haha



Regards,

S

Passmark detects an incorrect GPU! I have 2x FirePro D500's 3GB VRAM each

smallg
Community Leader
18
Years of Service
User Offline
Joined: 8th Dec 2005
Location:
Posted: 15th Jun 2014 01:19
how about if u set TriggerDist as an array?

life's one big game

windows vista ultimate

i5 @3.3ghz, 4gb ram, geforce gtx460, directx 11
Avram
GameGuru TGC Backer
17
Years of Service
User Offline
Joined: 3rd Sep 2006
Location: Serbia
Posted: 15th Jun 2014 10:05
Hm, I can't see how that could help... Can you explain it a bit detailed?

smallg
Community Leader
18
Years of Service
User Offline
Joined: 8th Dec 2005
Location:
Posted: 15th Jun 2014 19:11 Edited at: 28th Jun 2014 04:22
seems i was wrong and you can control individual volume levels, setting the sound volume after the play command is the way to do it correctly for multiple sounds (i had been trying to set the volume first)

and to fix the issue with multiple zones i believe setting your volume variable to an array should fix it so that multiple sound levels can be stored

life's one big game

windows vista ultimate

i5 @3.3ghz, 4gb ram, geforce gtx460, directx 11
Super Clark
GameGuru TGC Backer
13
Years of Service
User Offline
Joined: 4th Apr 2011
Location: UK
Posted: 23rd Feb 2015 01:40 Edited at: 23rd Feb 2015 04:22
Hi, I have used it and its very good, just need to sort the distance for walking
up to and from a flowing river now. Thank matey.

Is their anyway to loop or repeat sound you add, as i noticed that when you
walk away and the volume goes off if you go back to the area their is no
sound again.?
PM

Login to post a reply

Server time is: 2024-05-20 02:01:17
Your offset time is: 2024-05-20 02:01:17