Jump to content
Sign in to follow this  
download500

If (any of the peds I spawned) are within 50f of player then have (ped) do this

Recommended Posts

So depending on which option I choose, I could have any one of 8 cop ped objects dispatch to my location.

Regardless of which one I call, I want them to all have the same trigger once they get within a certain distance of me.

 

So basically I want to do something like

if Game.LocalPlayer.Character.Position.DistanceTo(((any one of my spawned cop peds).Position) <= 20.0F)

 

{

(That cops vehicle).IsSirenSilent = true;

}

I know I could do 8 different If/Then statements with one for each cop, but I figured there was an easier way.

How could I make this work?

Edited by download500

Share this post


Link to post
Share on other sites

List<Ped> MyCops = new List<Ped>();

MyCops.Add(cop1);
MyCops.Add(cop2); //etc

foreach (Ped cop in MyCops) {

if Game.LocalPlayer.Character.Position.DistanceTo(((any one of my spawned cop peds).Position) <= 20.0F) {

//yourcode

}}

Share this post


Link to post
Share on other sites
22 minutes ago, Albo1125 said:

List<Ped> MyCops = new List<Ped>();

MyCops.Add(cop1);
MyCops.Add(cop2); //etc

foreach (Ped cop in MyCops) {

if Game.LocalPlayer.Character.Position.DistanceTo(((any one of my spawned cop peds).Position) <= 20.0F) {

//yourcode

}}

You could also use a LINQ query, if you wanted to do some action on ALL cops if any one of them are within 20 metres.

int nearbyCops = (from x in myCops where x.DistanceTo(Game.LocalPlayer.Character.Position) <= 20.0f select x).Count();

if (nearbyCops > 0)
{
  foreach (Ped cop in myCops)
  {
    //do something
  }
}

//You can also get a list of nearby cops and only do an action on those nearby cops...

List<Ped> nearbyCopList = (from x in myCops where x.DistanceTo(Game.LocalPlayer.Character.Position) <= 20.0f select x).ToList();

if (nearbyCopList.Count > 0)
{
  foreach (Ped cop in nearbyCopList)
  {
    //do something
  }
}


//Or get the closest cop...

Ped closestCop = (from x in myCops where x.DistanceTo(Game.LocalPlayer.Character.Position) <= 20.0f select x).FirstOrDefault();
// if closestCop is null, no cop within 20 meters was found

//Or a list of cops sorted by how close they are...

List<Ped> nearbyCopList = (from x in myCops where x.DistanceTo(Game.LocalPlayer.Character.Position) <= 20.0f orderby x.DistanceTo(Game.LocalPlayer.Character.Position) select x).ToList();

 

Edited by Stealth22

Share this post


Link to post
Share on other sites

This is more of a general C# question as I ran into this issue as well in VB NET (I do office programming at work).

 

Can you create objects dynamically, that is, create a cop object every time a button is pressed and have it be a new cop object that gets added to the list you made above?

 

Or do I have to declare a finite number of cops in advance as Peds, and then once it has assigned the last cop ped I'm out?

 

In other words if I made 10 cop objects at the begging of my script, am I limited to spawning 10 cops from my button press?

Edited by download500

Share this post


Link to post
Share on other sites
32 minutes ago, download500 said:

This is more of a general C# question as I ran into this issue as well in VB NET (I do office programming at work).

 

Can you create objects dynamically, that is, create a cop object every time a button is pressed and have it be a new cop object that gets added to the list you made above?

 

Or do I have to declare a finite number of cops in advance as Peds, and then once it has assigned the last cop ped I'm out?

 

In other words if I made 10 cop objects at the begging of my script, am I limited to spawning 10 cops from my button press?

Nope, you can make them on the fly. As long as your list is globally declared, you're good.

Share this post


Link to post
Share on other sites

Hey stealth, the for each worked great than you. I used it for crowd control in one of my callouts where once you give an order to the group, each ped has a random individual chance to either obey and leave or taunt/fight you.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×