BOOLEANS, VARIABLES, CONSTANTS, ARRAYS, AND CONDITIONAL BRANCHES

The basic programming concepts of Booleans, Variables, Constants, Arrays, and Conditional Branches

Once you have a basic grasp on what exactly Booleans, variables, and conditional operations are exactly, you can use these concepts in anything involving computer programming. These concepts are portable to any programming language, engine, dwarf fortress, what have you, as essential basics for controlling the flow of a program, essentially what a program does.

Booleans

A Boolean is a single value, equivalent to or containing “true” or “false“. The value can either be stored in the memory, for example, in the RPGMaker series Booleans are called switches, of which the game maker has a large amount of at their disposal that are always in the memory and saved to the players’ save files to be used at any time, and can be changed to currently be equal to true or false at any time, or it can be a temporary Boolean value that has been returned by a function, which we will learn more of after we get to Conditional Branches.

Variables

Variables are combinations of symbols (names) that represent a stored value. They can hold different kinds of data, the most common types being integers, or whole numbers, floating-point numbers, or decimals, and strings, or a variable number of characters (text). Depending on the programming language or engine there may also be more complex data types, or the ability for programmers to define their own data types. Booleans are essentially just another datatype that is stored inside of a variable, but for the purpose of this article are explained separately.
Depending on a variable’s data type and the programming language or engine, the way you write a value to a variable and what functions can be performed on and with a variable varies. Writing data to the variable is usually as simple as declaring the variable (how one does so is dependent on the aforementioned things), then setting it as being the value you want it to have. For example, some common ways of doing so include:

Variable1 = 154;
Number = 129
SET asdf=33
Astring = “String3S”

Notice that a string has to have its values enclosed by quotation marks. This is almost universal among programming languages as otherwise there would be no way to tell whether String3S is a string, or the name of a variable, and most commonly quotation marks are used to show it is a string. To store the character that a string has to be enclosed with inside of a string, commonly it is appended with a backslash, for example:

“This quotation mark \” is properly stored inside of this string instead of making my compiler spit out errors.”

though the character it is appended with can vary. To store the character that you append the enclosing character with inside of a string, in some languages you have to append the character with itself, for example:

“This backslash \\ is properly stored inside of this string instead of making my compiler spit out errors.”

Some programming languages and engines require the type of data a variable holds to be chosen ahead of time, and from then on that variable is restricted to that type of data.
For example in:

String messagebox;
messagebox = “This is always going to contain a string.”

the variable messagebox is declared as being a string , and will always be a string. How the datatype is declared for each variable can vary.
Others are less strict and don’t require the data type to be declared ahead of time, and the type of data is simply implied from whatever value the variable is currently set too.
So for example:

aninteger = 2
Afloat = 1.9

aninteger would be implied to be an integer, since its value is set to a whole number, and Afloat would be implied as being a floating-point number.
Once a variable has been set to contain something, you can then use it in mathematical operations and what not. How this is done can vary, but a general example would be

variable1 = 3;
variable2 =2 ;
variable1 = (variable1+2)*2;
finaloutput = variable1*variable2;

in which case you end up with variable1 having the value 10, and finaloutput having the value 20. In some programming languages you’ll have to do a few extra things when mixing integers and floating-point numbers, mainly when it comes to the datatype the variable that holds the output of the equation is, but others will handle it for you. Obviously you can’t perform mathematical operations on strings, so they usually have their own specific programming language dependent functions for doing different things like joining strings together and removing a certain word and such, but this article won’t get into them
.
Constants

Whereas variables can be changed whenever you want to, constants are either defined by the engine, or (usually at runtime), by the programmer, and then after they are set to a value cannot be changed. For example, one would set the constant “totalnumberoflevels” to 20 at the beginning of a game which always has 20 levels.
After it is set one would use it for

progress_percentage = current_level/totalnumberoflevels;

to find how close the user is to the end of the game for example. Though you can use it in expressions, since it is a constant you can not change its value, so

totalnumberoflevels = totalnumberoflevel + 10;

would not change its value to 30.

Arrays

An array is an advanced datatype that is a collection of indexed values, and can usually have a variable size while the program is running. They usually have the form

variable_name[index]

Often the array index is an integer, thus

