Jump to content

using multiple if checks


Recommended Posts

ShadowCoderKing

c#

im not familiar with using multilpe IF CHECKS at the same time example if i dont want to use the list method of createing ped and want 3 IF CHECKS to check if the 3 peds are dead, do you just have the if checks one after another

       if (Entity.Exists(ENEMY1) && ENEMY1.IsDead)    if (Entity.Exists(ENEMY2) && ENEMY2.IsDead)    if (Entity.Exists(ENEMY3) && ENEMY3.IsDead)  {     ENEMY1.Delete();     ENEMY2.Delete();     ENEMY3.Delete(); }       

i just tested a script and it seems to be working correct

Edited by SpiderMight
Link to comment
https://gtaforums.com/topic/887068-using-multiple-if-checks/
Share on other sites

sollaholla

I think instead of posting a bunch of threads, just make a thread called "[C#] Need Assistance With Script". I don't want you to get banned, for not knowing the rules.

Anyways, let's continue...

i just tested a script and it seems to be working correct

Not exactly, because this is the logic as you wrote it:
If enemy 1 is dead, let's move on. Now if enemy 2 is dead let's move on again. Okay NOW if enemy three is dead, let's do some logic.

What you want (I assume) is:
If enemy 1 is dead, and if enemy 2 is dead, and if enemy 3 is dead, then let's do some logic.

And you would write this in code:

// this isn't how i would write it, but it should work for you// you can also use the '!' symbol in before the function, because it's the same as saying '== false'.if (IsAnyPedAlive(ENEMY1, ENEMY2, ENEMY3) == false) // no peds are alive.{    // delete all them peds boi    DeletePeds(ENEMY1, ENEMY2, ENEMY3);}---------------------------------------------------------------------------------------// below the function, write these functions.private bool IsAnyPedAlive(params Ped[] peds){    // let's check to make no peds are dead.    for(int i = 0; i < peds.Length; i++)    {        // well, we found a ped that's dead so let's return true.        if (peds[i].IsDead)            return true;    }    // if we've made it to this point, and no peds returned dead, then let's return false.    return false;}private void DeletePeds (params Ped[] peds){    for(int i = 0; i < peds.Length; i++)    {        peds[i]?.Delete();    }}
Edited by sollaholla
Link to comment
https://gtaforums.com/topic/887068-using-multiple-if-checks/#findComment-1069564429
Share on other sites

ShadowCoderKing

Thanks for all the help man

