Can't GetAllPeds in C#

I’ve been a little stumped why this call is always returning an empty list even when I was st anding right next to a Ped, also with a wider range.

var peds = World.GetAllPeds()
                .Where(p => p.IsPlayer == false && p.IsInRangeOf(_playerCoords, 3.0f))
                .Select(p => p.Handle)
                .ToList();

I tried this if place of the IsInRangeOf check:

GetDistanceBetweenCoords(
                _playerCoords.X, _playerCoords.Y, _playerCoords.Z,
                p.Position.X, p.Position.Y, p.Position.Z,
                true) < 50.0f)

So I stripped it right back and this returns an empty list:

var peds = World.GetAllPeds();

How are we supposed to get peds in the system when the basic API isn’t functioning?

MM, I think it’s becuase it’s a Server only function. In which case I fail to see why this method is available in a client side C# library when I’ve referenced CitizenFX.Core.Client nuget package. Why have a library that provides compile time access to methods which aren’t supposed to be available in that layer?

Does anyone have an example of using GetPedNearbyPeds(…) from C#?

MM, it isn’t.

OK, Thanks for the reply. So why when I’m standing next to peds do I get an empty list?
By the way the doco clearly shows it as server only
image
When I click “client” in the API Set selector the GetAllPeds function disappears.

‘By the way’, the ‘doco’ is not for any language-specific wrappers. You can see what calls the implementation uses.

No idea, could be anything.

Do you intentionally put the sarcasm in when you start your responses?

I really appreciate your time, having that source reference really helps me to understand what’s going on internally.

1 Like

For reference, There’s definitely a ped in game:


And this is my tick handler:

private async Task GetClosestPedInRange()
        {
            await Delay(100);

            if (IsPedInAnyVehicle(_player, true))
                return;

            int entHandle = -1;
            int handle = API.FindFirstPed(ref entHandle);
            Ped ped = (Ped)Entity.FromHandle(entHandle);
            Debug.WriteLine($"handle: {handle}, entHandle: {entHandle}");
}

It could be a script forgot to call END_FIND_PED and you’ve run out of find handles.

But entHandle and handle are supposed to be written to multiple times before EndFindPed is reached and in my case it’s always -1. The World.GetAllPeds() also fails to find anything and it definitely calls EndFindPed

I have a C# script using the CitizenFx.Core.Client NuGet package version 1.0.2967 (server 3524), and the following code returns peds just fine. :slight_smile:

private Ped[] GetLivingPeds()
{
    return World.GetAllPeds()?
    .Where(x => (x?.IsAlive ?? false))?
    .ToArray();
}

Maybe run this particular code, and count the number of entries in the array before you start filtering it any further?

Thanks, will try it out.
WOuld be great if you could pass your own Expression<Func<…>> to add on where conditions rather than do it after the call. Encapsulate all in one go

I created extension methods for the Ped[] array that suit my needs. Can probably be optimized, but feel free to mess around with it. :slight_smile:

Of course you can replace the .Where(x => ...) with whatever suits your needs. I just provided an example code that I know to be working on the versions I provided.

In my case, I need two different types of data from living peds, so I filter it after the fact.

public static class ExtensionMethods
{
    /// <summary>
    /// Retrieve the nearest human that's on foot
    /// </summary>
    /// <param name="peds"></param>
    /// <param name="ped"></param>
    /// <returns></returns>
    public static Ped GetNearestHumanOnFoot(this Ped[] peds, Ped ped)
    {
        if (!(ped?.Exists() ?? false))
        {
            return default;
        }

        return peds?
                .Where(x => (x?.IsHuman ?? false) && (!x?.IsInVehicle() ?? false))?
                .OrderBy(x => x?.Position.DistanceToSquared(ped.Position))?
                .FirstOrDefault();
    }

    /// <summary>
    /// Retrieve the closest player ped
    /// </summary>
    /// <param name="peds"></param>
    /// <param name="ped"></param>
    /// <returns></returns>
    public static Ped GetNearestPlayer(this Ped[] peds, Ped ped)
    {
        if (!(ped?.Exists() ?? false))
        {
            return default;
        }

        return peds?
                .Where(x => x?.IsPlayer ?? false)?
                .OrderBy(x => x?.Position.DistanceToSquared(ped.Position))?
                .FirstOrDefault();
    }
}

Thing is it just calls GetAllPeds anyway

Yes, and with the NuGet package + server versions I listed, that function is definitely working client side. :slight_smile: