Jump to content
Sign in to follow this  
download500

Game spawns infinite number of peds until crash, wondering why

Recommended Posts

The following is in my baseprocess for my callout. After a minimum amount of time, there is a chance that a cop will  find your suspect depending on who you're already with, or if you've already found him.

 

It works, except whenever the cop finds the suspect, the commands to spawn him and his car repeat infinitely making dozens of copies of it until the game crashes. This shouldn't happen because once otherCopFound becomes true, the script should never run again (based on the criteria of the first if statement.

 

Any idea why it's creating a continuous loop?

 

 base.Process();


            GameFiber.StartNew(delegate
            {
                




                if (foundPerp == false & otherCopFound == false)


                {

                    GameFiber.StartNew(delegate
                    {

                        int wait = 10000;

                    //wait = myRand.Next(60000, 600000);

                    GameFiber.Sleep(wait);


                    if (cophelps > 1) //begin cop helps

                    {

                        myBlip = myPed.AttachBlip();
                     

                        if (fledinvehicle == 1)
                        {
                            Game.DisplaySubtitle("Other Unit: I've located the subject.", 5000);
                        }
                        else
                        {
                            Game.DisplaySubtitle("Other Unit: I've located the subject's vehicle, initiating stop.", 5000);
                        }
                        

                        int perpflees = myRand.Next(1, 10);


                        if (perpflees == 2) 

                        {
                            if (myPed.IsFleeing == false)
                            {
                                Functions.AddPedToPursuit(this.pursuit, this.myPed);

                                GameFiber.Sleep(10000);
                                Functions.RequestBackup(myPed.Position.Around(5f), LSPD_First_Response.EBackupResponseType.Pursuit, LSPD_First_Response.EBackupUnitType.LocalUnit);

                                if (fledinvehicle == 1)
                                {

                                    Game.DisplaySubtitle("Other Unit: He's taking off, pursuing on foot!", 5000);
                                }
                                else
                                {
                                    Game.DisplaySubtitle("Other Unit: He's taking off, I'm in pursuit!", 5000);
                                }
                                
                            }
                          

                        }

                        else //if he doesnt flee
                        {


                            cop1 = new Ped("S_M_Y_Sheriff_01", myPed.Position, 1f);
                            copcar1 = new Vehicle("SHERIFF", myPed.Position, 15f);
                            copcar1.IsSirenOn = true;
                            copcar1.IsSirenSilent = true;



                            if (fledinvehicle == 2)


                            {
                                Game.DisplaySubtitle("Other Unit: I'm out with the subjects vehicle.", 5000);
                                myPed.Tasks.ParkVehicle(myVehicle, myVehicle.Position, myVehicle.Heading);
                                copcar1.Position = myVehicle.GetOffsetPosition(Vector3.RelativeBack * 5f);

                            }
                            else //on foot

                            {
                                Game.DisplaySubtitle("Other Unit: I'm out with the subject.", 5000);
                                myPed.Tasks.StandStill(-1);


                            }





                        }

                  

                        








                    }

                        //end cophelps
                    });
                }






            if ((caller.IsAlive) && Game.LocalPlayer.Character.Position.DistanceTo(caller.Position) <= 10.0F)
            {
                Game.DisplayHelp("Press T to get a statement from the caller");

                if (Game.IsKeyDown(Keys.T))
                {

                    int charges = myRand.Next(1, 10);

                    if (charges == 4)
                    {
                        Game.DisplaySubtitle("I would like to avoid pressing charges, if possible.", 5000);
                    }
                    else
                    {
                        Game.DisplaySubtitle("I would like to press charges", 5000);
                    }
                    

              
                }
            }


            if ((myPed.IsAlive) &&  (foundPerp == false)  && (otherCopFound == false)  && Game.LocalPlayer.Character.Position.DistanceTo(myPed.Position) <= 20.0F)
            {

                myBlip = myPed.AttachBlip();
                myBlip.Order = 100;
                if (fledinvehicle == 2)
                {

                    Game.DisplaySubtitle("Baker 4, I've located a vehicle matching the description, initiating stop", 5000);
                }
                else
                {

                    Game.DisplaySubtitle("Baker 4, I'm out with the subject now.", 5000);
                }
                
                GameFiber.Sleep(500);
                int r = myRand.Next(1, 10);
                GameFiber.Sleep(200);
                int willattack = myRand.Next(1, 10);

                if (r < 3)
                {

            
                    if (willattack < 4)
                    {

                        NativeFunction.CallByName<uint>("TASK_COMBAT_PED", myPed, Game.LocalPlayer.Character, 0, 1);

                    }
                    else {

                            if (myPed.IsInAnyVehicle(true))
                            {
                                myPed.Tasks.CruiseWithVehicle(40, VehicleDrivingFlags.Normal);
                            }

                            else
                            {
                                if (myPed.IsFleeing == false)
                                {
                                    Functions.AddPedToPursuit(this.pursuit, this.myPed);
                                }
                            }


                        }




                    }
                else
                {

                    myPed.Tasks.PutHandsUp(-1, Player);

                }

                foundPerp = true;

                // GameFiber.Sleep(30000); //sleeps this loop without ending it for 30 seconds, allows ped to potentially change his mind at any moment on how he will behave
            }


                GameFiber.StartNew(delegate
                {
                    if (Game.IsKeyDown(Keys.Delete))
            {


                Game.DisplaySubtitle("Ending Callout", 5000);
                if (myBlip.Exists()) myBlip.Delete();
                if (myPed.Exists()) myPed.Delete();
                if (callerblip.Exists()) callerblip.Delete();
                if (caller.Exists()) caller.Delete();
                if (copcar1.Exists()) copcar1.Delete();
                if (cop1.Exists()) cop1.Delete();
                this.End();
            }


                });


                if (Game.IsKeyDown(Keys.Y))
            {

               
             if (fledinvehicle == 2 )

                {
                    Game.DisplaySubtitle("Dispatch: Subject was described as a " + radiodesc + " and fled in a " + spawnedveh, 5000);
                }
             else
                {
                    Game.DisplaySubtitle("Dispatch: Subject was described as a " + radiodesc, 5000);
                }
                


            }


            });

            //A simple check, if our pursuit has ended we end the callout
            //if (!Functions.IsPursuitStillRunning(pursuit))
            //{
            //    this.End();
            //}
        }

 

Edited by download500

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  

×