Submit Your Article


 
RPG Maker

Welcome Guest ( Log In | Register )


  Games Resources RPG Maker VX RPG Maker XP Scripts Tutorials Downloads

 
Reply to this topicStart new topic
> [Scripting][Series]The Scripter’s Journey Series, 3. Controlling the Flow
The Law G14
post Jun 24 2012, 10:03 AM
Post #1


Scripter FTW
Group Icon

Group: Local Mod
Posts: 1,346
Type: Scripter
RM Skill: Skilled
Rev Points: 5




The Scripter’s Journey Series

3. Controlling the Flow


Table of Contents
I. The Importance of Program Flow!
II. Conditional Operators!
III. Logical Operators!
IV. Relational Operators!
V. All about Looping!
VI. Conclusion


The Importance of Program Flow!
Program flow is essential. What is program flow you ask? Well, program flow is basically the order or the sequence in which lines or blocks of code are executed. Hence, the title “Controlling the flow” means in this lesson we’ll be learning how to control the sequence of our programs. This is important because being able to control this program flow allows us to adjust to different user inputs or anything in the program that allows for different “routes” (I’ll call these things “situations” and I’ll explain them more throughout the tutorial). For example if the user buys a Wooden Sword at the local equipment shop we want a specific block of code to run when they buy it (like the sound effect that plays and the additional stats). In order to run that specific block of code, we would need to “control the flow” of the program and change the sequence so that we run the specific block of code that we want. Programming languages always give a lot options at your disposal in order to control this program flow and we’ll be going through everything you’ll need to know to effectively direct your program.


Conditional Operators!
So, the main idea of this tutorial is to be able to account for every possible “route” or situation so that you can direct your program in the right sequence or order to address this situation (Like the buying of the Wooden Sword example, this would be a “situation”). Situations can be thought of as Boolean Expressions. Remember last tutorial we talked about Booleans and how they can be equal to either true or false, Boolean Expressions are just the same except instead of just being a variable, it is an expression or “situation” that is evaluated to either true or false. (For example, 1 equals 1 is an expression that evaluates to true). Operators in general (at least the types of operators we’ll be discussing) allow us to “define” our situation (when I say situation I’m referring the Boolean Expression we just discussed). More specifically, Conditional Operators prefix this definition of the situation and establishes when the block or line of code will execute in relation to the situation. Here is a list of the conditional operators that you’ll be using frequently: “if”, “elsif”, “else”, “unless”, “while”, and “until”.

“If”, “elsif”, and “else” are all essentially the same idea. “If” is the mother of all conditional operators, it’s the conditional operator that is used undoubtedly used the most. Here is a basic example of the usage of “if” to help you understand it’s basic idea:

IF my dog runs down the street…(certain block of code will execute, in this case, you’ll run down the street like a lunatic to save your dog).

So as you can see, “if” basically says that the code will execute if the situation evaluates to “true”. Now “elsif” and “else” cannot exist by themselves, they need to be paired with “if”. So here is another basic example:

IF my dog runs down the street…I’ll run down the street like a lunatic to save my dog.
ELSIF (basically establishing another possible situation) my dog runs to my backyard…I’ll laugh and go use the bathroom.
ELSIF my dog watches television..I’ll watch television with him.
ELSE…I don’t care if he is doing anything else.

Through this example I hope you understand the jist of these guys, but let me further elaborate. Elsif is basically another “if” condition, it just needs to come after the original “if” condition to establish that you now after more “situations”. “Else” is basically the “if all else fails” condition and does not prefix a “situation”, else pretty much takes in every other infinitely possible situation and points it to one block of code or line of code. In the above example if the dog doesn’t do any of the possible situations, then you don’t care anymore. Now, time to give a more serious, but straightforward example to show how you would structure an actual if statement in your Script Editor (Also, don’t worry about the == sign, we’ll get to that later):

CODE
if 1 == 1
print “YEEA”
elsif 1 == 3
print “that makes no sense”
else
print “Ok”
end


You must always end an if statement with an “end” keyword, even if there wasn’t any “elsif” or “else” tags, a simpler example for you to understand would be:
CODE
if 1 == 1
print “Good.”
end


Now, let’s move on to the next Conditional Operator which will be “unless”. As I said before, Conditional Operators prefix the definition of the situation and establish when the block of code will execute in relation to the situation. In if statements (when I say “statements” that basically refers to the situation and the block or line of code that is associated with the Conditional Operator) the block of code occurs when the situation defined, evaluates to true. In unless statements, the line or block of code occurs if the situation evaluates to false.

UNLESS my dog runs down the street…(certain block of code will execute, in this case, I’ll be happy that he is safe in my house).

So if the dog doesn’t run down the street, the situation evaluates to “false” and the code can now execute. Here’s it in code form:

