Jack of Most Trades
- Feb 16, 2013
- Reaction score
- First Language
- Primarily Uses
This isn’t so much a tutorial on how to program so much as it is an introduction to how programming works. Some parts may seem oversimplified to people who already know what programming is, but this is intended to be an introduction to the basic concepts behind code.
What Makes Up a Program?
What Makes Up a Program?
Programming is done with programming languages, and for the most part, a programming language is something that can instruct the computer to store/change values and control flow-- control the order in which things should be done.
Storing values means the program can take a number, or a word, or some other kind of data, remember it, then recall it when asked to. Values are stored within a variable.
You are familiar with variables from math. If I say x is 3, then ask “What’s x + 1?” you say “4” because you mentally substitute x with 3, then add 1 to it.
Variables also have identifiers. An identifier is just a fancy word for the name of the variable— that is, what is used to refer to the arrow. In the math example above, the identifier for the variable is x, and the value of the variable is 3. And since 3 is a whole number, the type of the variable x is an integer.
Flow control tells the computer how the code should be read, and which instructions to execute. It’s like saying “If this is true, then do this. Otherwise, do this” or “Do this 10 times”. Flow control can tell a computer to skip portions of code, or run a single section of code multiple times. Like a tree trunk that splits off into many branches, code can branch with the usage of flow control. It is the ability to control flow that makes programs versatile and responsive.
What role do these two concepts play in the scripts and plugins we see in RPG Maker engines like XP, VX, VX Ace, and MV, then? Let’s take a look at some examples.
Our first example is the famous Word Wrap script written for VX Ace by KilloZapit.
The below stores the value of true into the variable named DEFAULT_WORDWRAP, as well as some others. For learning purposes, we're just going to focus on the first variable, DEFAULT_WORDWRAP.
Here, the value of DEFAULT_WORDWRAP is stored into the variable @wordwrap. Since DEFAULT_WORDWRAP is pointing to the value true, @wordwrap is set to true.
The reason why there's an @ is because it's an instance variable. I won't explain in too much more detail, since those are slightly more difficult to understand. They function pretty much like normal variables, except they only exist at a certain layer of code. For example, you might own a house with a lawn. People can walk around on your lawn, but without your key, they can't get into your house. The instance variables are inside that house, and aren't accessible outside the program.
Now, you saw this "alias_method" thing in the previous image, but I didn't explain it until now. What "alias_method" does is it sets the identifier process_character_kz_window_base to the portion of code named process_character. After that, the script adds onto the portion of code, then it tells the computer to run the original portion of code that was stored into process_character_kz_window_base with "process_character_kz_window_base(c, text, pos)".
The second red box, the "if @wordwrap (is true, then do the following)" asks the computer to recall the value that @wordwrap was set to. Previously, we saw that @wordwrap was set to DEFAULT_WHITESPACE which was set to true. So let's do some substitution- "if @wordwrap is true, then do the following" is equivalent to "if true is true, then do the following". And indeed, true is true, and so the indented portion will be run by the computer.
Now, there's some other stuff after that "if @wordwrap". That whole line is, after all, "if @wordwrap && c =~ /[ \t]/". That green part is something called "regex matching", which is even more advanced than instance variables, so I won't explain it much, either. The line in its entirety says "if @wordwrap is true AND (&&) c matches a certain regex pattern, then do the following" However, if you're curious, you can look it up in your spare time.
Now, that was a VX Ace example, let's look at a MV one! This is from NonCombat Menu, by yours truly ^^
NCMenu.showGoldWindow is set to "whether or not the parameter named 'show gold window' is equal to 'yes'". It's given a true or false value depending on the plugin parameters that the user configures. In this case, the parameter called Show Gold Window is set to yes, so NCMenu.showGoldWindow is set to true.
Similar to in the VX Ace example, the computer will createGoldWindow (create the gold window) if NCMenu.showGoldWindow is true. With the same substitution, we get "if true is true, do the following" and so the gold window is created.
If you want to learn to program, I would recommend codecademy or KhanAcademy's Hour of Code. But if you simply wished to understand how the dozens of scripts and plugins floating around in that project of yours worked, I hope this was a sufficient introduction.
Thanks for reading!
This tutorial was created for the Learning Together event.