Author Topic: A car horn script (more info in OP)  (Read 843 times)

Basically /title

I have no idea how to script but could someone make a small script that you could paste into a vehicle's .CS file and when you Click, (LMB) the script plays a .wav sound file (that you place in the vehicle's .zip folder) and it plays the said .wav file in-game as a sort of horn.

I've already made 4 horn sound files but I just need a small script that I could paste into each vehicle.cs file that will use the horn. The reason I'm doing it this way is because I want horns to differ between different vehicles rather than every vehicle using the same horn sound.

Thanks for any help!

oh and I'll be using this in a City server and I'll give you credit

in every vehicle, create the horn sound datablock and in the vehicle datablock, give it a field "hornSound = [your sound datablock];"

then run this somewhere
package VehicleHorn
{
   function Armor::onTrigger(%this, %player, %slot, %val)
   {
      if (isObject(%sound = %player.getObjectMount().getDatablock().hornSound) && %slot == 0 && %val))
      {
         serverPlay3D(%sound, %player.getObjectMount().getPosition());
      }
      return parent::onTrigger(%this, %player, %slot, %val);
   }
};
activatePackage(VehicleHorn);

add a timeout to it so players can't spam the horn

in every vehicle, create the horn sound datablock and in the vehicle datablock, give it a field "hornSound = [your sound datablock];"

then run this somewhere
package VehicleHorn
{
   function Armor::onTrigger(%this, %player, %slot, %val)
   {
      if (isObject(%sound = %player.getObjectMount().getDatablock().hornSound) && %slot == 0 && %val))
      {
         serverPlay3D(%sound, %player.getObjectMount().getPosition());
      }
      return parent::onTrigger(%this, %player, %slot, %val);
   }
};
activatePackage(VehicleHorn);

Thanks but uh, I don't really know how to set a datablock nor do I know how to "run this somewhere"

Could you type it out on what to do exactly and where to go and such? I'm like very VERY new to any sort of torque script

You can copy and paste that script anywhere inside of your fileName.cs file, just make sure it's not inside of any other script, scripts like those are usually around the bottom of a CS file

As for the sound datablocks, this is what it should look like
Code: [Select]
datablock AudioProfile(VehicleName_HornSound)
{
   filename    = "./fileName.wav";
   description = AudioClose3d;
   preload = true;
};
This goes somewhere at the top of your CS file

Then in the WheeledVehicleData datablock, you put hornSound = VehicleName_HornSound; where ever, like this for example (look for the red text)
Quote
datablock WheeledVehicleData(JeepVehicle)
{
   category = "Vehicles";
   displayName = " ";
   shapeFile = "./jeep.dts"; //"~/data/shapes/skivehicle.dts"; //
   emap = true;
   minMountDist = 3;
   
   numMountPoints = 7;
   mountThread[0] = "sit";
   mountThread[1] = "sit";
   mountThread[2] = "sit";
   mountThread[3] = "sit";
   mountThread[4] = "sit";
   mountThread[5] = "root";
   mountThread[6] = "root";
   mountThread[7] = "sit";

   maxDamage = 200.00;
   destroyedLevel = 200.00;
   speedDamageScale = 1.04;
   collDamageThresholdVel = 20.0;
   collDamageMultiplier   = 0.02;

   massCenter = "0 0 0";
   //massBox = "2 5 1";

   maxSteeringAngle = 0.9785;  // Maximum steering angle, should match animation
   integration = 4;           // Force integration time: TickSec/Rate
   tireEmitter = VehicleTireEmitter; // All the tires use the same dust emitter

   // 3rd person camera settings
   cameraRoll = false;         // Roll the camera with the vehicle
   cameraMaxDist = 13;         // Far distance from vehicle
   cameraOffset = 7.5;        // Vertical offset from camera mount point
   cameraLag = 0.0;           // Velocity lag of camera
   cameraDecay = 0.75;        // Decay per sec. rate of velocity lag
   cameraTilt = 0.4;
   collisionTol = 0.1;        // Collision distance tolerance
   contactTol = 0.1;

   useEyePoint = false;   

   defaultTire   = jeepTire;
   defaultSpring   = jeepSpring;
   //flatTire   = jeepFlatTire;
   //flatSpring   = jeepFlatSpring;

   numWheels = 4;

   // Rigid Body
   mass = 300;
   density = 5.0;
   drag = 1.6;
   bodyFriction = 0.6;
   bodyRestitution = 0.6;
   minImpactSpeed = 10;        // Impacts over this invoke the script callback
   softImpactSpeed = 10;       // Play SoftImpact Sound
   hardImpactSpeed = 15;      // Play HardImpact Sound
   groundImpactMinSpeed    = 10.0;

   // Engine
   engineTorque = 12000; //4000;       // Engine power
   engineBrake = 2000;         // Braking when throttle is 0
   brakeTorque = 50000;        // When brakes are applied
   maxWheelSpeed = 30;        // Engine scale by current speed / max speed

   rollForce      = 900;
   yawForce      = 600;
   pitchForce      = 1000;
   rotationalDrag      = 0.2;

   // Advanced Steering
   steeringAutoReturn = true;
   steeringAutoReturnRate = 0.9;
   steeringAutoReturnMaxSpeed = 10;
   steeringUseStrafeSteering = true;
   steeringStrafeSteeringRate = 0.1;

   // Energy
   maxEnergy = 100;
   jetForce = 3000;
   minJetEnergy = 30;
   jetEnergyDrain = 2;

   splash = vehicleSplash;
   splashVelocity = 4.0;
   splashAngle = 67.0;
   splashFreqMod = 300.0;
   splashVelEpsilon = 0.60;
   bubbleEmitTime = 1.4;
   splashEmitter[0] = vehicleFoamDropletsEmitter;
   splashEmitter[1] = vehicleFoamEmitter;
   splashEmitter[2] = vehicleBubbleEmitter;
   mediumSplashSoundVelocity = 10.0;   
   hardSplashSoundVelocity = 20.0;   
   exitSplashSoundVelocity = 5.0;
      
   //mediumSplashSound = "";
   //hardSplashSound = "";
   //exitSplashSound = "";
   
   // Sounds
   //   jetSound = ScoutThrustSound;
   //engineSound = idleSound;
   //squealSound = skidSound;
   softImpactSound = slowImpactSound;
   hardImpactSound = fastImpactSound;
   //wheelImpactSound = slowImpactSound;
   hornSound = VehicleName_HornSound;

   //   explosion = VehicleExplosion;
   justcollided = 0;

   uiName = "Jeep ";
   rideable = true;
      lookUpLimit = 0.65;
      lookDownLimit = 0.45;

   paintable = true;
   
   damageEmitter[0] = VehicleBurnEmitter;
   damageEmitterOffset[0] = "0.0 0.0 0.0 ";
   damageLevelTolerance[0] = 0.99;

   damageEmitter[1] = VehicleBurnEmitter;
   damageEmitterOffset[1] = "0.0 0.0 0.0 ";
   damageLevelTolerance[1] = 1.0;

   numDmgEmitterAreas = 1;

   initialExplosionProjectile = jeepExplosionProjectile;
   initialExplosionOffset = 0;         //offset only uses a z value for now

   burnTime = 4000;

   finalExplosionProjectile = jeepFinalExplosionProjectile;
   finalExplosionOffset = 0.5;          //offset only uses a z value for now

   minRunOverSpeed    = 4;   //how fast you need to be going to run someone over (do damage)
   runOverDamageScale = 8;   //when you run over someone, speed * runoverdamagescale = damage amt
   runOverPushScale   = 1.2; //how hard a person you're running over gets pushed

   //protection for passengers
   protectPassengersBurn   = false;  //protect passengers from the burning effect of explosions?
   protectPassengersRadius = true;  //protect passengers from radius damage (explosions) ?
   protectPassengersDirect = false; //protect passengers from direct damage (bullets) ?

};

Alright I understand that and I'll give it a shot, thanks!

It isn't working, there's some syntax in the console log

