SA-MP Forums Archive
[Tutorial] Entering RC Vehicles with OnPlayerKeyStateChange - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+---- Forum: Tutorials (https://sampforum.blast.hk/forumdisplay.php?fid=70)
+---- Thread: [Tutorial] Entering RC Vehicles with OnPlayerKeyStateChange (/showthread.php?tid=158074)



Entering RC Vehicles with OnPlayerKeyStateChange - Hiddos - 08.07.2010

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.


Re: Entering RC Vehicles with OnPlayerKeyStateChange - Sergei - 09.07.2010

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;
}



Re: Entering RC Vehicles with OnPlayerKeyStateChange - Hiddos - 09.07.2010

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


Re: Entering RC Vehicles with OnPlayerKeyStateChange - Cameltoe - 09.07.2010

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


Re: Entering RC Vehicles with OnPlayerKeyStateChange - iZN - 13.07.2010

Nice tutorial Hiddos ^_^


Re: Entering RC Vehicles with OnPlayerKeyStateChange - Aleluja - 13.07.2010

this is nice TuT man D


Re: Entering RC Vehicles with OnPlayerKeyStateChange - Nanjaya - 23.06.2013

Nice