UGX-Mods

Call of Duty: Black Ops 3 => Tutorial Desk => Scripting => Topic started by: qwerty195 on September 17, 2018, 08:42:55 pm

Title: Abnormal202 Scripting Tutorial 4: If Statements
Post by: qwerty195 on September 17, 2018, 08:42:55 pm
ABNORMAL202 SCRIPTING TUTORIAL 4: IF STATEMENTS
See ALL Tutorials (https://www.ugx-mods.com/forum/scripting/91/abnormal202-scripting-tutorials-master/16746/)

DESCRIPTION

If Statements after very important in coding. They let you check if a certain condition is met, and then do something. Or if that something is not met, do another something. OR if that something is not met but this something is do this something....

If Statements are pretty easy to get the hang of, and are very powerful. So let's hop in:

CREATING AN IF STATEMENT

In order to create an if statement, you first have to have something that you will be checking for if it's true or not. Remember earlier when I talked about booleans? basically we'll need some sort of variable that can store true or false (or 1 or 0).

so let's set a simple variable to a boolean:
im_a_good_student = true;
and make an if statement checking if that is indeed true:
if(im_a_good_student)
{
   IPrintLnBold("Good for you. What, you want a cookie or something?");
}
notice:
  • the word if
  • the ()
  • where you put what you're checking is true (inside the ())
  • there is no ; 
  • the {}
so if the variable im_a_good_student is in fact equal to true, the code inside the {} of the if statement will be run, and this case print that string to the screen. If it is not true, the if statement will simply be skipped over.
It's important to note you don't always have to directly set and check a variable to be true. you can actually call functions inside an if statement, provided they will return a boolean.
lot of different Engine functions (and I mean a lot) return booleans. for example pretty much anything function that starts with the word "is" returns a boolean, such as: IsMeleeing(), IsPlayer(), IsWeapon(), etc.
so instead of doing it the long way like this:
weapon = GetWeapon("smg_fastfire");

this_is_a_weapon = IsWeapon(weapon);
if(this_is_a_weapon)
{
   IPrintLnBold("that's a weapon alright");
}
we can take the shortcut:
weapon = GetWeapon("smg_fastfire");

if(IsWeapon(weapon))
{
   IPrintLnBold("that's a weapon alright");
}

ELSE

Sometimes if a certain condition in our if statement is not met, we don't just want to skip over the code, but do something else entirely...
lucky for you that can be accomplished pretty easily:
if(im_a_good_student)
{
   IPrintLnBold("Good for you. What, you want a cookie or something?");
}
else
{
   IPrintLnBold("I expected better from you");
}
notice:
  • the word else
  • there is no condition I'm checking for is true or not inside the else statement. This is because the code in the else statement is only activated it the if statement above is not true
  • the {}
  • you must have an if statement before you have an else.

ELSE IF

So far these all make pretty logical sense if we look at them English-wise. if something, do something. else do this other thing. now let's keep that going with the hybrid baby of if and else, the else if().
just like the else, there must be an if statement before you can have an else if. the else if is similar to the if statement, as it checks if a condition is met, and will be skipped over if it isn't. 
if(im_a_good_student)
{
   IPrintLnBold("Good for you. What, you want a cookie or something?");
}
else if(math_is_hard)
{
   IPrintLnBold("okay I'll give you that. But that's no excuse");
}
else
{
   IPrintLnBold("I expected better from you");
}
in this case first it will check if im_a_good_student is true. if it is, it will execute the code inside the first if statement, and nothing else.
if im_a_good_student is not true, it will then move to the else if statement and check if math_is_hard is true. If it is, then it will execute the code inside the else if statement, and nothing else.
finally if neither the if statment or any else if's are met, than the else code will activate.
note that you can have as many else if's in a chain as you want, just remember it needs to start with an if statement:
if(im_a_good_student)
{
   IPrintLnBold("Good for you. What, you want a cookie or something?");
}
else if(math_is_hard)
{
   IPrintLnBold("okay I'll give you that. But that's no excuse");
}
else if(life_is_hard)
{
   IPrintLnBold("let me play the world's tiniest violin for you");
}
else if(im_a_victim_of_circumstance)
{
   IPrintLnBold("uh huh...");
}
else
{
   IPrintLnBold("I expected better from you");
}

AND'S/OR'S

We can also check for more than one condition to be met inside an if statement before the if code is executed.
in GSC language:
and = &&
or = ||
note that for or those are not lowercase L's or capital i's, but the vertical line which you can make by holding shift and pressing the "\" button on your keyboard.
so as you might expect, having an and means all conditions must be met, and having an or means just one of the conditions has to be met:
if(the_month_is_may && the_day_is_the_twenty_ninth)
{
   IPrintLnBold("HAPPY BIRTHDAY!!!!");
}
if(the_month_is_may || the_day_is_the_twenty_ninth)
{
   IPrintLnBold("what? I'm not celebrating your birthday. it's 3 months away...");
}
 in the above case, the first code with the and in the if statement will only get activated once a year.
for the bottom case with the or in it, it would get run on the 29th of every month, and on every day in the month of may, because just one of those conditions has to be met.
it should be noted you can chain more than one and or or in the same if statement:
if(you_brought_the_cake && I_brought_the_balloons && she_brought_the_drinks)
{
   IPrintLnBold("what a fun party...");
}
if(you_provide_gas || you_provide_grass || you_provide_ass )
{
   IPrintLnBold("come on in");
}
See ALL Tutorials (https://www.ugx-mods.com/forum/scripting/91/abnormal202-scripting-tutorials-master/16746/)