i have a problem again :(

In the OnTick part it just keeps replaying the last if checks near the bottom of the script instead of going to the OnKeyUp part to restart the mod once its finished, please help

   private void OnTick(object sender, EventArgs e)   {       if (Entity.Exists(Balla1) && Balla1.IsDead)      {         Balla1.Delete();             Balla1 = GTA.World.CreatePed(new Model(588969535), new GTA.Math.Vector3(68.3224f, -1899.852f, 21.59978f), -154.4317f);           // ped2     }       if (Entity.Exists(Balla1) && Balla1.IsDead) //WHEN THE ONTICK SCRIPT END THIS IS WHERE IT LOOPS BACK TO IN A INFANT LOOP INSTEAD OF CHECKING IF BUTTON PRESSED       {            Balla1.Delete();                   Balla1 = GTA.World.CreatePed(new Model(588969535), new GTA.Math.Vector3(69.46897f, -1907.409f, 21.64032f), -144.9987f);            // ped3         }            if (Entity.Exists(Balla1) && Balla1.IsDead)            {              Balla1.Delete();                  }     }   // how do i make it go to void OnKeyUp(object sender, KeyEventArgs e) from here ? // IT JUST KEEPS LOOPING TO WHERE I SAID ABOVE                   void OnKeyDown(object sender, KeyEventArgs e)    {    }    void OnKeyUp(object sender, KeyEventArgs e)    {        if (e.KeyCode == Keys.U)            if (!Entity.Exists(Balla1))        {                      Balla1 = GTA.World.CreatePed(new Model(361513884), new GTA.Math.Vector3(100.33f, -1920.99f, 20.74796f), 147.9984f);        }    }}
Edited by SpiderMight
Link to comment
https://gtaforums.com/topic/887068-using-multiple-if-checks/#findComment-1069564690
Share on other sites

sollaholla

I have absolutely no idea what you mean, but I do see an issue in your code.
You're checking the same thing 3 times, so every single time, Balla1 is dead, you create new one and move the ped in the same memory slot as the last one. It's very strange design.
I honestly don't know what your script needs to do, and I have no clue how to help because nothing in the script makes sense :p (Not being rude here, don't take it the wrong way. I know you're just trying to learn)

Link to comment
https://gtaforums.com/topic/887068-using-multiple-if-checks/#findComment-1069564747
Share on other sites

ShadowCoderKing

if the button U is pressed then it spawns a ped and if the ped is dead then it spawns a new ped2 and if that new ped2 is dead it spawns ped3 and if that ped3 is dead it should go back to check if the button is pressed to start the process over again but instead it go's back checking if ped3 is dead and then it spawns ped3, and the ped3 part keeps happening in a loop

 

i really dont know why its doing that i cant complete my mod because this is happening please help if you can :(

Edited by SpiderMight
Link to comment
https://gtaforums.com/topic/887068-using-multiple-if-checks/#findComment-1069565468
Share on other sites

sollaholla

if the button U is pressed then it spawns a ped and if the ped is dead then it spawns a new ped2 and if that new ped2 is dead it spawns ped3 and if that ped3 is dead it should go back to check if the button is pressed to start the process over again but instead it go's back checking if ped3 is dead and then it spawns ped3, and the ped3 part keeps happening in a loop

Then there's absolutely no need to have ped2 and ped3.

You should read more up on variables and what exactly they do, it's very useful to utilize them in a way that saves memory.

I'd recommend watching some

, because what you're trying to do may not seem complex to most of us, but for people that aren't familiar with variable assignments and if statements, it's probably a good idea.

 

Here's some psuedocode that should get you through it:

using GTA;using GTA.Math;using GTA.Native;using System.Windows.Forms;// Replace the namespace and class names. This is just an example.namespace MyNamespace{    public class MyClass : Script    {        // This is going to be the ped we will spawn when we press the U key.        private Ped _myPed;                // This is the kill count that we currently have (or), the amount        // of peds that have died.        private int _killCount = 0;                // You can change this value to specify the amount of kills        // that you want to be considered "max" so it will end the "spawn loop".        private const int MaxKills = 3;                // Here's our constructor where we initialize variables and/or subscribe to events.        public MyClass()        {            // We're going to subscribe to both the Tick and KeyUp events.            Tick += OnTick;            KeyUp += OnKeyUp;        }                // Here's where we put logic that needs to run every frame. (or every millisecond)        private void OnTick(object sender, EventArgs e)        {                        if (Entity.Exists(_myPed) && _myPed.IsDead)            {                // Delete the ped, since it's no longer needed.                _myPed.Delete();                                // Let's up the kill count.                _killCount++;                                // This will only run if we've passed the max kills.                if (_killCount >= MaxKills)                {                    // You might be confused at this point.                    // "Well even if we delete the ped how do we spawn him again?"                    // If you remember in our OnKeyUp function we told the compiler                    // to ONLY spawn the ped when it doesn't exist, and killcount is 0.                    // So now that we've deleted the ped, it no longer exists in the game,                    // and we've set our killcount back to 0.                    // Therefore if we hit the U key again, it will spawn another ped,                     // since both functions (OnKeyUp & OnTick) run independently of                     // eachother.                    _killCount = 0;                                        // Let's notify the player he's killed all the peds.                    // More info on coloring text: https://pastebin.com/5Ez35NwX                    UI.Notify ("You've killed all ~r~" + (MaxKills + 1) + "~s~ enemies.");                    return;                }                                // If we've made it this far, that means we haven't yet killed enough peds.                // We're using our "golden rule" function to spawn the ped again.                CreatePed();            }        }                // Here's where we put logic that requires a keypress to activate.        private void OnKeyUp(object sender, KeyEventArgs e)        {            // Make sure we pressed the U key, and that myPed doesn't            // already exist in the game.            // We're also going to have to check the killcount to make sure we haven't            // killed any peds yet..            if (e.KeyCode == Keys.U && !Entity.Exists(_myPed) && _killCount <= 0)            {                // Again using our "golden rule function".                CreatePed();            }        }                // THIS is the golden rule of programming. NEVER EVER re-use code. What we're doing        // here is exactly what you need to be doing. If we make this a function, then        // we never have to write the same line of code ever again, since now we can just         // call this function.        private void CreatePed ()        {            Vector3 spawn = Game.Player.Character.Position + Game.Player.Character.ForwardVector * 15;            _myPed = World.CreatePed(PedHash.Ballasog, spawn);            _myPed.Weapons.Give(WeaponHash.AdvancedRifle, 150, true, true);            _myPed.Armor = 150;            _myPed.Task.FightAgainst (Game.Player.Character);        }    }}

 

Again, I recommend, if you can't understand this code, to watch the tutorial above.

It should really help you become more advance in programming.

Edited by sollaholla
Link to comment
https://gtaforums.com/topic/887068-using-multiple-if-checks/#findComment-1069566688
Share on other sites

Hi, I'm new here. I'll be auditing your code. :monocle: I know you're teaching programming here, so I want to correct anything that may be confusing.

private bool IsAnyPedAlive(params Ped[] peds){    // let's check to make no peds are dead.    for(int i = 0; i < peds.Length; i++)    {        // well, we found a ped that's dead so let's return true.        if (peds[i].IsDead)            return true;    }    // if we've made it to this point, and no peds returned dead, then let's return false.    return false;}

This function seems to be backwards. It returns true if one or more peds are dead, and returns false if no peds are dead. Therefore, it should really be called IsAnyPedDead(). If you want it to be IsAnyPedAlive(), then change .IsDead to .IsAlive.

UI.Notify ("You've killed all ~r~" + (MaxKills + 1) + "~s~ enemies.");return;

Cosmetic bug: Since MaxKills is 3, this would print "You've killed all 4 enemies." There's no need to add 1 here.

// THIS is the golden rule of programming. NEVER EVER re-use code. What we're doing// here is exactly what you need to be doing. If we make this a function, then// we never have to write the same line of code ever again, since now we can just // call this function.

Never re-use code? I know what you're saying here, especially since you explained it. However, "code reuse" usually means the opposite of what you said. Calling a function in multiple places (instead of writing the same code over and over) is an example of reusing code. Instead, you might want to say "Never duplicate code."

Link to comment
https://gtaforums.com/topic/887068-using-multiple-if-checks/#findComment-1069578027
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
  • 0 User Currently Viewing
    0 members, 0 Anonymous, 0 Guests

×
×
  • Create New...

Important Information

By using GTAForums.com, you agree to our Terms of Use and Privacy Policy.