FindFirstPed etc.. in c#?

Are there any examples of findfirstped findnextped and endfindped for finding the nearest peds? I only know of entitiespool but that solution is pretty outdated and doesnt work sometimes.

I know that there are a few lua examples but idk how i would translate those into c# because they do things like
local handle, ped = FindFirstPed()

Also how does findnextped work?? Its a bool and u have to pass it an entity to find? I dont understand.

All help is appriciated

Actually, iterators are already implemented for you in the World class

usage example:

using CitizenFX.Core;
// other stuff

var peds = World.GetAllPeds();

There are more iterators available. See https://github.com/citizenfx/fivem/blob/master/code/client/clrcore/External/World.cs#L780

1 Like

Hmmm :grinning:

i dont think it works i already tryed it and it doesnt return any peds

I don’t have access to GTA/FiveM for the next couple of days but I know for sure that World.GetAllVehicles() works. I don’t see why the GetAllPeds helper method wouldn’t as it’s pretty much a copy of the commonly seen ped iterators of Lua.

It doesn’t return ALL peds, but only those around you. Maybe try GetAllVehicles() and see if vehicles are returned?

I’m pretty sure that I tried GetAllPeds like this:

Ped peds = World.GetAllPeds();

and the array was empty, even after having online players/AI around me. GetAllVehicles works fine though.

Alright that’s odd. I found some old code of mine where I iterate over Peds. Perhaps this will be of use

private async Task OnPedTick()
{
    try
    {
        await Delay(0);

        pedHandle = API.FindFirstPed(ref pedEntity);

        var playerPos = Game.PlayerPed.Position;

        do
        {
            pedSuccess = API.FindNextPed(pedHandle, ref pedEntity);
            var pedPos = API.GetEntityCoords(pedEntity, true);
            var pedDistance = API.GetDistanceBetweenCoords(pedPos.X, pedPos.Y, pedPos.Z, playerPos.X, playerPos.Y, playerPos.Z, true);

            if(API.IsPedInAnyVehicle(API.GetPlayerPed(-1), true) == false)
            {
                if (API.DoesEntityExist(pedEntity))
                {
                    if(API.IsPedDeadOrDying(pedEntity, true) == false)
                    {
                        if(API.IsPedInAnyVehicle(pedEntity, true) == false)
                        {
                            var pedType = API.GetPedType(pedEntity);

                            if(pedType != 28 && API.IsPedAPlayer(pedEntity) == false)
                            {
                                pedCurrentPos = pedPos;

                                if(pedDistance <= 3 && pedEntity != API.GetPlayerPed(-1) && pedEntity != pedOld)
                                {
                                    if (_isChecked == false)
                                        TriggerServerEvent("drugsell:check");

                                    if (playerHas)
                                    {
                                        Screen.DisplayHelpTextThisFrame("Press ~INPUT_VEH_HEADLIGHT~ to start selling ~b~");
                                        if (Game.IsControlJustPressed(1, Control.VehicleHeadlight))
                                        {
                                            pedOld = pedEntity;
                                            API.SetEntityAsMissionEntity(pedEntity, true, true);
                                            API.ClearPedTasks(pedEntity);
                                            API.FreezeEntityPosition(pedEntity, true);
                                            var sellRandom = rnd.Next(1, 12);
                                            if(sellRandom != 1 && sellRandom % 2 == 1)
                                            {
                                                Screen.ShowSubtitle("~r~Person rejected your offer!", 2000);
                                                _isSelling = false;
                                                API.SetEntityAsMissionEntity(pedEntity, true, true);
                                                API.SetPedAsNoLongerNeeded(ref pedEntity);
                                            }
                                            else
                                            {
                                                API.TaskStandStill(pedEntity, 9000);
                                                pedCurrentPosNew = API.GetEntityCoords(pedEntity, true);
                                                _isSelling = true;
                                                _secondsRemaining = 10;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }


        } while (pedSuccess);

        API.EndFindPed(pedHandle);
    }
    catch (Exception ex)
    {
        Debug.WriteLine(ex + " : Exception thrown on DrugWeed#OnPedTick()");
    }
}

Keep in mind that the code is OLD and can be improved a lot, but I remember this working

One of my friends are testing a bit so if he get it to work I’m gonna send it

this is what I personally use and it works

   public static int GetPedInFront(int ignore, float range = 2.00f)
   {
       try
       {
           int playerCount = GetNumberOfPlayers();
           Vector3 ignorePos = GetEntityCoords(ignore, true);

           for (int i = 0; i < playerCount; i++)
           {
               int ped = GetPlayerPed(i);
               if (ped != ignore)
               {
                   Vector3 pedPos = GetEntityCoords(ped, true);

                   float distance = GetDistanceBetweenCoords(pedPos.X, pedPos.Y, pedPos.Z, ignorePos.X, ignorePos.Y, ignorePos.Z, false);
                   if (distance < range)
                   {
                       return ped;
                   }
               }
           }
       }
       catch (Exception ex)
       {
           Debug.WriteLine($"Ex -> {ex}");
       }

       return 0;
   }

This works just fine for me. Are you sure you’re using an updated client reference file?

RegisterCommand("peds", new Action<dynamic, List<dynamic>, string>((dynamic source, List<dynamic> args, string rawCommand) =>
{
    string pedHandles = "";
    foreach (var p in World.GetAllPeds())
    {
        pedHandles += p.Handle + ", ";
    }
    Debug.WriteLine(pedHandles.Trim().Trim(','));
}), false);

Output in the console after typing /peds in the chat:

514, 6658, 28930, 7170, 29186, 31746, 32002, 22530, 32258, 8962, 29698, 23042, 29954, 23298, 9474, 23554, 30210, 24066, 32514, 10242, 11010, 11266, 32770, 11522, 30722, 25346, 25602, 30978, 33026, 26114, 33282, 26626, 33538, 5634, 31490, 27906, 20738, 15106, 28162, 28418, 28674
1 Like

yes um i had a citizenfx.dll as reference from last year xd i got a new one and it fixed it im sry

1 Like

NP, glad it’s working for you.

I hope not come too late…

public async void CheckPeds()
        {
            while (true)
            {
                int handle = -1;
                bool success;
                int ped = FindFirstPed(ref handle);
                Debug.WriteLine($"FindFirstPed: {ped}. {handle}");
                do
                {
                    //await Delay(1);

                    Vector3 PedsCoords = GetEntityCoords(handle, false);
                    Vector3 PlayerCoords = GetEntityCoords(PlayerPedId(), false);
                    World.DrawLine(PedsCoords, PlayerCoords, Color.FromArgb(255, 0, 0));

                    success = FindNextPed(ped, ref handle);
                    Debug.WriteLine($"FindNextPed: {success}, {handle}");
                } while (success);
                

                EndFindPed(ped);
                Debug.WriteLine($"EndFindPed: {ped} {handle}");


                await Delay(0);
            }
        }

how do I return a character skin?