Author Topic: Vehicle Script Assistance  (Read 1410 times)

Hey, could someone possibly throw together a script for me? If so, I need a function that:

When the vehicle is Accelerating over 5mph or so:
   -check if a variable called "isAccel" is 0. if so:
     -set it to 1
     -mount an image called DMTVJetStreamImage1 and one called DMTVJetStreamImage2
     -play a thread called "toFly"

and then when below that speed:
   -check if the variable is 1. if so:
     -set the variable to 0
     -unmount the image called DMTVJetStreamImage1 and the one called DMTVJetStreamImage2
     -play a thread called "toIdle"

this is probably a lot to ask of people... but i cant get mine to work:

Code: [Select]
function vehicle::onSpawn(%obj,%velocity)
{
  if(vectorLen(%obj.getVelocity()) > 1)
  {
  %obj.mountImage(DMTVJetStreamImage1,0);
    %obj.mountImage(DMTVJetStreamImage2,1);
        %obj.playThread(0,"toFly");
        return %p;
  }

  if(vectorLen(%obj.getVelocity()) < 1)
  {
  %obj.unMountImage(DMTVJetStreamImage1,0);
    %obj.unMountImage(DMTVJetStreamImage2,1);
            %obj.playThread(0,"toIdle");
            return %p;
  }
}
I copied various code sections from other add-ons to fill the places i needed, but that's probably why it didn't work :P i didn't even have the variable in the script, but that's my crappy scripting knowledge for you.

The biggest problem is that you're only checking it once when the vehicle is created.
A few other smaller problems:
Using vehicle::onSpawn will affect every vehicle. In the script below, change <YourVehicleDatablockName> to, well, you vehicle datablock name
You have a check for > 1, and a check for < 1, with no check for exactly one. I changed it to >= 1 and < 1. This is not an issue here as the velocity is extremely unlikely to be exactly 1 for any noticable amount of time, but just keep it in mind for future scripts.
Untested:

Code: [Select]
function <YourVehicleDatablockName>::onSpawn(%this)
{
%this.checkVelocity();
}

function <YourVehicleDatablockName>::checkVelocity(%this)
{
cancel(%this.checkVelocitySchedule);
if(!isthisect(%this))
return;

if(vectorLen(%this.getVelocity()) >= 1 && !%this.isFly)
{
%this.mountImage(DMTVJetStreamImage1,0);
%this.mountImage(DMTVJetStreamImage2,1);
%this.playThread(0,"toFly");
%this.isFly = 1;
}

if(vectorLen(%this.getVelocity()) < 1 && %this.isFly)
{
%this.unMountImage(DMTVJetStreamImage1,0);
%this.unMountImage(DMTVJetStreamImage2,1);
%this.playThread(0,"toIdle");
%this.isFly = 0;
}

%this.checkVelocitySchedule = %this.schedule(100,checkVelocity);
}


I'm unsure if those are the correct arguments for onSpawn as I couldn't find any source. I also changed a few things to match my own coding style.
Also, get in the habit of proper indenting. It makes code a lot easier to read.
« Last Edit: August 15, 2011, 01:05:57 PM by Headcrab Zombie »

thanks man. once again i owe you one :)

-edit: it unfortunately didn't work ingame, although from the looks of it it should have. do you think if i sent you the whole of the script that it would help?
« Last Edit: August 15, 2011, 01:35:24 PM by Midway Sentinel »

Trying add %obj.checkVelocity() to the onAdd function, and remove the onSpawn function
« Last Edit: August 15, 2011, 03:50:47 PM by Headcrab Zombie »

thanks, ill try it straight away!

like so?

Code: [Select]
function DMTVvehicle::onadd(%this,%obj)
{
       %obj.checkVelocity
     %obj.PlayAudio(0,DMTVSound);
}
and remove the whole onSpawn function?

like so?

Code: [Select]
function DMTVvehicle::onadd(%this,%obj)
{
       %obj.checkVelocity();
     %obj.PlayAudio(0,DMTVSound);
}
and remove the whole onSpawn function?

Fixed, and unless you added something else to onSpawn, then yeah.

still does nothing ingame. i dont get it. it should work

I must be doing something wrong, but without a copy on blockland (I'm in class) I can't figure out what

ok, well i thank you for your efforts. maybe i can send you the entire project later, so you can try and find the full error? i know little scripting, but im attempting to learn. the way i see it: if i could learn modeling, i can learn scripting.

Does it give any error in the console?

when i fly it? or when the datablocks are loading?


this upon spawn:

I didn't see it until now, but sometime when I was writing the code I gave you, I replaced every occurance of 'obj' with 'this' and what was supposed to say 'if(!isObject(%this))' instead says 'if(!isthisect(%this))'
Fixing that and something else and I'll edit this post.

Edit : Replace the entire DMTV::checkVelocity function with this:
Code: [Select]
function DMTVCheckVelocity(%obj)
{
cancel(%obj.checkVelocitySchedule);
if(!isObject(%obj))
return;

if(vectorLen(%obj.getVelocity()) >= 1 && !%obj.isFly)
{
%obj.mountImage(DMTVJetStreamImage1,0);
%obj.mountImage(DMTVJetStreamImage2,1);
%obj.playThread(0,"toFly");
%obj.isFly = 1;
}

if(vectorLen(%obj.getVelocity()) < 1 && %obj.isFly)
{
%obj.unMountImage(DMTVJetStreamImage1,0);
%obj.unMountImage(DMTVJetStreamImage2,1);
%obj.playThread(0,"toIdle");
%obj.isFly = 0;
}

%obj.checkVelocitySchedule = schedule(100,0,DMTVcheckVelocity,%obj);
}
(Hopefully I didn't make the same mistake there)

And change %obj.checkVelocity() in onAdd to DMTVCheckVelocity(%obj);
« Last Edit: August 15, 2011, 05:57:56 PM by Headcrab Zombie »