Log in to reply
 

[C#] How to get a closest ped with an parameters (Help pls)



  • Hi devs!

    I have one problem with getting the nearest ped with specific parameters. That is, I want to get the nearest Ped, but not just the first one that comes along, but for example the one with health < 50 and so on, does anyone know how to implement it, I know in addition to GetClosestPed(), there is also GetClosest<Ped>(), but I do not know how to use it. I would appreciate your help!)

    :coffee: :coffee: :coffee: :coffee: :coffee: :coffee: :coffee: :coffee: :coffee: :coffee: :coffee: :coffee:


  • MODERATOR

    You'll have process all peds to do your own sorting.

    Something like

    Ped closestPed = null;
    float closestDistance = 1000000.0f;
    foreach (Ped ped in allPeds)
    {
      if (ped.DistanceTo(player) < closestDistance &&
           ped.Health < 50)
      {
        closestPed = ped;
        closestDistance = ped.DistanceTo(player);
      }
    }
    

    (this probably doesn't compile but you get the idea)



  • @ikt im still a novice scripter but curious why you would test for a condition which apparently would always be true? Im referring to the distance.


  • MODERATOR

    @JohnFromGWN

    It's just an arbitrary choice. If you want to consider all peds and get the closest one to you, no matter how far they are, the initial value should be chosen to be very large so any ped matches, and from there it can get the closest one.

    A smaller value, such as 50, can be set, but your "closest" ped won't be detected if they're outside this radius. Since the OP hasn't specified a max radius I'll just check everything.



  • @ikt got it. Thanks



  • Just rip the GetClosest function and modify it to your liking.

    public static Ped GetClosestPedWithHealth(Vector3 position, params Ped[] peds)
    {
    	Ped closest = null;
    	float closestDistance = 3e38f;
    
    	foreach (var ped in peds)
    	{
    		if (ped.Health >= 50) continue; //skip peds with health not < 50
    	
    		var distance = position.DistanceToSquared(ped.Position);
    		if (distance <= closestDistance)
    		{
    			closest = ped;
    			closestDistance = distance;
    		}
    	}
    	return closest;
    }
    


  • @Jitnaught staying on topic, what does 3e38f mean? Im assuming it doesn't mean 3 to the power of 38 for this float.

    And is it possible to also test for gender, or i should ask how do you determine gender?
    Update: nvm for gender, found native PED_TYPE_CIVFEMALE



  • @JohnFromGWN 3e38f means 3 * 10^38, which is close to the max value of float (3.402... * 10^38)



  • @Jitnaught Thanks. I was afraid of that - one helluva overkill number, no?



  • Yes, a bit overkill, but there is no ill-effect for using float.MaxValue (or a value close to it) so might as well use that instead of some arbitrary number.



  • @Jitnaught With that range, you're going to get closets peds from GTA IV and GTA VI when it eventually is released.



  • Thank you so much, that is what i need!) ^з^


Log in to reply
 

Looks like your connection to GTA5-Mods.com Forums was lost, please wait while we try to reconnect.