letsplayordy

[GTAV] Get closest peds and vehicles functions

May 22nd, 2015
569
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //#include <stack>
  2.  
  3. std::stack<Any> GetNearbyPeds(Ped ped, float radius, int maxAmount)
  4. {
  5.     std::stack<Any> peds;
  6.     int *handles = new int[maxAmount * 2 + 2];
  7.     //0 index is the size of the array
  8.     handles[0] = maxAmount;
  9.  
  10.     int count = PED::GET_PED_NEARBY_PEDS(PLAYER::PLAYER_PED_ID(), (Any*)handles, -1);
  11.  
  12.     if (handles != NULL)
  13.     {
  14.         for (int i = 0; i < count; i++)
  15.         {
  16.             int offsettedID = i * 2 + 2;
  17.  
  18.             if (handles[offsettedID] != NULL && ENTITY::DOES_ENTITY_EXIST(handles[offsettedID]))
  19.             {
  20.                 Vector3 pedCoord = ENTITY::GET_ENTITY_COORDS(ped, 1);
  21.                 Vector3 ped2Coord = ENTITY::GET_ENTITY_COORDS(handles[offsettedID], 1);
  22.                 if (GAMEPLAY::GET_DISTANCE_BETWEEN_COORDS(pedCoord.x, pedCoord.y, pedCoord.z, ped2Coord.x, ped2Coord.y, ped2Coord.z, 1) <= radius)
  23.                 {
  24.                     peds.push(handles[offsettedID]);
  25.                 }
  26.             }
  27.         }
  28.     }
  29.     return peds;
  30. }
  31.  
  32. std::stack<Any> GetNearbyVehicles(Ped ped, float radius, int maxAmount)
  33. {
  34.     std::stack<Any> vehs;
  35.     int *handles = new int[maxAmount * 2 + 2];
  36.     //0 index is the size of the array
  37.     handles[0] = maxAmount;
  38.  
  39.     int count = PED::GET_PED_NEARBY_VEHICLES(PLAYER::PLAYER_PED_ID(), (Any*)handles);
  40.  
  41.     if (handles != NULL)
  42.     {
  43.         for (int i = 0; i < count; i++)
  44.         {
  45.             int offsettedID = i * 2 + 2;
  46.  
  47.             if (handles[offsettedID] != NULL && ENTITY::DOES_ENTITY_EXIST(handles[offsettedID]))
  48.             {
  49.                 Vector3 pedCoord = ENTITY::GET_ENTITY_COORDS(ped, 1);
  50.                 Vector3 vehCoord = ENTITY::GET_ENTITY_COORDS(handles[offsettedID], 1);
  51.                 if (GAMEPLAY::GET_DISTANCE_BETWEEN_COORDS(pedCoord.x, pedCoord.y, pedCoord.z, vehCoord.x, vehCoord.y, vehCoord.z, 1) <= radius)
  52.                 {
  53.                     vehs.push(handles[offsettedID]);
  54.                 }
  55.             }
  56.         }
  57.     }
  58.     return vehs;
  59. }
  60.  
  61.  
  62. //example nearby vehicles
  63. std::stack<Any> vehs = GetNearbyVehicles(PLAYER::PLAYER_PED_ID(), 100, 10000);
  64. while (!vehs.empty())
  65. {
  66.     if (vehs.top() != NULL && ENTITY::DOES_ENTITY_EXIST(vehs.top()))
  67.     {
  68.         //do stuff
  69.     }
  70.     vehs.pop();
  71. }
  72.  
  73. //example nearby peds
  74. std::stack<Any> peds = GetNearbyPeds(PLAYER::PLAYER_PED_ID(), 100, 10000);
  75. while (!peds.empty())
  76. {
  77.     if (peds.top() != NULL && ENTITY::DOES_ENTITY_EXIST(peds.top()))
  78.     {
  79.         //do stuff
  80.     }
  81.     peds.pop();
  82. }
RAW Paste Data