anarray[0]

and

anarray[1]

would each have a separate value.
If you're wondering how making storing and using data more confusing is useful, arrays allow you to access a variable based on another variables value. For example, in

whattofind = 1;
anarray[0] = 2;
anarray[1] = 3;

output = anarray[whattofind];

output would equal 3 since the equation is equivalent to

output = anarray[1];

Additionally, in many cases arrays can have more then one index, for example an array with two indexes
anaarraywithtwoindexes[0][0]
would be called a two dimensional array, and
anaarraywithtwoindexes[0][0]; anaarraywithtwoindexes[0][1]; anaarraywithtwoindexes[1][0]; anaarraywithtwoindexes[1][1]; 
would each have a separate value.
How exactly arrays are handled and what functions you have to interact them depend on the programming language, and they’ll seem more useful once you’re a little more advanced and you start learning how they can make your life a lot easier by dramatically lowering how much code you have to write and allowing for a variable amount of data to be stored, for uses such as storing your map data in a way easily allowing you to have variable map sizes. The application of arrays is beyond the scope of this article though, as effective use involves more advanced control statements, so I’m going to leave it just at a basic explanation on how you read and write from them for now.

Conditional Branches

Now for conditional branches, where you actually put all of these data types to use for controlling the flow of your program. Your basic conditional branches can be broken into four different parts, if, then, else, then. Basically, it is found whether a given equation is true or false, and then if it is true the program then goes on to execute some code, else if it is false, it goes on to execute a different set of code. After the if/then/else/then statement, the flow of the program will continue on in normal fashion. How exactly conditional statements look can vary widely based on the language. For example:

IF(1=1){
showmessage(“One is indeed equal to one!”);
}
else
{
showmessage(“One is not equal to one!”);
wait(29);
showmessage(“Wait a minute, does that make sense?!”);
}

showmessage(“This is executed whether one is equal to one or not.”);

the “{}s” contain the ‘then’ code

IF 1=1
showmessage(“One is indeed equal to one!”);
else
showmessage(“One is not equal to one!”);
wait(29);
showmessage(“Wait a minute, does that make sense?!”);
showmessage(“This is executed whether one is equal to one or not.”);

the tabs define what code is inside the ‘then’ statement, all of the code with an indention greater then the if/else is inside these statements.
The way that conditional branches function is always the same regardless of the structure the conditional branches take in the code though.
Now, for a more practical example using variables.

randomchance = random(0,100);
randomchance += herodexteritybonus;

IF (randomchance > 80){
criticalhit=true;
totaldamage=totaldamage*cricticalhitbonus;
}
ELSE{
criticalhit=false;
}

In the above example, the random chance variable is set to a random number between 0 and 100 (let‘s say that that‘s how our random() function works for the purpose of this example), and then has the herodexteritybonus added too it. After that, if the random chance variable is greater then 80, the criticalhit variable is set to true and the totaldamage variable is increased. If it is not greater then 80 the criticalhit variable is set to false, and the totaldamage variable is not increased.
You can use any conditional operator in the if equation, from = to >= and so on. Appending the operator with “not” or “!” depending on the syntax of the programming language or engine (i.e. randomchance not =, NOT randomchance= or randomchance !=) will change it to where the if then code is executed if the equation is not equal and so forth..
You can nest additional conditional statements inside of other conditional statements, for example:

randomchance = random(0,100);
randomchance += herodexteritybonus;
Herohassupercriticalhitbonu = true;

IF (randomchance > 80){
criticalhit=true;
totaldamage=totaldamage*cricticalhitbonus;
IF(herohassupercriticalhitbonus){
totaldamage=totaldamage*2;
}
}
ELSE{
criticalhit=false;
}

will double the totaldamage variable if the herohassupercriticalhitbonus is true and randomchance is greater then 80. Note that there appears to be no equation being checked at all there. That is because herohassupercriticalhitbonus is a Boolean, so if its value is true the if then statement code will be executed, no additional operators required, which is quite common but like everything not universal. You might also note that you don’t actually have to follow an IF conditional with an ELSE conditional.
If your going to have another IF statement inside of an ELSE statement, you can sometimes combine the two into an ELSE IF statement, that will only execute the else then code if its if equation is true.
For example, instead of:

