R3QQ

getClosestVehicle, ped, obj etc. Examples. (GTAV mod, C++)

Jul 5th, 2020
364
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.56 KB | None | 0 0
  1. // Using Script Hook V SDK
  2. // dev-c.com/gtav/scripthookv/
  3.  
  4. // Gets the closest ped, vehicle, object or pickup depending on function used.
  5.  
  6. const int ARR_SIZE = 1024;
  7.  
  8. Ped getPlayerPed()
  9. {
  10.     return PLAYER::PLAYER_PED_ID();
  11. }
  12.  
  13. bool doesEntityExistsAndIsNotNull(Entity entity)
  14. {
  15.     return (entity != NULL && ENTITY::DOES_ENTITY_EXIST(entity));
  16. }
  17.  
  18. float getDistanceBetweenVectors(Vector3 vector1, Vector3 vector2)
  19. {
  20.     return SYSTEM::VDIST(vector1.x, vector1.y, vector1.z, vector2.x, vector2.y, vector2.z);
  21. }
  22.  
  23. Entity getEntityClosestToCoords(Entity entities[], int count, Vector3 coords, float maxDistance)
  24. {
  25.     float shortestDistance = maxDistance; // start value
  26.     Entity closestEntity = NULL;
  27.  
  28.     if (entities != NULL)
  29.     {
  30.         for (int i = 0; i < count; i++)
  31.         {
  32.             if (doesEntityExistsAndIsNotNull(entities[i]))
  33.             {
  34.                 Vector3 entCoords = ENTITY::GET_ENTITY_COORDS(entities[i], true);
  35.                 float distanceFound = getDistanceBetweenVectors(coords, entCoords);
  36.                 if (distanceFound <= shortestDistance)
  37.                 {
  38.                     shortestDistance = distanceFound;
  39.                     closestEntity = entities[i];
  40.                 }
  41.             }
  42.         }
  43.     }
  44.     return closestEntity;
  45. }
  46.  
  47. Ped getPedClosestToCoords(Vector3 coords, float maxDistance)
  48. {
  49.     Ped peds[ARR_SIZE];
  50.     int count = worldGetAllPeds(peds, ARR_SIZE);
  51.     return getEntityClosestToCoords(peds, count, coords, maxDistance);
  52. }
  53.  
  54. Object getObjectClosestToCoords(Vector3 coords, float maxDistance)
  55. {
  56.     Object objects[ARR_SIZE];
  57.     int count = worldGetAllObjects(objects, ARR_SIZE);
  58.     return getEntityClosestToCoords(objects, count, coords, maxDistance);
  59. }
  60.  
  61. Vehicle getVehicleClosestToCoords(Vector3 coords, float maxDistance)
  62. {
  63.     Vehicle vehicles[ARR_SIZE];
  64.     int count = worldGetAllVehicles(vehicles, ARR_SIZE);
  65.     return getEntityClosestToCoords(vehicles, count, coords, maxDistance);
  66. }
  67.  
  68. Pickup getPickupClosestToCoords(Vector3 coords, float maxDistance)
  69. {
  70.     Pickup pickups[ARR_SIZE];
  71.     int count = worldGetAllPickups(pickups, ARR_SIZE);
  72.     return getEntityClosestToCoords(pickups, count, coords, maxDistance);
  73. }
  74.  
  75. // Example of usage: Getting the closest vehicle from player's coords and make it explode.
  76. void getClosestEntityDemo()
  77. {
  78.     if (doesEntityExistsAndIsNotNull(getPlayerPed()))
  79.     {
  80.         Vector3 playerCoords = ENTITY::GET_ENTITY_COORDS(getPlayerPed(), true);
  81.         Vehicle closestVeh = getVehicleClosestToCoords(playerCoords, 100); // Distance in meters
  82.         if (doesEntityExistsAndIsNotNull(closestVeh))
  83.         {
  84.             VEHICLE::EXPLODE_VEHICLE(closestVeh, true, false);
  85.             //VEHICLE::DELETE_VEHICLE(&closestVeh);
  86.             //VEHICLE::SET_VEHICLE_COLOURS(closestVeh, 0, 0);
  87.         }
  88.     }
  89. }
Add Comment
Please, Sign In to add comment