Code: [Select]
Loading Add-On: Vehicle_Microbus (CRC:2042821337)
Add-Ons/Vehicle_Microbus/Vehicle_Microbus.cs Line: 289 - Syntax error.
>>> Some error context, with ## on sides of error halt:
//^^^%this.setNodeColor("doortrim",%stripe);

//^^}

//^}

//};

//activatePackage(Vehicle_Microbus);



package VehicleHorn

{

   function Armor::onTrigger(%this, %player, %slot, %val)

   {

      if (isObject(%sound = %player.getObjectMount().getDatablock().hornSound) && %slot == 0 && %val))##
##
      {

         serverPlay3D(%sound, %player.getObjectMount().getPosition());

      }

      return parent::onTrigger(%this, %player, %slot, %val);

   }

};

activatePackage(VehicleHorn);
>>> Error report complete.

ADD-ON "Vehicle_Microbus" CONTAINS SYNTAX ERRORS

And here is the entire paste of the Microbus CS file I'm using to test this out

Code: [Select]
datablock AudioProfile(Microbus_HornSound)
{
   filename    = "./carHorn1.wav";
   description = AudioClose3d;
   preload = true;
};
 
datablock TSShapeConstructor(MicrobusDts)
{
    baseShape  = "./microbusupdate2.dts";
    sequence0  = "./root.dsq root";
    sequence1  = "./t_open.dsq open";
    sequence2  = "./t_close.dsq close";
    sequence3  = "./l_open.dsq lopen";
    sequence4  = "./l_close.dsq lclose";
    sequence5  = "./r_open.dsq ropen";
    sequence6  = "./r_close.dsq rclose";
    sequence7  = "./s_open.dsq sopen";
    sequence8  = "./s_close.dsq sclose";
};
   
