Scripts / Sound Command Questions

Author
Message
GoDevils
9
Years of Service
User Offline
Joined: 24th Sep 2014
Location: Arizona USA
Posted: 12th Mar 2021 04:20
I have a script that activates a sound. The sound plays or loops. However, during game play, the player trips a 2nd sound (separate script) and the 2nd sound begins to play. Is there a way for the first script to detect the playing of the 2nd sound so the first script can stop the first sound?

Also, could someone give me an quick overview refresher on "Global Sounds" and "RawSounds"

Thanks
"THERE IS NO SPOON"

AMD 6300 6 core 3.5 ghz, Windows 10, 8GB ram, GTX 1060 2GB ram
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 12th Mar 2021 09:15
When you trigger the first sound set a global variable to indicate the entityId and slot playing.
In the second script check if that variable is set and if so use the values to stop the first sound.
Then set the global variable to the entityId and slot of the second sound so you can do the same in a third script etc.

-- at top of each script:
g_SoundPlaying = g_SoundPlaying or {}

-- in each script where you play the sound
if g_SoundPlaying.e then StopSound( g_SoundPlaying.e, g_SoundPlaying.slot ) end
g_SoundPlaying = { id = e, slot = <soundslot> }

Been there, done that, got all the T-Shirts!
PM
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 12th Mar 2021 09:18
Raw sounds are those already pre-loaded into the engine, things like the footfall sounds, heartbeat etc.

Global sounds are ones you load yourself and give an Id to, then you can play them using that id from any script (hence 'global').

There is also a separate music system.

Been there, done that, got all the T-Shirts!
PM
GoDevils
9
Years of Service
User Offline
Joined: 24th Sep 2014
Location: Arizona USA
Posted: 12th Mar 2021 20:33
@ Amen

Thanks so much. this is just the information I needed.

Just two syntex questions g_SoundPlaying = g_SoundPlaying or {}
I haven't use the "or" before. Can you explain

if g_SoundPlaying.e then StopSound( g_SoundPlaying.e, g_SoundPlaying.slot ) end
Have not used the ".e" before. is this the same as (e)?

Why not declare the global variable at the top of one script
Why each script?

Thanks


"THERE IS NO SPOON"

AMD 6300 6 core 3.5 ghz, Windows 10, 8GB ram, GTX 1060 2GB ram
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 12th Mar 2021 21:07 Edited at: 12th Mar 2021 21:10
So Lua has this concept of nil which always equates to 'false' and basically is what you get if a variable has not been initialised yet.

So:
g_SoundPlaying = g_SoundPlaying or {}

is a shorthand way of saying:
if g_SoundPlaying == nil then g_SoundPlaying = {} end

By doing it this way it doesn't matter which script is loaded first if they all have this line then g_SoundPlaying will get defined as a list and a later loading script will not mess with that definition. It also makes sure that all the scripts refer to the same global variable as you can simply cut and paste the line to all the scripts that need to access the variable and as a neat side effect to see what the variable actually is when you come to look at the script later on it is right there in the script and you don't need to hunt for it.

So now we have defined our global list we can store values in it, the first value we want to store is the entityId of the currently playing sound, i.e. 'id', the ',' notation is a shorthand way of saying g_SoundPlaying ['id'].
The second value is the slot number of the sound playing, i.e. '.slot'.

I've realised an error though, it should be:
if g_SoundPlaying.id then StopSound( g_SoundPlaying.id, g_SoundPlaying.slot ) end

Oops.
Been there, done that, got all the T-Shirts!
PM
GoDevils
9
Years of Service
User Offline
Joined: 24th Sep 2014
Location: Arizona USA
Posted: 12th Mar 2021 22:08

OK... I think I've got it...Famous Last Words..

Thanks so much


"THERE IS NO SPOON"

AMD 6300 6 core 3.5 ghz, Windows 10, 8GB ram, GTX 1060 2GB ram
GoDevils
9
Years of Service
User Offline
Joined: 24th Sep 2014
Location: Arizona USA
Posted: 14th Mar 2021 04:14
Hi Amen

I've been trying the global sound tracking discussed above. and I've run into a problem.
I've integrated your code into my script but when I test it I get an error code (see below)

Here's the code integrated into my script.

********************************************************
g_SoundPlaying1 = g_SoundPlaying1 or {} --at the top of the script


--**** This plays or loops and sets volume ***
if SndLoop == 1 then PlaySound(e,ObjPoint[13])
end
if SndLoop == 2 then LoopSound(e,ObjPoint[13])
end

SetSoundVolume(ObjPoint[14])
--************************************************

--****************** This checks the global and stops playing if needed ******************

if g_SoundPlaying1.id then StopSound (g_SoundPlaying1.id, g_SoundPlaying1.slot) end
-- NOTE this is the line that's getting the error

g_SoundPlaying1 {id = e, slot = ObjPoint[13]} -- sets id and slot for new sound


Error code: " attempt to call global g_SoundPlaying1 [a table value]"

I'm not sure what's happening ??? the code seems correct. Do I need to set
the id and slot variables into the table???
"THERE IS NO SPOON"

AMD 6300 6 core 3.5 ghz, Windows 10, 8GB ram, GTX 1060 2GB ram
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 14th Mar 2021 09:05
g_SoundPlaying = { id = e, slot = <soundslot> }

So with the weird looking code you have it would be:
g_SoundPlaying = { id = e, slot = ObjPoint[13] }

Been there, done that, got all the T-Shirts!
PM
GoDevils
9
Years of Service
User Offline
Joined: 24th Sep 2014
Location: Arizona USA
Posted: 15th Mar 2021 01:06
!! Success !! Got it working. I move the first line into the init function, and found a syntax error. Once corrected she works likes a charm.

Thanks Amen
"THERE IS NO SPOON"

AMD 6300 6 core 3.5 ghz, Windows 10, 8GB ram, GTX 1060 2GB ram

Login to post a reply

Server time is: 2024-05-02 01:29:08
Your offset time is: 2024-05-02 01:29:08