Jump to content

Two conditions for one callout?


Recommended Posts

The scenario seemed pretty simple to me: you respond to suspicious activity in a certain location. As you come close, you get an update about terrorist activity and you’re being given a task to secure the area. If you get them all killed you complete the task.

 

I came up with multiple solutions that seemed logical to me but obviously the code has very little with (my?) logic and I can't make it without your help.

The callout has been accepted. „myBlip” takes you to the location. Then…

        public override void Process()
        {
            base.Process();
            {

                if (Game.LocalPlayer.Character.DistanceTo(Soldier1.Position) < 200f)
                {
                    CallTheCops = true;
                    Game.DisplaySubtitle("~r~SECURE THE AREA~w~", 7500);
                    myBlip.DisableRoute();
                    myBlip.Delete();

                    myBlip2 = new Blip(Guard1.Position, 100f);
                    myBlip2.Color = Color.Red;
                    myBlip2.Alpha = 0.5f;
                }

                GameFiber.StartNew(delegate
                {

                    while (true)
                    {
                        GameFiber.Yield();
                        if (CallTheCops == true && Guard1.IsDead && Guard2.IsDead && Guard3.IsDead && Soldier1.IsDead && Soldier2.IsDead)
                        {
                            Game.DisplaySubtitle("~g~AREA SECURED~w~", 7500);
                            myBlip2.Delete();
                            if (Guard1.Exists()) Guard1.Dismiss();
                            if (Guard2.Exists()) Guard2.Dismiss();
                            if (Soldier1.Exists()) Soldier1.Dismiss();
                            if (Soldier2.Exists()) Soldier2.Dismiss();
                            if (Sniper.Exists()) Sniper.Dismiss();
                        }
                    }


                });

      //end of the code
        }

    }

The first condition works fine. The other one's outcome is being looped and crashes the games no matter whether it's meet or not.

 

        public override void Process()
        {
            base.Process();
            {

                if (Game.LocalPlayer.Character.DistanceTo(Soldier1.Position) < 200f)
                {
                    CallTheCops = true;
                    Game.DisplaySubtitle("~r~SECURE THE AREA~w~", 7500);
                    myBlip.DisableRoute();
                    myBlip.Delete();

                    myBlip2 = new Blip(Guard1.Position, 100f);
                    myBlip2.Color = Color.Red;
                    myBlip2.Alpha = 0.5f;
                }



                 if (CallTheCops == true && Guard1.IsDead && Guard2.IsDead && Guard3.IsDead && Soldier1.IsDead && Soldier2.IsDead)
                 {
                     Game.DisplaySubtitle("~g~AREA SECURED~w~", 7500);
                     myBlip2.Delete();
                     if (Guard1.Exists()) Guard1.Dismiss();
                     if (Guard2.Exists()) Guard2.Dismiss();
                     if (Soldier1.Exists()) Soldier1.Dismiss();
                     if (Soldier2.Exists()) Soldier2.Dismiss();
                     if (Sniper.Exists()) Sniper.Dismiss();
                  }

            }

        }
                                                                                    
       //end of the code
        }

    }

The first condition works fine. The other one is being totally ignored.

        public override void Process()
        {
            base.Process();
            {
                jestemblisko();
            }

        }



        public override void End()
        {
            myBlip2.Delete();
            if (Guard1.Exists()) Guard1.Dismiss();
            if (Guard2.Exists()) Guard2.Dismiss();
            if (Soldier1.Exists()) Soldier1.Dismiss();
            if (Soldier2.Exists()) Soldier2.Dismiss();
            if (Sniper.Exists()) Sniper.Dismiss();

                base.End();
        }




        public void jestemblisko()
        {

            GameFiber.StartNew(delegate
            {


                while (true)
                {
                    GameFiber.Yield();
                    if (Game.LocalPlayer.Character.DistanceTo(Soldier1.Position) < 200f)
                    {
                        break;
                    }

                    CallTheCops = true;
                    Game.DisplaySubtitle("~r~SECURE THE AREA~w~", 7500);
                    myBlip.DisableRoute();
                    myBlip.Delete();

                    myBlip2 = new Blip(Guard1.Position, 100f);
                    myBlip2.Color = Color.Red;
                    myBlip2.Alpha = 0.5f;

                }



               while (true)
               {
                    GameFiber.Yield();
                    if (CallTheCops == true && Guard1.IsDead && Guard2.IsDead && Guard3.IsDead && Soldier1.IsDead && Soldier2.IsDead)
                    {
                        break;
                    }

                    Game.DisplaySubtitle("~g~AREA SECURED~w~", 7500);
                    this.End();

                }


            });
        }
        }

    }

Everything goes to hell.

 

However, once it's just one condition everything works perfect but I do have a little problem with that, since I've already created a plugin outside of a "callout sheet" where there are three different conditions doing their job just fine...  Could you tell me (again) what do I do wrong and how can I keep going with all of this without having a stroke or a massive heart attack? 🙂

 

 

EDIT 1: I've tried to remove {} after the base.Process(); but still nothing.

Edited by PawelSad12
Link to comment
Share on other sites

Pawel,
one of the most troublesome and important aspect of scripting for GTA/LCPD:FR is to create chunks of code that will work in sequence. There were many techniques: switch...case + enum, bool flags, while loops but I managed to create probably the best solution which is to use a list of functions that might be switched on and off. Here are some resources:
https://github.com/LtFlash/LtFlash.Common/blob/master/LtFlash.Common/Processes/ProcessHost.cs

https://github.com/LtFlash/LtFlash.Common/blob/master/Examples/Processes/ProcessHost.cs

In your code it could be used like this:

 

public override void OnCalloutBegin()
{
  proc.ActivateProcess(IsPlayerClose);
}

public override void Process() 
{
  proc.Process()
}

public void IsPlayerClose()
{
  if(DistanceToPlayer < 200f)
  {
    //createblips
    proc.SwapStages(IsPlayerClose, HasPlayerClearTheArea);
    proc.ActivateStage(DisplayPlayerTaskOnScreen);
  }
}

private void DisplayPlayerTaskOnScreen()
{
  //print msg "Eliminate enemies
}

private void HasPlayerClearTheArea()
{
  if(AreEnemiesDead())
  {
    //print msg
    End();
  }
}

private bool AreEnemiesDead()
{
  return (!Enemy1.Valid() || Enemy1.IsDead) && (!Enemy2.Valid() || Enemy2.IsDead);
}

 

Here's some information on Action delegate behind that technique:

https://www.dotnetperls.com/action

Using that, I believe you can solve your problem quickly.

Edited by LtFlash
Link to comment
Share on other sites

  • Management Team

The way I would do it is make a List<Ped> of your guards. Then check in your Process() for any dead guards in the list and remove them, for example:

 

foreach(var guard in guards.ToArray())
{
     if(guard.IsDead)
     {
          guards.Remove(guard);
     }
}

You could then check for when your List is empty to end the process. Also, make sure you check your CallTheCops variable in the first if statement so you are not creating blips for eternity while the player is in range. Basically, your Process() would look like this:

 

if (!CallTheCops && Game.LocalPlayer.Character.DistanceTo(Soldier1.Position) < 200f)
{
  //dostuff
  CallTheCops = true; //this should be a class variable
}

foreach(var guard in guards.ToArray())
{
  if(guard.IsDead)
  {
    guards.Remove(guard); //guards should be a List<Ped> created whenever you create your guards
  }
}

if(guards.Count() == 0)
{
  //end
}

There are other ways to go about this that are cleaner if your callout becomes more complicated, but this should work for your case.

"Work and ideas get stolen, then you keep moving on doing your thing."

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...