datablock WheeledVehicleData(MicrobusVehicle)
{
    category = "Vehicles";
    displayName = " ";
    shapeFile = "./microbusupdate2.dts";
    emap = true;
    minMountDist = 3;
   
   numMountPoints = 9;
   mountThread[0] = "sit";
   mountThread[1] = "sit";
   mountThread[2] = "sit";
   mountThread[3] = "sit";
   mountThread[4] = "sit";
   mountThread[5] = "sit";
   mountThread[6] = "sit";
   mountThread[7] = "sit";
   mountThread[8] = "sit";
 
    maxDamage = 300.00;
    destroyedLevel = 300.00;
    energyPerDamagePoint = 160;
    speedDamageScale = 1.04;
    collDamageThresholdVel = 20.0;
    collDamageMultiplier   = 0.02;
 
    massCenter = "0 0.38 0.4";
   //massBox = "3 3 3";
 
    maxSteeringAngle = 0.9785;  // Maximum steering angle, should match animation
    integration = 4;           // Force integration time: TickSec/Rate
    tireEmitter = VehicleTireEmitter; // All the tires use the same dust emitter
 
    // 3rd person camera settings
    cameraRoll = false;         // Roll the camera with the vehicle
    cameraMaxDist = 10;         // Far distance from vehicle
    cameraOffset = 4.9;        // Vertical offset from camera mount point
    cameraLag = 0.0;           // Velocity lag of camera
    cameraDecay = 0.75;        // Decay per sec. rate of velocity lag
    cameraTilt = 0.3;
   collisionTol = 0.1;        // Collision distance tolerance
   contactTol = 0.1;
 
    useEyePoint = false;   
 
    defaultTire  = MicroBusWheelTire;
    defaultSpring = jeepSpring;
    //flatTire      = jeepFlatTire;
    //flatSpring     = jeepFlatSpring;
 
   numWheels = 4;
 
    // Rigid Body
    mass = 370;
    density = 5.0;
    drag = 1.6;
    bodyFriction = 0.6;
    bodyRestitution = 0.6;
    minImpactSpeed = 10;        // Impacts over this invoke the script callback
    softImpactSpeed = 10;       // Play SoftImpact Sound
    hardImpactSpeed = 15;      // Play HardImpact Sound
    groundImpactMinSpeed    = 10.0;
 
    // Engine
    engineTorque = 3500; //2500;       // Engine power
    engineBrake = 500;         // Braking when throttle is 0
    brakeTorque = 4000;        // When brakes are applied
    maxWheelSpeed = 30;        // Engine scale by current speed / max speed
 
    rollForce       = 100;
    yawForce        = 100;
    pitchForce      = 100;
    rotationalDrag      = 0.2;
   
    // Advanced Steering
   steeringAutoReturn = true;
   steeringAutoReturnRate = 0.9;
   steeringAutoReturnMaxSpeed = 10;
   steeringUseStrafeSteering = true;
   steeringStrafeSteeringRate = 0.1;
   
    // Energy
    maxEnergy = 100;
    jetForce = 3000;
    minJetEnergy = 30;
    jetEnergyDrain = 2;
 
    splash = vehicleSplash;
    splashVelocity = 4.0;
    splashAngle = 67.0;
    splashFreqMod = 300.0;
    splashVelEpsilon = 0.60;
    bubbleEmitTime = 1.4;
    splashEmitter[0] = vehicleFoamDropletsEmitter;
    splashEmitter[1] = vehicleFoamEmitter;
    splashEmitter[2] = vehicleBubbleEmitter;
    mediumSplashSoundVelocity = 10.0; 
    hardSplashSoundVelocity = 20.0; 
    exitSplashSoundVelocity = 5.0;
       
    //mediumSplashSound = "";
    //hardSplashSound = "";
    //exitSplashSound = "";
   
    // Sounds
    //   jetSound = ScoutThrustSound;
    //engineSound = idleSound;
    //squealSound = skidSound;
    softImpactSound = slowImpactSound;
    hardImpactSound = fastImpactSound;
    //wheelImpactSound = slowImpactSound;
        hornSound = Microbus_HornSound;
 
    //   explosion = VehicleExplosion;
    justcollided = 0;
 
   uiName = "RAF 2203 Latvija";
    rideable = true;
        lookUpLimit = 0.65;
        lookDownLimit = 0.45;
 
    paintable = true;
   
   damageEmitter[0] = vehicleBurnEmitter;
    damageEmitterOffset[0] = "0.0 0.0 0.0 ";
    damageLevelTolerance[0] = 0.99;
 
   damageEmitter[1] = vehicleBurnEmitter;
    damageEmitterOffset[1] = "0.0 0.0 0.0 ";
    damageLevelTolerance[1] = 1.0;
 
   numDmgEmitterAreas = 1;
 
   initialExplosionProjectile = microbuloveplosionProjectile;
   initialExplosionOffset = 0;         //offset only uses a z value for now
 
   burnTime = 4000;
 
   finalExplosionProjectile = microbusFinalExplosionProjectile;
   finalExplosionOffset = 0.5;          //offset only uses a z value for now
 
   minRunOverSpeed    = 2;   //how fast you need to be going to run someone over (do damage)
   runOverDamageScale = 5;   //when you run over someone, speed * runoverdamagescale = damage amt
   runOverPushScale   = 1.2; //how hard a person you're running over gets pushed
   
   isInteractive = true;
    iNumZones = 4;
    iZone[0] = "-5 1.9 0.6 -1.8 4.1 3.3";
    iZoneScript[0] = "onLeftDoor";
    iZone[1] = "1.8 1.9 0.6 5 4.1 3.3";
    iZoneScript[1] = "onRightDoor";
    iZone[2] = "1.72 -0.1 0.3 5 1.8 3.3";
    iZoneScript[2] = "onSideDoor";
    iZone[3] = "-1.6 -5.2 0.3 1.6 -3.04 3.2";
    iZoneScript[3] = "onRear";
};
 
