PDA

View Full Version : [Tutorial] Entering RC Vehicles with OnPlayerKeyStateChange


Hiddos
08/07/2010, 05:20 PM
I'll be using OnPlayerKeyStateChange here for entering RC vehicles.

Here's what we're gonna use:
A define for the range of when the player can or can't enter a RC vehicle
OnPlayerKeyStateChange

Enough shit, let's get started:

Put this above your script, but under your #includes. You might wish to enter the range without a new #define though.

#define RC_ENTER_RANGE 8 // Range in foot


Here's just a basic stock function I made, you might like to use it for further scripting as well.
Put it down your script (Anywhere will do, but I usually do it completely down).
Thanks to Sergei for showing this method.

stock IsVehicleRCVehicle(vehicleid)
{
switch(GetVehicleModel)
{
case 441,464,465,501,564,594: return 1;
}
return 0;
}

Now the hard work is done, this'll be easy. Or maybe not.

public OnPlayerKeyStateChange(playerid,newstate,oldstate)
{
if(newkeys & 16) // Is the 'key value' of the enter vehicle control.
Beginning of our OnPlayerKeyStateChange callback.
Here's the part for exiting the vehicle. Now it may sound weird why exiting goes first, but it's easier.
new Float:x,Float:y,Float:z;
if(IsPlayerInAnyVehicle(playerid) && IsVehicleRCVehicle(GetPlayerVehicleID(playerid)) // Checks if the player is in a vehicle and, if so, if the vehicle is a RC vehicle.
{
GetVehiclePos(GetPlayerVehicleID(playerid),x,y,z);
SetPlayerPos(playerid,x,y,z);
return 1; // OnPlayerKeyStateChange won't be executed any further.
}
You might be wondering why I won't simply use 'RemovePlayerFromVehicle', as it does almost the same. The weird thing is, this function doesn't work with RC vehicles. Trailers unconfirmed.

Now for entering a vehicle:
for(new v; v < MAX_VEHICLES; v++) // Loop through all vehicles
{
GetVehiclePos(v,x,y,z);
if(IsPlayerInRangeOfPoint(playerid,RC_ENTER_RANGE, x,y,z) && IsVehicleRCVehicle(v)) //Check if the player is near a RC vehicle.
{
PutPlayerInVehicle(playerid,v,0); // Put the player in the RC vehicle
return 1; // OnPlayerKeyStateChange won't be executed any further.
}
}

So your OnPlayerKeyStateChange should now look like this:
public OnPlayerKeyStateChange(playerid,newstate,oldstate)
{
if(newkeys & 16) // Is the 'key value' of the enter vehicle control.new Float:x,Float:y,Float:z; Scroll down for more information about this '&'
{
if(IsPlayerInAnyVehicle(playerid) && IsVehicleRCVehicle(GetPlayerVehicleID(playerid)) // Checks if the player is in a vehicle and, if so, if the vehicle is a RC vehicle.
{
GetVehiclePos(GetPlayerVehicleID(playerid),x,y,z);
SetPlayerPos(playerid,x,y,z);
return 1; // OnPlayerKeyStateChange won't be executed any further.
}
for(new v; v < MAX_VEHICLES; v++) // Loop through all vehicles
{
GetVehiclePos(v,x,y,z);
if(IsPlayerInRangeOfPoint(playerid,RC_ENTER_RANGE, x,y,z) && IsVehicleRCVehicle(v)) //Check if the player is near a RC vehicle.
{
PutPlayerInVehicle(playerid,v,0); // Put the player in the RC vehicle
return 1; // OnPlayerKeyStateChange won't be executed any further.
}
}
}
return 1;
}

*The '&' in a check ('if()', for example), checks if the value is in the given variable. You could do it with if(newkeys== 16), but then it'd check if he only presses the enter button. So in this case, the '&' checks if a player presses the Enter button, and it doesn't minds if he presses another key.

Hope you could do something with this. Here's a little filterscript you might like to use, in-case the above doesn't works.
Link: http://pastebin.com/hn6dBpNR

Questions may be asked in this thread.

- Hiddos was here.

Sergei
09/07/2010, 10:09 AM
Why you do create new array then loop through it while that function only requires simple switch?

stock IsVehicleRCVehicle(vehicleid)
{
switch(GetVehicleModel(vehicleid))
{
case 441,464,465,501,564,594: return 1;
}
return 0;
}

Hiddos
09/07/2010, 10:19 AM
Didn't notice that, I'll change it. Thanks ^^

Cameltoe
09/07/2010, 10:26 AM
Another great tutorial by you Hiddos :D i'll test it when i get home :) but looks great :)

iZN
13/07/2010, 10:37 AM
Nice tutorial Hiddos ^_^

Aleluja
13/07/2010, 11:52 AM
this is nice TuT man :DD

Nanjaya
23/06/2013, 08:33 AM
Nice :D