Jump to content
Pottyscotty

Sound play when button pressed.

Recommended Posts

I would guess it's possible but how would I go about making a sound play (Once, not looped) when I press a button command (Specifically when turning on/off the LAFlashinglights and LADirectionallights).

 

Any help appreciated. 

Try looking at the LA Mod ToStation scripts. They would play a sound upon pressing a button.

Link to comment
Share on other sites

Try looking at the LA Mod ToStation scripts. They would play a sound upon pressing a button.

Forgot about them - I tried this piece of code but there is no sound played (But also no errors).

 

Using the Radio Sound FX as a placeholder.
 
//******************************************************************************************// #Version 1.2#//// 		Includes: All Flashing Lights command////	- VcmdFlashingLights////		Script by Hoppah//		//		Usage of this script in other mods is NOT allowed without permission of Hoppah////******************************************************************************************int DummyGroup = 24;const char OBJ_TILLER[]				= "mod:Prototypes/Vehicles/02 LA Fire Department/tiller_cabin.e4p";const char DUMMY_ISTRAILED[] 		= "DummyIsTrailed";const char SND_BEEP[]			= "mod:Audio/FX/radio/1019.wav";object VcmdFlashingLights : CommandScript{	VcmdFlashingLights()	{		SetIcon("flashinglightson");		SetCursor("flashinglights"); 		SetGroupID(DummyGroup); 		SetGroupLeader(true);		SetRestrictions(RESTRICT_SELFEXECUTE);	} 	bool CheckPossible(GameObject *Caller) 	{ 		if (!Caller->IsValid()) 			return false;		Vehicle v(Caller);		if (v.IsBlueLightEnabled())			SetIcon("flashinglightsoff");		else			SetIcon("flashinglightson");  		if (Caller->GetType() == ACTOR_VEHICLE) 		{ 			return true; 		}  		return false; 	}  	bool CheckTarget(GameObject *Caller, Actor *Target, int childID) 	{ 		if (!Caller->IsValid() || !Target->IsValid() || Target->GetID() != Caller->GetID()) 			return false;  		if (Caller->GetType() == ACTOR_VEHICLE) 		{ 			return true; 		}  		return true; 	}	void PushActions(GameObject *Caller, Actor *Target, int childID)	{		Vehicle v(Caller);		if (StrCompare(v.GetPrototypeFileName(), OBJ_TILLER) == 0 && v.HasCommand(DUMMY_ISTRAILED))		{			int VecID = v.GetID();			if (v.IsBlueLightEnabled())			{				v.EnableBlueLights(false);				VehicleList list(VT_FIREFIGHTERS_DLK, VT_FIREFIGHTERS_RW);				for(int i = 0; i < list.GetNumVehicles(); i++)				{					Vehicle *c = list.GetVehicle(i);					if(c->GetUserData() == VecID)					{						System::Log("Vehicle flashinglights found");						Audio::PlaySample3D(SND_BEEP, v.GetPosition());						if (c->IsBlueLightEnabled())							c->EnableBlueLights(false);					}				}			} else			{				v.EnableBlueLights(true);				VehicleList list(VT_FIREFIGHTERS_DLK, VT_FIREFIGHTERS_RW);				for(int i = 0; i < list.GetNumVehicles(); i++)				{					Vehicle *c = list.GetVehicle(i);					if(c->GetUserData() == VecID)					{						System::Log("Vehicle flashinglights found");						Audio::PlaySample3D(SND_BEEP, v.GetPosition());						if (!c->IsBlueLightEnabled())							c->EnableBlueLights(true);					}				}						}		} 		else if (v.IsBlueLightEnabled())			v.EnableBlueLights(false);		else			v.EnableBlueLights(true);				}};
Link to comment
Share on other sites

Is it a sound played the instant the command button is pressed?

Can't work out what kind of question you are asking so I'll answer both I can think of haha -

 

Yes that is what I want to achieve.

 