function MicrobusVehicle::onAdd(%this,%obj)
{
    parent::onadd(%this,%obj);
 
    VehicleSpeedCheck(%obj);
 
    %obj.setWheelTire(0, MicrobusTire);
    %obj.setWheelTire(1, MicrobusTire);
    %obj.setWheelTire(2, MicrobusTire);
    %obj.setWheelTire(3, MicrobusTire);
 
    %obj.setWheelSpring(0, jeepspring);
    %obj.setWheelSpring(1, jeepspring);
    %obj.setWheelSpring(2, jeepspring);
    %obj.setWheelSpring(3, jeepspring);
 
    %obj.setWheelSteering(0,1);
    %obj.setWheelSteering(1,1);
    %obj.setWheelSteering(2,0);
    %obj.setWheelSteering(3,0);
 
    %obj.mountable = true;
    %obj.playthread(0, root);
 
}
function MicrobusVehicle::onLeftDoor(%this, %obj, %client, %offset, %position, %vector)
{
    %time = getSimTime();
    if((%time - %obj.leftDoorTime) > 1300)
    {
        %obj.leftDoorTime = %time;
        %obj.leftDoor = !%obj.leftDoor;
        %obj.playThread(0, %obj.leftDoor ? lopen : lclose);
    }
    return 1;
}
function MicrobusVehicle::onRightDoor(%this, %obj, %client, %offset, %position, %vector)
{
    %time = getSimTime();
    if((%time - %obj.rightDoorTime) > 1300)
    {
        %obj.rightDoorTime = %time;
        %obj.rightDoor = !%obj.rightDoor;
        %obj.playThread(1, %obj.rightDoor ? ropen : rclose);
    }
    return 1;
}
function MicrobusVehicle::onSideDoor(%this, %obj, %client, %offset, %position, %vector)
{
    %time = getSimTime();
    if((%time - %obj.sideDoorTime) > 1300)
    {
        %obj.sideDoorTime = %time;
        %obj.sideDoor = !%obj.sideDoor;
        %obj.playThread(2, %obj.sideDoor ? sopen : sclose);
    }
    return 1;
}
function MicrobusVehicle::onRear(%this, %obj, %client, %offset, %position, %vector)
{
    %time = getSimTime();
    if((%time - %obj.rearTime) > 1300)
    {
        %obj.rearTime = %time;
        %obj.rear = !%obj.rear;
        %obj.playThread(3, %obj.rear ? open : close);
    }
    return 1;
}
 
//package Vehicle_Microbus
//{
//  function Vehicle::setNodeColor(%this,%node,%color)
//  {
//      parent::setNodeColor(%this,%node,%color);
//      if(%this.getDatablock() == MicrobusVehicle.getID() && %node $= "ALL")
//      {
//          %r = getWord(%color,0);
//          %g = getWord(%color,1);
//          %b = getWord(%color,2);
//          %lowest = 1;
//          for(%a = 0; %a < 3; %a++)
//          {
//              %val = getWord(%color,%a);
//              if(%val > %highest)
//                  %highest = %val;
//              if(%val < %lowest)
//                  %lowest = %val;
//          }
//          %average = atof(%r) + atof(%g) + atof(%b);
//          %average /= 3;
//          %sat = %highest - %lowest;
//          if(%sat < 0.1 && %average > 0.25)
//              %stripe = "0.55 0.58 1 1.0";
//          else
//              %stripe = "0.9 0.9 0.9 1.0";
//          %this.setNodeColor("stripa",%stripe);
//          %this.setNodeColor("stripea",%stripe);
//          %this.setNodeColor("stripb",%stripe);
//          %this.setNodeColor("strips",%stripe);
//          %this.setNodeColor("doortrim",%stripe);
//      }
//  }
//};
//activatePackage(Vehicle_Microbus);
 
package VehicleHorn
{
   function Armor::onTrigger(%this, %player, %slot, %val)
   {
      if (isObject(%sound = %player.getObjectMount().getDatablock().hornSound) && %slot == 0 && %val))
      {
         serverPlay3D(%sound, %player.getObjectMount().getPosition());
      }
      return parent::onTrigger(%this, %player, %slot, %val);
   }
};
activatePackage(VehicleHorn);

Alright Conan found out what was wrong, in the script block he added in an extra parenthesis by mistake. I removed it and now the script works flawlessly! Thank you Conan and Masterlego dude! <3

oh and here's a video example of it ingame (click the image)


Locking thread, I will unlock it if I need any further help!

Edit: Fixed the video link lol
« Last Edit: June 24, 2016, 03:37:33 AM by Insert Name Here² »