CODE
unless 1 == 1
print “Yea!” # Will never print this because 1 always equals 1 therefore it is not ‘false’
end


Next, we’ll discuss the “while” and “until” Conditional Operators. I won’t go too in depth about that them now because we’ll be discussing them more in “All about looping”. While is basically just like an if statement, if the situation evaluates to true, the block of code will execute. The key difference is that the code will continually execute, over and over again. Until is basically just like an unless statement, if the situation evaluates to false, the block of code will execute, except it will execute continually, over and over again. This is the basic premise of looping and like I said, we’ll go into that later.


Logical Operators!
Like I said earlier, operators help us “define” our situation (a.k.a Boolean Expression). Logical Operators allow us to specifically modify the “truth” or “false” aspect of a Boolean Expression’s evaluation. The logical operators that are used in RGSSX are “and”, “or”, and “not”.

IF my dog AND my neighbor’s dog run down the street…blahblahblah

As you can see “and” basically tells us that more than one situation need to evaluate to true for the code to execute. So basically, two situations need to be true in order for the block of code to execute, not one, but two.

UNLESS my dog OR my neighbor’s dog runs down the street…blahblahblah

Each situation is divided by the “and” or “or” Logical Operators to produce and overall situation. As we know before from unless statements, the block of code executes if the situation evaluates to false. “Or” basically says though that only ONE of the situations need to be true or relevant for anything to happen. So if “my dog runs down the street” or “my neighbor’s dog runs down the street” evaluate to true then the code won’t execute. But if both of them evaluate to false, than the code will execute. Let’s use an if statement with “or” so you can better understand it:

IF my dog OR my neighbor’s dog runs down the street…blahblahblah

Again, when you see “or” or “and” that means you have a new situation. When you see “or”, each situation is completely independent, and “and” each situation is tied together and rely on each other. In if statements the situation needs to evaluate to true for the code to execute. Since we’re using “or” though, only one situation needs to be true for “if” to work. So if “my dog runs down the street” evaluates to true, then bingo, the code runs. Now let’s use some RGSSX examples:

CODE
if 1 == 1 and 2 == 2
print “Nice.”
End

if 1 == 1 or 2==3
print “Nice.” # Only one needs to be true, and since 1 equals 1, then we’re in business.
End

unless 1==1 and 2== 2
print “Nice.” # since the situation evaluates to true, this will never execute.
end

unless 1 == 1 or 2 == 3
print “Nice.” # Since only one of the situations need to be true, this code won’t execute.
End


Now, since we have “and” and “or” understand, let’s cover “not”. Not basically means “the opposite of” a Boolean Expression or situation.

IF NOT 1 equals 1…blahblahblah

Don’t look at the “not”, just look at the Conditional Operator at the beginning, and the situation. It reads “if one equals one”. That evaluates to true, one always equals one. So since we have “not” in front of that, that switches the value from true to false. Since if statements only run when the situation evaluates to true, this statement won’t run. So “not” is used to reverse the evaluation. Here is code example:

CODE
if not 1 == 2
print “yea” # 1 == 2 normally evaluates to false, but since we have “not” in font, it’s reversed
end


Now since you have a grasp on all three Logical Operators, there is one last little cool tip I’ll discuss to wrap up this section. There is a shorthand way to write down these operators. You can use && to represent “and”, || to represent “or”, and ! to represent “not”.

CODE
if 1 == 1 && 2 == 2
print “yea”
end



Relational Operators
Relational Operators are the easiest type of Operators to grasp. Basically Relational Operators simply compare and contrast different elements in a situation, such as the == operator we’ve been using. The list of Relational Operators are as follows: >, >=, <, <=, !=, and ==

First let’s go over == which simply means “equals to”, do NOT confuse this with = which is the Assignment Operator and assigns a value to a variable. It’s easy understand, basically just use this if you’d like to determine if something is equal to something else.

CODE
donkey = 5
monkey = 4
if donkey == monkey
print “that’s weird”
end


Next, we have the != Operator which means “not equal to” or just the opposite as the last == Operator.

The > Operator means “greater than”, the < Operator means “less than”, the >= Operator means “greater than or equal to”, and the <= Operator means “less than or equal to”. You guys should already know this though from basic math courses. Here is a code example:

CODE
if (1 > 0 and 2 < 3 ) or (2 > 4)
print  “yessir”
end


Just a side note, the parenthesis allow us to group ideas together. So in this case the first parenthesis is all one idea while the second parenthesis is all one idea.


All About Looping!
As opposed to previous examples in which if something evaluates to true or false, the code will execute once, looping means the code will continue to run over and over again as long as the Boolean Expression is evaluating to the correct value (whether it be true or false). There are several topics to cover under looping so let’s begin.

First of all, let’s get back into while and until. While is basically, like I said before, and if statement in “loop” form. So if the situation evaluates to true, the code will execute continually as long as the Boolean Expression keeps evaluating to true, if it fails to do so, it will break out of the loop.