Yes I took it from a script (ToPoliceStation) where it is played when pressed.

Link to comment
Share on other sites

Can't work out what kind of question you are asking so I'll answer both I can think of haha -

 

Yes that is what I want to achieve.

 

Yes I took it from a script (ToPoliceStation) where it is played when pressed.

Alternative option. Go to Specs/audiocommands.xml

 

Create a new entry for your command. Assign it a sound to play once it is activated.

redacted

Change the command name to the one you desire. Then  change the sample file to the sound you want to play.

 

In the first place, I'm wondering what the PlaySound() function is doing in a for loop. I only use the play sound function in a single instance. What is the aim of the for loop? To seek out and search for any number of DLK's or RW's?

Link to comment
Share on other sites

Alternative option. Go to Specs/audiocommands.xml

 

Create a new entry for your command. Assign it a sound to play once it is activated.

<command name= "VcmdDeployBack" comment="">		<sample file="ACTION04.wav"/>		<sample file="ACTION03.wav"/>		<sample file="ACTION02.wav"/></command>

Change the command name to the one you desire. Then  change the sample file to the sound you want to play.

 

In the first place, I'm wondering what the PlaySound() function is doing in a for loop. I only use the play sound function in a single instance. What is the aim of the for loop? To seek out and search for any number of DLK's or RW's?

Can I do that with an existing command (VcmdFlashingLights) or does it create a new one?

 

I put it there because I thought it would trigger with the v.EnableBlueLights functions, I seemed I was wrong though :D

Link to comment
Share on other sites

My way of doing it would be to place the Sound function first. Then put the BlueLights(true) function after that.

 

I do not see what the use of a for loop is in a flashing light script if it only applies to one vehicle.

 

If you were trying to disable all the lights on every vehicle within a certain range, using a for loop would be the necessary thing to do. I have a similar button that I've created for my LAPD Command vehicle. I have named it the "Shut the ****" button because it disables the lights and sirens of every vehicle in 1500.0f float range ingame.

Link to comment
Share on other sites

My way of doing it would be to place the Sound function first. Then put the BlueLights(true) function after that.

 

I do not see what the use of a for loop is in a flashing light script if it only applies to one vehicle.

 

If you were trying to disable all the lights on every vehicle within a certain range, using a for loop would be the necessary thing to do. I have a similar button that I've created for my LAPD Command vehicle. I have named it the "Shut the ****" button because it disables the lights and sirens of every vehicle in 1500.0f float range ingame.

I'll try that in the morning as it's quite late here. - Going to try the audiocommands method first but where is the directory for the "Sampled Files" or is it just any .wav in Audio/FX?

 

This is the default LAFlashingLights script and the only thing I have edited is adding the two audio lines so the loop was already there for some reason.

 

Haha that's the reason I never turn off Automatic Sirens.

Link to comment
Share on other sites

I'll try that in the morning as it's quite late here. - Going to try the audiocommands method first but where is the directory for the "Sampled Files" or is it just any .wav in Audio/FX?

 

This is the default LAFlashingLights script and the only thing I have edited is adding the two audio lines so the loop was already there for some reason.

 

Haha that's the reason I never turn off Automatic Sirens.

Did some tinkering. The for loop is for the LAFD Tiller which is useless to you. I'd suggest deleting the whole piece of code and leaving it like this:

 

http://pastebin.com/cWu1mvmp

Link to comment
Share on other sites

Did some tinkering. The for loop is for the LAFD Tiller which is useless to you. I'd suggest deleting the whole piece of code and leaving it like this:

 

http://pastebin.com/cWu1mvmp

Thank you very much, I'll change it in the morning. Wonder why the Tiller needs to be looped.

 

Just so you know, the audiocommands method also worked.

Link to comment
Share on other sites

Thank you very much, I'll change it in the morning. Wonder why the Tiller needs to be looped.

 

Just so you know, the audiocommands method also worked.

