Author Topic: how do i go making a togglegravity event based off of the gravity jeep?  (Read 968 times)

i'm not completely familiar with torquescript so a lot of my jabs at it are lucky guesses when using other script references.

the basic idea is i'm trying to make an event that toggles a vehicle like the gravity jeep to connect to walls and stuff or not. i modified some speedkarts to add this same gravity effect. is the event not correctly targeting the vehicle because it's regarding an obj? is vehicle.obj a thing?
the solution is probably painfully obvious and i'm just not understanding it because i'm dumb. thanks in advance

edit: also another thing i was wondering: the bool stuff for the event i found had the outcomes as true or false. does it matter if i use %obj.isshiftinggravity=1; over %obj.isshiftinggravity=true; ? i'd imagine it wouldn't but just making sure
Code: [Select]
registerOutputEvent("Vehicle", "ToggleGravity", "bool",1);

function ToggleGravity(%obj, %bool)
{
if(%bool==true)
%obj.isshiftinggravity=1;

else
if(%bool==false)
%obj.isshiftinggravity=0;
}

the script from the gravity jeep:
Code: [Select]
$GravityJeepTypemasks=$typemasks::fxbrickobjecttype | $typemasks::interiorobjecttype | $typemasks::terrainobjecttype | $typemasks::StaticObjectType;

datablock wheeledvehicledata(GravityJeepVehicle : JeepVehicle){
 cameraRoll=true;
 cameramaxdist=8;
 cameraoffset=0;
 uiname="Gravity Jeep";
};

function GravityJeepVehicle::onadd(%this,%obj){
 %obj.isshiftinggravity=1;
 %obj.grav="0 0 -0.5";
 shiftgravitycheck(%obj);
 parent::onadd(%this,%obj);
}

function shiftgravitycheck(%obj){
 if(!%obj.isshiftinggravity)
  return;
 %pos=%obj.getposition();
 %ray=containerraycast(%pos,vectoradd(%pos,vectorscale(%obj.getupvector(),"-4 -4 -4")),$GravityJeepTypemasks,%obj);
 %normal=getwords(%ray,4,6);
 if(%normal!$="")
  %obj.grav=vectorscale(%normal,"-0.5 -0.5 -0.5");
 %obj.setvelocity(vectoradd(%obj.getvelocity(),vectoradd("0 0 0.5",%obj.grav)));
 schedule(25,0,shiftgravitycheck,%obj);
}
« Last Edit: October 19, 2015, 02:49:17 AM by Kobewarrior »

You will probably want to have two datablocks, one for each state of the vehicle, and then just switch between them.
In torque, 1 vs true vs "true" doesn't matter.
Your output event is going to look for a function that has classname::functionname as its identifier.  I am not sure exactly which vehicle class type is default for events, but you can look that up by looking at other vehicle events.
For functions such as GravityJeepVehicle::onadd(%this, %obj), I am pretty sure %this refers to the vehicle, and %obj refers to the object being added to the vehicle, such as  a person jumping into it, but %this might refer to the datablock instead, so I wouldn't trust me.

For functions such as GravityJeepVehicle::onadd(%this, %obj), I am pretty sure %this refers to the vehicle, and %obj refers to the object being added to the vehicle, such as  a person jumping into it, but %this might refer to the datablock instead, so I wouldn't trust me.
those %whatever arguments(?) are something i haven't been explained to but i get a rough idea of how they work (after learning a bit of C). i've went and used the same gravity jeep script for the speedkarts and they registered the gravity effect perfectly; the event on the other hand refuses to work with it. from my understanding there's a constant check with the gravity jeep to see if isshiftinggravity is positive and work from there? i've set it to 0 before which completely negated the gravity effect so i figured it would be as simple as an on and off switch

You will probably want to have two datablocks, one for each state of the vehicle, and then just switch between them.
does this not affect the driver? i've never done anything with vehicles before so this is completely new to me. i've always thought a changing state on a vehicle kicks the player out much like changing a horse's datablock during the ride (or is that because they're generally classified as bots and that's just a bot thing?). i'm trying to apply this to multiple speedkarts so i don't know if this over-complicates the situation