CODE
while 1 == 1
print “this will print forever!”
end


Until is basically an unless statement in “loop” form. If the situation evaluates to false, the code will execute continually as long as the Boolean Expression keeps evaluating to false.

CODE
unless 1 == 2
print “this will print forever!”
end


Ok, now that you have a grasp of “while” and “until”, let’s move on. Now were going to discuss ranges and “for” loops. Loops don’t always need to run forever unless the Boolean Expression evaluates to the opposite value, sometimes, if you provide a “range” or values to loop through, the loop will end after going through each value in the range. A range can be defined using periods.

CODE
1..5


This range has five values, from ‘1’ to ‘5’.

CODE
1…5


This range has 4 values, from ‘1’ to ‘4’. When you use three dots, the last number is omitted from the range. The way I think about it is that if you add something (the extra dot) you have to lose something (the last number which is 5). So now let’s apply this knowledge on ranges to what we call “for” loops.

CODE
for i in 1..5
print i
end


Results: 1, 2, 3, 4, 5
Let’s dissect this code. First of all “i” is a variable that is constructed within the for loop. You do not make this variable and put in the loop, you simply give it’s name after the “for” keyword and it is automatically made. I call this variable the “Range Variable”, because it is equal to each value in the range. The way this works is that we have our range (since we have two dots it extends from one to five), and the for loop goes through each number individually and assigns this value in the range to the Range Variable (which is i). We then print that variable (which is equal to one of the values in the range) each time the loop goes through until it finishes. Also as a sidenote, each time a loop “goes” through a value, that is called an “iteration”, this will be important when we discuss how to control our loops.

Next we’ll discuss the most basic loop, which is the “loop do” loop and is provided with no condition and runs forever unless broken out from within (while talk about this later when we discuss how to control our loops).

CODE
loop do
print “I’ll keep printing forever until this computer EXPLODES”
end


Now that we understand all the basic types of loops, let’s look to how to control our loops. Loops can be controlled through four key terms: “break”, “next”, “retry”, and “redo”.

Break applies to all loops. Basically, break is like an emergency escape pod, it allows you to exit a loop. For example let’s take our last “loop do” loop that runs forever, we don’t want that to happen, so we would apply a break.

CODE
one = 1
loop do
one += 1
print “I’ll keep printing forever! I think…”
if one == 6
break
end
end


First of all, the += sign is simply an Increment Sign that allows you to add on to a value. So in this case, after each iteration of the loop, the “one” variable increase by 1, and the loop will break out when one equals 6.

The next three keywords to discuss work best with “for” loops. Next allows us to skip the current iteration and go to the next iteration.

CODE
for i in 1..5
if i == 3
next
end
print i
end


This code would skip 3 so therefore 3 would never be printed. Redo causes us to redo the iteration and “play” it over again.

CODE
for i in 1..5
if i == 3
redo
end
print i
end


What would happen here is that 3 would continually be printed over and over again. Finally we have retry which is just like redo except it goes through the WHOLE loop over again.

Finally, I have one last note to add to this tutorial before I conclude. You may have found it annoying to put the whole:

CODE
if i == 3
retry
end


Since there is only one word or a couple words that come when the condition is met. So this can be solved through the use of Statement Modifiers:

CODE
retry if i == 3
break if 1 == 1
next if i != 2
redo if i > 1



Conclusion
So from this tutorial you’ve learned that it is very important to direct the flow of your program based on different situations (Boolean Expressions) that arise during the course of your program. Three types of Operators helps us do so: Conditional, Logical, and Relational. Finally, looping also allows to direct program flow and stay in a certain block of code for a certain of time as a long as a condition is met or if the use of Loop Controllers (break, next, redo, and retry) are implemented.

The order of how these tutorials will progress: http://www.rpgrevolution.com/forums/index....showtopic=54953

~Law


__________________________

To put in sig, copy this link:
CODE
[url="http://www.rpgrevolution.com/forums/index.php?showtopic=51540"][img]http://img40.imageshack.us/img40/6504/conceptthelawbanner.png[/img][/url]


Sig Stuff


"When you first come, no one knows you. When help them out, they all know you. When you leave, they all love you. When you come back, they've already forgotten you." -- copy into your sig if you think this quote speaks true!

If you are one of the very few teenagers that know what real rap is and don't blindly listen to the hate statements (rap is crap), then put this in your sig. I say this in the name of Common, Mos Def, Lupe Fiasco, 2Pac, Nas, Talib Kweli, Eminem, and many others. -Exiled One

My Project Thread: Gai's Hunters


Go to the top of the page
 
+Quote Post
   

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 23rd May 2013 - 12:37 PM
RPG RPG Revolution is an Privacy Policy and Legal
eXTReMe Tracker