Tiller trailer needs to be in a for loop because the game has to "seek out" the specific tiller trailer connected to the Tiller cabin. The for loop scans every single vehicle on the map and if it finds the tiller trailer, it then disables its lights accordingly.

 

The UK got no tillers so that piece of code is useless to you. What I sent you is just the flashing lights code without the tiller functinality and with a sound being played.

Link to comment
Share on other sites

Tiller trailer needs to be in a for loop because the game has to "seek out" the specific tiller trailer connected to the Tiller cabin. The for loop scans every single vehicle on the map and if it finds the tiller trailer, it then disables its lights accordingly.

 

The UK got no tillers so that piece of code is useless to you. What I sent you is just the flashing lights code without the tiller functinality and with a sound being played.

Ah I see, thanks for that.

 

I do get an error when using the script, this is when manually clicking the command. The only thing I have changed is the PlaySample3D at the bottom of the script to play SND_BUTTON not SND_TOSTATION. I am guessing the error is to do with the "Game::FindFreePosition(Caller, PD);" line.

 

RUuRwl.png

Link to comment
Share on other sites

That would be it. I posted that code expecting that you'd proofread it first before sending it ingame. That piece of code is useless for your purposes. SND_TOSTATION would also need to be changed to work.

What caller should I be using as "Caller, Actor" does not appear to work and as far as I know there is no Vehicle Caller.

Link to comment
Share on other sites

What caller should I be using as "Caller, Actor" does not appear to work and as far as I know there is no Vehicle Caller.

You need to define a vehicle variable to be the Caller.

I'd define a Vehicle variable like this:

 

Vehicle v;

 

To assign it as a caller, you append it like this Vehicle v(Caller);

 

Check this out again. Should give you more insight. It has modifications that disable special lights when enabling blue lights.

http://pastebin.com/cWu1mvmp

Link to comment
Share on other sites

You need to define a vehicle variable to be the Caller.

I'd define a Vehicle variable like this:

 

Vehicle v;

 

To assign it as a caller, you append it like this Vehicle v(Caller);

 

Check this out again. Should give you more insight. It has modifications that disable special lights when enabling blue lights.

http://pastebin.com/cWu1mvmp

The script is now working to the sense that it plays the sound (5 times though). It would appear that the modification to the script is stopping me from enabling the Blue Lights, however I would have thought this would mean I can turn on the blue lights if the Directionals were on, which did not happen.

 

https://www.youtube.com/watch?v=-t53yV03f7E&feature=youtu.be

Link to comment
Share on other sites

The script is now working to the sense that it plays the sound (5 times though). It would appear that the modification to the script is stopping me from enabling the Blue Lights, however I would have thought this would mean I can turn on the blue lights if the Directionals were on, which did not happen.

My bad. Forgot the enable button. Try the pastebin again.

Link to comment
Share on other sites

My bad. Forgot the enable button. Try the pastebin again.

That seems to have just flipped it round, I can't disable lights now and the sound still plays through five times.

 

Looking at the script does these lines mean that the Directional lights will enable the blue lights only if they are off?

 

                        Game::ExecuteCommand("VcmdDirectionalLightsOff", &v, &v);                        (!v.IsBlueLightEnabled())                                v.EnableBlueLights(true);
Link to comment
Share on other sites

 

That seems to have just flipped it round, I can't disable lights now and the sound still plays through five times.

 

Looking at the script does these lines mean that the Directional lights will enable the blue lights only if they are off?

 

                        Game::ExecuteCommand("VcmdDirectionalLightsOff", &v, &v);                        (!v.IsBlueLightEnabled())                                v.EnableBlueLights(true);

Give it another go with the pastebin. I'm beginning ot enjoy this.

Link to comment
Share on other sites

Give it another go with the pastebin. I'm beginning ot enjoy this.

Works perfectly now, thank you so much for your help! Turns out the reason it was beeping five times was because I was using the beep sound effect for the bomb not my custom one :D

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...