randomchance = random(0,100);
randomchance += herodexteritybonus;
Herohassupercriticalhitbonu = true;



IF (randomchance > 80){
criticalhit=true;
totaldamage=totaldamage*cricticalhitbonus;
IF(herohassupercriticalhitbonus){
totaldamage=totaldamage*2;
}
ELSE{
IF(herohascriticalhitpenalty){
totaldamage=totaldamage*0.9;
}
}
}
ELSE{
criticalhit=false;
}

this will perform the exact same thing:

randomchance = random(0,100);
randomchance += herodexteritybonus;
Herohassupercriticalhitbonu = true;



IF (randomchance > 80){
criticalhit=true;
totaldamage=totaldamage*cricticalhitbonus;
IF(herohassupercriticalhitbonus){
totaldamage=totaldamage*2;
}
ELSE IF(herohascriticalhitpenalty){
totaldamage=totaldamage*0.9;
}
}
ELSE{
criticalhit=false;
}

Often, you will want to have more complex IF and IF ELSE statements without cluttering your code with more conditional branches. For that, you can use AND (also &&) and OR (||). You can combine AND NOT for outputting true if one expression is true and one expression is false, etc.
So, for example:

randomchance = random(0,100);
randomchance += herodexteritybonus;
Herohassupercriticalhitbonu = true;



IF (randomchance > 80){
criticalhit=true;
totaldamage=totaldamage*cricticalhitbonus;
IF(herohassupercriticalhitbonus){
totaldamage=totaldamage*2;
}
ELSE IF(herohascriticalhitpenalty){
totaldamage=totaldamage*0.9;
}
}
ELSE{
IF(herohassecondchance AND (random(0,100) <= 10){
totaldamage=totaldamage*cricticalhitbonus;
}
ELSE{
criticalhit=false;
}
}

will still increase totaldamage if herohassecondchance is true and a randomly generated number is less than or equal to 10. You should note that in most programming languages, there is a way to use functions, like the random function above for example, inside a conditional. This way, you can avoid having to have a separate variable being set to the output of the random() function head of time, thus being more efficient. You can also use it for comparisons as other datatypes, such as strings. Let's say that there is a function built into a programming language, "match_string();". It outputs true if part of a string contains another string.
So, for example:

stringtocheck = "Buy cheap viagra get free aids at Cheap Cocaine free stuff at wwwpharmacy.com ";

IF(match_string(stringtocheck,"free stuff")){
its_a_bot_get_it();
}
else{
no_match();
}

In some programming languages, there are built in functions that will output a Boolean value to report on there status, etc, when they are used in a conditional statement, but function differently outside of the conditional statement. For example, let’s say that the programming language we’re using has a function, “load_file();”, that when used outside of a conditional statement loads a file. However, when it is used inside of a conditional statement, it will give a value based on whether the file has already been completely loaded or not.

IF(load_file(“cookies.txt”)){
Message = “has already been loaded”;
}
ELSE{
load_file(“cookies.txt”);
}

Something to keep in mind in case you run into such a thing later down the road.

_______________________________________________________________________________________________________________________________________________________

That concludes this overview of the basic concepts of Booleans, variables, arrays, and how to use them in conditional branches. Hopefully now you can apply this knowledge by figuring out the specific syntax for using these concepts in the programming language or engine your trying to use now, and every one you use in the future, thus starting yourself on your long journey to code your own side view battle system instead of just plugging in a side-view battle system script into your game just because you think that front-view battle “are lame”.

For your information front-view battles are not “lame”, they are the original and best way of viewing things. Why do you think you see from a forward looking position instead of in third-person?

Posts

Pages: 1
c++ *nostalgia*;

edit: I only had time to skim over it, and it seems sound to me, but I'll make a proper comment later.
I know that but it was so spot on with my lessons today so I just had to say it XD
cout << "Not c++ again!"; //dies
[ORG 0x100]
mov ah, 9
mov dx, msg
int 0x21 ; MS-DOS interrupt

mov ax, 0x4C00
int 0x21

msg db 'C++? I can't keep up with all these newfangled higher programming languages kids use today. Thats about three years after my prime.$' ;
Pages: 1