[Tutorial] Entering RC Vehicles with OnPlayerKeyStateChange
#1

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.

pawn Код:
#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.

pawn Код:
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.

pawn Код:
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.
pawn Код:
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:
pawn Код:
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:
pawn Код:
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.
Reply
#2

Why you do create new array then loop through it while that function only requires simple switch?

pawn Код:
stock IsVehicleRCVehicle(vehicleid)
{
     switch(GetVehicleModel(vehicleid))
     {
          case 441,464,465,501,564,594: return 1;
     }
     return 0;
}
Reply
#3

Didn't notice that, I'll change it. Thanks ^^
Reply
#4

Another great tutorial by you Hiddos i'll test it when i get home but looks great
Reply
#5

Nice tutorial Hiddos ^_^
Reply
#6

this is nice TuT man D
Reply
#7

Nice
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)