What Makes Up a Program?

Discussion in 'Non-Maker Specific Tutorials' started by mjshi, Sep 25, 2016.

  1. mjshi

    mjshi Jack of Most Trades Veteran

    Messages:
    815
    Likes Received:
    625
    Location:
    USA
    First Language:
    English
    Primarily Uses:
    N/A

    What Makes Up a Program?

    Introduction
    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.

    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

    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 in code work much the same way. A variable is like an arrow that points to a value. You can assign a variable to point to any kind of data that you like. Some programming languages, like Java (NOT JavaScript!) also require that you give variables a type. Giving a variable a type means that the variable can only point to a certain kind of data. For example, a variable assigned an integer (whole number) type can only be given values that are also integers. With the arrow analogy, it’s like painting the arrow green, and from then on that arrow can only point to green things. If you asked it to point to something red, then it would become confused and throw an error.

    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

    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.

    Examples

    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.

    RED: identifier
    BLUE: value


    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.

    Screenshot_1.png

    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.

    Screenshot_2.png

    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.

    Screenshot_3.png

    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.

    Screenshot_4.png
    Screenshot_5.png

    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.

    Screenshot_7.png

    Conclusion

    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.
     
    Last edited: Oct 9, 2017
    #1
  2. Olavo Tozete Tercini

    Olavo Tozete Tercini Villager Member

    Messages:
    16
    Likes Received:
    0
    Location:
    Lille - FR
    First Language:
    Portuguese
    I loved this stuff!! Thank you
     
    #2
  3. mjshi

    mjshi Jack of Most Trades Veteran

    Messages:
    815
    Likes Received:
    625
    Location:
    USA
    First Language:
    English
    Primarily Uses:
    N/A
    You're very welcome ^^ I'm glad you liked it.
     
    #3

Share This Page