Advertisement
R3QQ

getClosestPed example (GTAV scripting in C++)

Jul 5th, 2020
822
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. float getDistanceBetweenVectors(Vector3 vector1, Vector3 vector2){
  2.     return SYSTEM::VDIST(vector1.x, vector1.y, vector1.z, vector2.x, vector2.y, vector2.z);
  3. }
  4.  
  5. bool doesEntityExistsAndIsNotNull(Entity entity)
  6. {
  7.     return (entity != NULL && ENTITY::DOES_ENTITY_EXIST(entity));
  8. }
  9.  
  10. Ped getPlayerPed()
  11. {
  12.     return PLAYER::PLAYER_PED_ID();
  13. }
  14.  
  15. Ped getClosestPed(Vector3 point, float maxDistance) // distance in meters
  16. {
  17.     Ped ped = NULL;
  18.     float shortestDistance = maxDistance; // start value
  19.     const int ARR_SIZE = 1024;
  20.     Ped peds[ARR_SIZE];
  21.     int count = worldGetAllPeds(peds, ARR_SIZE);
  22.  
  23.     if (peds != NULL)
  24.     {
  25.         for (int i = 0; i < count; i++)
  26.         {
  27.             if (doesEntityExistsAndIsNotNull(peds[i]))
  28.             {
  29.                 Vector3 pedCoords = ENTITY::GET_ENTITY_COORDS(peds[i], true);
  30.                 float distanceFound = getDistanceBetweenVectors(point, pedCoords);
  31.                 if (distanceFound <= shortestDistance)
  32.                 {
  33.                     shortestDistance = distanceFound;
  34.                     ped = peds[i];
  35.                 }
  36.             }
  37.         }
  38.     }
  39.     return ped;
  40. }
  41.  
  42. void howToUseExample(){
  43.     if (doesEntityExistsAndIsNotNull(getPlayerPed()))
  44.     {
  45.         Vector3 playerCoords = ENTITY::GET_ENTITY_COORDS(getPlayerPed(), true);
  46.         Ped closestPedToPlayerPos = getClosestPed(playerCoords, 100);
  47.         if (doesEntityExistsAndIsNotNull(closestPedToPlayerPos))
  48.         {
  49.             // Do something with closestPedToPlayerPos
  50.         }
  51.     }
  52.  
  53. }
  54.  
  55. // Fixed a few things
Advertisement
RAW Paste Data Copied
Advertisement