I'm assuming you are attempting to have 1 of 4 possible sirens play when the siren is activated, which is selected at random?
Get Ready! Super long answer that's designed to teach instead of just giving you an answer. I have a soft spot for people actively attempting to learn to script.
Lets focus on the siren selection first, then we'll worry about the sound not following the vehicle problem later. Deal?
Lets start with this part of the code first:
if (random == 0)
{
soundID = Audio::PlaySample3D("mod:Audio/FX/Sirens/Siren01.wav", CarPos, true);
} else
{
soundID = Audio::PlaySample3D("mod:Audio/FX/Sirens/Siren02.wav", CarPos, true);
} else
{
soundID = Audio::PlaySample3D("mod:Audio/FX/Sirens/Siren07.wav", CarPos, true);
} else
{
soundID = Audio::PlaySample3D("mod:Audio/FX/Sirens/Siren08.wav", CarPos, true);
}
I'm sure you may be aware, but for the sake of others, let me explain something. If-Else statements work by evaluating the condition statement in between the first set of parentheses as either true or false.
if ( CONDITION TRUE )
Do something
else
Do something else
If the statement next to "if" is true, then the code block (designated between an opening "{" and closing "}" set of curly braces) immediately following it is executed. Any subsequent "else" statements are ignored and skipped. If the "if" statement is false, then the preceding code block is ignored and instead the "else" code block (again, designated as being between an opening "{" and closed "}" set of curly braces) is executed.
As a rule, every "if" statement may only have one else statement ("else if" statements don't count). Having more than one pure "else" statement leads to unpredictable behavior, or usually, an error.
What you have in your code is an if statement with multiple "else" statements following it. In most programming languages this is not allowed and would result in an error. EM4, on the other hand, seems to have forgiven you for this and just executed all the else statements as if they were one giant code block.
So we need to fix that...later...because we have another problem on our hands...the way you are generating your random selection between the 4 sirens.
int random = Math::rand()%2;
This is self explanatory. You're generating a random number and assigning it to a variable. This is correct. What you may not realize is that you are only generating 2 possible random numbers: 0 or 1.
Math::rand() is a built in function that will generate a random number between 0 and some very large number. In your case, you only need to pick between 4 different numbers (1 number for each siren option). To do that we use math. The "%" is a mathematical operator called modulus. It gives you the remainder of a division.
Division
int a = 9 / 3; //This equals 3. ( 9 divided by 3 equals 3 )
int b = 10 / 3; //This actually equals 3. The decimal is dropped since its being assigned to an integer variable (which only stores whole numbers). 3 goes into 10 3 times with a remainder.
Modulus
int a = 9 % 3; //This equals 0, since 3 divides into 9 evenly and thus has no remainder.
int b = 10 % 3; //This equals 2 since 3 goes into 10 3 times, but has a remainder of 2.
Back to your problem:
int random = Math::random() % 2; random will equal the remainder of whatever random number is generated divided by 2. This means that your options will only ever be 1 (if an odd number) or 0 (if an even number). You need to change this. Instead, you want to randomly select between 4 options so lets change it to:
int random = Math::random() % 4;
Now, no matter what number is generated, you'll only have 4 possible options: 0 (if the number is evenly divisible by 4), 1, 2, 3.
Now that we have the script picking a siren sound, we need to fix our broken if-else statement so the proper one plays.
Back to the if-else
So we have a random number selected (0, 1, 2, or 3) and we want to play a different siren for each selection. Lets update our if-else statement to be correct:
int random = Math::rand() % 4;
if (random == 0)
{
soundID = Audio::PlaySample3D("mod:Audio/FX/Sirens/Siren01.wav", CarPos, true);
} else if (random == 1)
{
soundID = Audio::PlaySample3D("mod:Audio/FX/Sirens/Siren02.wav", CarPos, true);
} else if(random == 2)
{
soundID = Audio::PlaySample3D("mod:Audio/FX/Sirens/Siren07.wav", CarPos, true);
} else
{
soundID = Audio::PlaySample3D("mod:Audio/FX/Sirens/Siren08.wav", CarPos, true);
}
- The first "if" statement asks: Is my random number 0? If so, play siren01. Otherwise, proceed to the "else" statement.
- The first "else" says: Ok, the random number wasn't 0. Is the random number 1? If so, play siren02. If not, proceed to the next "else" statement.
- The second "else" says: Fine, is the random number a 2? If so, play siren07. If not, proceed down to the next "else".
- The third "else" doesn't ask a question (no "if" statement). It just says: I don't care what the value of the random number is. It's not what the above statements were looking for so I'll just play siren08.
In this case, if the random number was 3, it would execute the final "else" statement since it is not 0, 1, or 2 which are being looked for in the preceding "if" and "else-if" statements.
So after all that, it should make a proper selection. Try this and fix your script, then report back what else needs fixing.
This was long winded! I apologize! I want to help you and others learn the basics of scripting. It is NOT an easy skill to learn or develop without some sort of programming background. I admire your effort to do something yourself instead of just asking for help making something without actually putting in the effort to figure it out. Scripting is probably among the rarest skills in the community, and perhaps one of the most sorely needed, so anything I can do to help develop a new scripter is a good thing IMO.