Log in to reply
 

How to get peds within a 5f radius without picking up playerPed?



  • I'm trying to get a function to return all peds within a radius of 5 around the player, without including the playerPed.

    Currently, I am using Ped[] pGroup = World.GetNearbyPeds(playerPed.Position, 5f, ModelHash) and using the ModelHash as a scapegoat until I figure out how to use GetNearbyPeds with returning the playerPed.

    I use Ped p = pGroup[0] to reference the closest Ped in the group, but when doing that without the ModelHash, it always returns the players ped.



  • Use the GetNearbyPeds overload that takes a Ped parameter instead of a Vector3 parameter. This will use the Ped parameters position and will not include the Ped in the returned array.

    P.S. GetNearbyPeds is not sorted by distance to position, so getting the first Ped in the array will not always be the most nearby Ped.



  • @Jitnaught Do you have any tips on sorting the peds by distance? Thank you for clearing that up by the way, it was so simple smh.



  • @dev614 If you want to just find the closest ped, write a loop that goes through all of the peds and checks the distance to the player. If the distance is smaller than the last then save the distance and ped to variables.

    Something like this

    float maxDistance = 50f;
    Ped[] peds = World.GetNearbyPeds(..., maxDistance);
    Ped closestPed = null;
    float lastDistance = maxDistance;
    foreach (Ped ped in peds)
    {
       float distance = ped.Position.DistanceTo(Game.Player.Character.Position);
       if (distance < lastDistance)
       {
          closestPed = ped;
          lastDistance = distance;
        }
    }
    if (ped != null)
    {
       //do whatevs
    }


  • @Jitnaught You're awesome, thank you so much!

    I'm working on adding a "recruit peds" feature to Eddlms Bodyguard Squads script and it's nearly complete. The only thing that I can't seem to figure out is how to get recruited peds to stand still until tasking/after completing task.

    Would you happen to know how to make a ped (in its own group) stand still until being tasked? And then after completing the task, stand still until being tasked again?

    Here's what I have so far:

        pedsGroup = World.GetNearbyPeds(player.Character, 5f);
        Squad1Leader = pedsGroup[0];
        Squad1Group = Function.Call<int>(Hash.CREATE_GROUP);
        PreparePed(Squad1Leader, MainWeaponSquad1, SecondaryWeaponSquad1);
    
        Function.Call(GTA.Native.Hash.SET_GROUP_FORMATION, Squad1Group, 1);
        Function.Call(GTA.Native.Hash.SET_GROUP_FORMATION_SPACING, Squad1Group, 1.0f, 1.0f, 1.0f);
        Function.Call(Hash.REMOVE_PED_FROM_GROUP, Squad1Leader);
        if (Squad1Followplayer.Checked) { Squad1Followplayer.Checked = false; }
        if (Squad1.Count > 0)
        {
            Function.Call(Hash.SET_PED_AS_GROUP_MEMBER, Squad1Leader, Squad1Group);
        }
        else
        {
            Function.Call(Hash.SET_PED_AS_GROUP_LEADER, Squad1Leader, Squad1Group);
        }
        Squad1.Add(Squad1Leader);
        Function.Call(Hash.SET_PED_NEVER_LEAVES_GROUP, Squad1Leader, true);
        foreach (Ped recruit in Squad1)
        {
            recruit.RelationshipGroup = Squad1RelationshipGroup;
            Function.Call(Hash.SET_PED_SHOOT_RATE, recruit, 1000);
            Function.Call(Hash.SET_PED_COMBAT_ABILITY, recruit, 100);
            recruit.AddBlip();
            recruit.CurrentBlip.Color = BlipColor.Blue;
            recruit.CurrentBlip.Scale = 0.7f;
            SetBlipName(recruit.CurrentBlip, "Squad Member");
            PreparePed(recruit, MainWeaponSquad1, SecondaryWeaponSquad1);
            recruit.AlwaysKeepTask = true;
        }


  • @dev614 try

    recruit.Tasks.StandStill(-1);
    


  • @stillhere said in How to get peds within a 5f radius without picking up playerPed?:

    recruit.Tasks.StandStill(-1);

    That works when recruiting the ped, but after ordering them to run to a location, after completing the task the ped begins to wander again. :(



  • @dev614 Ah I understand, you may want to use a TaskSequence.

    TaskSequence sequence = new TaskSquence();
    sequence.AddTask.RunTo(....);
    sequence.AddTask.StandStill(-1);
    sequence.Close();
    

    then when you want to make the ped do the tasks:

    recruit.Task.PerformSequence(sequence);
    


  • @stillhere That would require me picking apart Eddlm's order functions and trying to redo all the tasks, which from the looks of it isn't possible because to add them to the sequence you would have to use the sequence.AddTask method, which doesn't use all of the task functions in the order function.

    Would there be a way to check if a ped has completed a task, and if so, to stand still afterwards? Something to run in an onTick?



  • @dev614 I thought you were making your own bodyguard script, my mistake.
    Do the peds spawned by Eddlm's script wander off as well? If not then there's probably a flag or some function applied to his spawned peds that you haven't applied to your recruited peds, which in that case I'd ask him.
    It would certainly be easier than having to constantly loop through the recruits, checking for tasks. But to answer your question, it seems possible: http://gtaforums.com/topic/896723-help-with-get-script-task-status-and-get-is-task-active/#entry1069845125



  • @stillhere Unfortunately Eddlm's a bit caught up with real life things, and no they do not. I tried to search the code for the portion that controls the squads spawning, yet it doesn't seem to contain anything special. I think since the created ped had no tasks to begin with, it just stands still regardless - so after being tasked, it returns to its default state - standing still. That's just whats makes me feel good about myself though, I haven't actually tried to look into it because it's beyond my comprehension tbh. I'll give that link a go and continue to take shots in the dark hoping to make something happen!

    Here's the code used to create a squad: https://pastebin.com/34MXYjmv



  • @dev614 Hm well I would try running recruit.Tasks.ClearAll(); before recruit.AlwaysKeepTask = true;
    I'm not sure if Eddlm's script creates a new Group for the squad or if his spawned peds are just added to the player group, but it won't hurt to try adding your recruits to the default player group (PLAYER::GET_PLAYER_GROUP) instead of creating a new group.

    Here's a slightly old version of the script's source, posted on November 1st 2015 (the last update on the mod page was January 17, 2016).



  • @Jitnaught Hey, could you assist me in setting up that loop to check for the closest ped? I need the ped to be named "Squad1Leader", am I setting this up right?

        float maxDistance = 25f;
        pedsGroup = World.GetNearbyPeds(player.Character, maxDistance);
        Ped ped = null;
        float lastDistance = maxDistance;
        foreach (Ped Squad1Leader in pedsGroup)
        {
            float distance = Squad1Leader.Position.DistanceTo(player.Character.Position);
            if (distance > lastDistance)
            {
                ped = Squad1Leader;
                lastDistance = distance;
            } 
        }
        if (Squad1Leader != null)
        {


  • @dev614 Switch "ped" and "Squad1Leader". Also "distance > lastDistance" should be "distance < lastDistance"

    float maxDistance = 25f;
    pedsGroup = World.GetNearbyPeds(player.Character, maxDistance);
    Ped Squad1Leader = null;
    float lastDistance = maxDistance;
    foreach (Ped ped in pedsGroup)
    {
        float distance = ped.Position.DistanceTo(player.Character.Position);
        if (distance < lastDistance)
        {
            Squad1Leader = ped;
            lastDistance = distance;
        } 
    }
    if (Squad1Leader != null)
    {


  • @Jitnaught Using that code seems to add the member to the group, but doesn't work how the pedGroup[0] was working as far as making them the leader and whatnot, I'm not sure why? I'm going to post the entire code and if you're willing, if you could take a look and see what changes I may have to make I'd be super appreciative. If not it's totally fine, you shouldn't have to hold my hand through this, but I'm just now learning to code so it's amateur hour xD

    Here's the code for the recruit function: https://pastebin.com/XtUR5VYy



  • @dev614 What doesn't work?



  • @Jitnaught Apparently the loop. When using pedGroup[0] it's working as expected and adding the Ped in slot [0] in pedGroup to Squad1Group and making them the leader, thus being able to be commanded. When using your loop, it seems to make the ped join the group (they stop and stand still) but they don't appear on the Squad UI and I cannot command them, meaning they're not the leader.



  • @dev614 Is all the code after the loop the same as when you're only using pedGroup[0]?



  • @Jitnaught Yessir, here's the codes:

    Working code with pedGroup[0]: https://pastebin.com/q3MTx3JS

    Nonworking code with loop: https://pastebin.com/yX7DSqpT

    Maybe since Squad1Leader is declared null, the other functions (such as the order functions) can't pick it up, so the "Squad1Leader" variable isn't used for the UI or the orders functions? I don't know, just taking a shot in the dark.



  • @dev614 The code only adds Squad1Leader if it is not null, so that shouldn't be a problem.
    Sorry I can't tell why it wouldn't be working. I would suggest debugging using Visual Studio (attach to GTA V process), setting breakpoints and seeing if all the code is being called, Squad1Leader is being added, etc.


Log in to reply
 

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