Your output event is going to look for a function that has classname::functionname as its identifier.  I am not sure exactly which vehicle class type is default for events, but you can look that up by looking at other vehicle events.
i'll try my best to process this out with my sleep deprived mind haha. event_vehicle seems to show a lot of Vehicle being the classname if i'm correct, an example being Vehicle::setColor? also i'm not very sure if this is of any significance: $inputTarget_Vehicle = %obj;

For functions such as GravityJeepVehicle::onadd(%this, %obj), I am pretty sure %this refers to the vehicle, and %obj refers to the object being added to the vehicle, such as  a person jumping into it, but %this might refer to the datablock instead, so I wouldn't trust me.
onAdd is essentially the equivalent of a constructor, being called when the vehicle is created. So there would be no one jumping into it at that time; the method for that is onMount (though I'm not sure if that's a vehicle or player method)

from my understanding there's a constant check with the gravity jeep to see if isshiftinggravity is positive and work from there? i've set it to 0 before which completely negated the gravity effect so i figured it would be as simple as an on and off switch
If just setting isshiftinggravity works, then just fix up that event and that's all you need (as well as creating the gravity version of the speedkarts):

Code: [Select]
registerOutputEvent("Vehicle", "SetGravity", "bool",1);

function Vehicle::SetGravity(%obj, %bool)
{
if(%bool == true)
%obj.isshiftinggravity=1;
else
%obj.isshiftinggravity=0;
}

Note that simply if(%bool) is fine, you'll see that a lot
I renamed the event from ToggleGravity to SetGravity. ToggleGravity would be a better name if you weren't providing an argument, and just inverting it from whatever state it currently is, but if you're giving an argument and setting it to that, then SetGravity is a more accurate description
« Last Edit: October 19, 2015, 07:09:57 AM by Headcrab Zombie »

Code: [Select]
registerOutputEvent("Vehicle", "SetGravity", "bool",1);

function Vehicle::SetGravity(%obj, %bool)
{
if(%bool == true)
%obj.isshiftinggravity=1;
else
%obj.isshiftinggravity=0;
}
You can actually make it even simpler by doing
Code: [Select]
registerOutputEvent("Vehicle", "SetGravity", "bool",1);

function Vehicle::SetGravity(%obj, %bool, %client)
{
%obj.isShiftingGravity = %bool;
}
Because it's just a boolean.

Also, note that the third argument of your function should be %client(being the person who triggered the event) because that's what the fourth argument of registerOutputEvent does if it's true.

You can actually make it even simpler by doing
Because it's just a boolean.
Err... Right.

Also, note that the third argument of your function should be %client(being the person who triggered the event) because that's what the fourth argument of registerOutputEvent does if it's true.
It shouldn't really matter as long as he's not trying to use %client, Torque functions are weird in that way, that you can pass more or less arguments then the function declares (not to say that's a good practice, though).
If he doesn't need client he could just take that out of the registerOutputEvent call

It shouldn't really matter as long as he's not trying to use %client, Torque functions are weird in that way, that you can pass more or less arguments then the function declares (not to say that's a good practice, though).
If he doesn't need client he could just take that out of the registerOutputEvent call
I was just pointing it out so that he knows what the last argument does.

i've given both scripts a spin and it works for the most part. the only issue it seems to have is that you can't exactly turn the gravity back on but it turns off just fine. i've tried setting the default value of the speedkart's %obj.isshiftinggravity to 0 or putting it in a comment completely but that didn't work.

I was just pointing it out so that he knows what the last argument does.
that clears it up a bit

would it help if i made these two completely seperate events, like GravityOn or GravityOff?

would it help if i made these two completely seperate events, like GravityOn or GravityOff?
No

Give me a sec and I'll look through the gravity jeep code
EDIT: Add shiftgravitycheck(%obj); after setting isshiftinggravity to 1
« Last Edit: October 19, 2015, 05:55:03 PM by Headcrab Zombie »

EDIT: Add shiftgravitycheck(%obj); after setting isshiftinggravity to 1
this worked perfectly! :o man am i happy haha

so a quick question before i close off the discussion: did the gravity check schedule stop when the vehicle gravity was set to 0 to sort optimize the unneeded constant checks? i need to get more into coding after this but if i'm remembering correctly, return handles this?