Home > Tutorials > Ruby Game Scripting System > RGSS For Dummies Tutorial 1: The Basics
RGSS For Dummies Tutorial 1: The Basics
Greetings, brave dummy! I'm RPG and I'll try my best to teach you Ruby Game Scripting System (RGSS). This series of tutorials is aimed at people who have no programming experience, but such experience can also help you.
1. About RGSS
2. About Coding
7. Control Flow
9. First Program
1. About RGSS
RGSS (Ruby Game Scripting Language) is -as the name implies- a scripting language based on Ruby. Ruby is an object-oriented programming language created by Yukihiro Matsumoto; you can find more information at Ruby's official site (http://www.ruby-lang.org). I think RGSS is easy to learn and use, although it could look weird at times. With RGSS, you can have great control over your RMXP game; you can modify almost everything, and you can add almost everything. Basically the limit is your ability and knowledge.
2. About Coding
Computers are dumb, they don't really think like you (assuming you do~). The computer follows basic instructions to perform operations, you need to give it some commands and it'd execute them. For example, if you want to know the sum of 1 and 2 you'd ask the computer something like "Please calculate the sum of 1 and 2". But then, you don't need to be polite to the computer and it doesn't make a difference to it wither or not you use 'the' and 'of'. So we end with "calculate sum 1 and 2". Well, why use too many words anyways? We can just do "calculate 1 + 2". Wait, why add calculate? What else can it do with 1 + 2? Make a song about a 1, a plus sign and a 2? So, we end up with "1 + 2", simple, isn't it?
The point of the whole thing is that the instructions we give to the computers are written in some sort of simplified English (talking about high level languages here). Programming languages, like natural ones, have a set of words and rules. However, there aren't as much words or rules in programming languages, only the 'useful' things are there. Learning the important words and rules (or grammar, syntax, whatever) doesn't mean you've learned the language. Let's say that I know all about English grammar and that I've know every word the dictionary, can I got to a person and say "Your tea is good for egg production under the light of the new super monkey nose."? Well, I can. But it doesn't really make much sense, it's not a logical thing to say. Same with computers, you don't just give it stupid instructions just because they are valid. Things should make sense or else you might get some nasty errors.
You can give the computer only one command, but you'd most likely give it more. For example (not real code):
Ask the user for the password
Wait for the user to type in the password
If the password the user typed is correct, play some victory music
If the password is wrong, punch the user in the face
Duplicate the program and spread copies all over the user computer
Start sending e-mails with copies of the program
Become one of the most famous viruses
Uh, maybe I went a bit too far... but I hope you get the basic idea. The group of instruction we give could form a block of code, and although it doesn't sound that important, it is. Long ago programs were just lines of code with little organization; everything was in one big block that is executed line by line from top to bottom. If for some reason you wanted to go up again, you'd ask the computer to go to that line. The whole thing was very messy and produced 'spaghetti code' (no, not code written in Italian. Just messy unorganized bad code). It looked like this:
Display a message "Welcome to the super program 007!"
Ask user to input a number
Ask user to input another number
Calculate the sum of the numbers and display it
Display a message "Do you want to exit? (Y)es / (N)o"
If the user types 'Y', exit the program
If the user types 'N', go to line 2 (ask for input for another two numbers)
Basically every time you choose no, the program will be executed again. What's wrong about that? It's not really obvious in that little example. But in big complicated programs, it'd end up like a maze with lots of ‘go to's and jumps from line to line. When the code is too long, it'd be hard for you to follow and understand it, which makes it hard to improve it or fix bugs.
Well, worry not! Functions are there to fix this. Sometimes functions could be called Subroutines or Methods (Ruby calls them that, but it's not very accurate). Basically a function is a block of related code that you 'call'. You create a function, have all your code inside it and then call it as much as you want. There is no need to re-write stuff or to jump around like a monkey. Functions organize your programs and makes thing simpler. Well, here's an example:
Function name: Sum
Function takes two values and calls them num1 and num2
>>Function Starts Here:<<
Calculate the sum of num1 and num2 and display it
>>Function Ends Here<<
Call the function Sum with values 1 and 2
Call the function Sum with values 10 and 5
Call the function Sum with values 29 and 7
Running this program (it's not real btw) would display 3, 15 and 36. Instead of writing the calculate code every time, we just call the function. This can be very useful, let's say you're making a game and made a function to draw things, another to check input and another for calculations, you can then do:
Call Input Check
Go to line 1 (Draw)
Whoa! You've got a game! (Not really) This might make little sense, but just know that functions are blocks of related code that could be called. I'll explain them in more details later. Also, keep in mind that Functions are called Methods in RGSS, I used the world 'Function' here because that's the commonly used term in programming. I'll use methods when talking about RGSS though, just remember that they are the same thing.
Wait, why am I discussing variables after functions? Shouldn't they be discussed first? I don't know, I think that code->block->function flows well, but I could be wrong~
Variables are places to store data on your computer. Think of a variable as a locker that you put something in, and then you can refer to the the thing by saying the locker's number. So you could say "Please give me the monkey in locker 5" or just "5". Not a very good example... remember how the sum function in the previous example accepted two values (num1 and num2) and calculated their sum? You see, num1 and num2 could be anything, they could be 1 and 2, 10 or 5, 29 and 7. We just store the two numbers in two variables called num1 and num2 and they'd act as if they were the actual numbers. Still don't get it? You know how students could have ID numbers in school? My ID number is 1921 and when I'm having an exam they ask me to write my ID number on the paper. They don't care about my name because they can just use my ID number to get everything they need to know about me, so the ID and me are the same. A variable is like an ID number (it doesn't need to be a number, could have letters like 'FOA' for my name's initials), it represents a certain value just as an ID number represent a student. Makes no sense? Too bad you're just dumb :(
Nah, just kidding, it'll become easier to understand when we have real code examples.
Let's say that in some city they have many people with the name John Smith. People often get confused and if you yell "John Smith!!" in the street then tons of people would look at you. Now that's a big problem, for example the city mayor (Mr. John Smith) was arrested because he was a suspect of some crime (Murder of John Smith) but it turned out to be a mistake because the real murderer was another John Smith. So, the mayor was angry and decided to do something. He decided that all John Smithes should have numbers. The mayor would be John Smith , another person would be John Smith ,.... and so on until you have John Smith . The people in the city lived happily ever after and having too many Johns wasn't a problem anymore. In programming, there are situations where you'd have many variables with similar functions or names. You might need variables to hold all the skills your hero has. But if they had 100 skills, it'd be annoying to create 100 variables and manage them. So, we use arrays, one variable that holds many values. They allow you to easily access values by referring to them by numbers (called index).
7. Control Flow
Your program would often need to examine things, take decisions and interact with the user, it might also need to do the same thing many times. Now, you know the Show Choices (or whatever it's called) command in rm2k/3/xp? You provide it with some commands to choose from and things to do based on the chosen command. When the user plays the game, they'd see the choices, pick one and different things would happen. Like:
Message Box "Hello I sell cats, are you interested?"
Open the shop menu
Message Box "Don't want to buy anything? Too bad!"
Now without having such a choice, the user would be forced to view the shop menu even if they don't want to buy anything. Another example is the conditional branch (if command) in RPG Maker, you can use it to check if the player has a potion, do something if they do, do something else if they don't. In programming languages you'd often control the flow of code. In one of our examples, we checked if the password is correct or not. In another program we'd loop to display an annoying message 100 times without typing 100 lines of code. More about that in some future tutorial.
Our world is object-oriented. It is composed of entities that have special properties and can interact with each other. A human is an object that has a name, an age, an occupation. This human can walk, think, eat, get killed, and do other things. An apple is an object; it has properties such as origin, weight, color, percent of water in it and a size. The apple can also be eaten, can fall off a tree, can be thrown in the sky, etc. A human can interact with an apple by eating it, throwing it, holding it, etc. Think of things this way, and you will have no problem with object-oriented concepts.
Computer programs tend to simulate the real world, they draw things like humans do, they calculate things, print things or even simulate things. Games try to simulate the real world all the time, a character in the game has data such as it's sprite file name, it's size on screen, it's position and more. It can also do things like jumping, swimming, killing monsters, etc. The character can interact with the map and with other NPCs and enemies. Do you see the connection? If programs were written in a way that actually simulates the real world we can have more effective programs, and this is where object-oriented languages came from. In programming, an object is an instance of a class, a class is a (user defined) data type. Each object has special properties (variables that describe that object) and methods to operate on these properties (functions).
So basically, an object combines both data (variables) and functions (methods). Objects can inherit other objects (A human is an animal, an apple is a fruit) and can interact with each other. In RGSS, almost everything is an object. You will often create classes and use existing ones, just take a look at the script editor. Notice how each page starts with the word 'class'?
Don't worry if you don't understand this, I'll explain it all in more details later. You might like to read it again then.
9. First Program
Well, tired of all that explaining? Want to try something yourself? Open RMXP, add an event and choose some picture for it, double click the command list thing (the big white box), go to the last command page (3?) and choose the last option, 'script'. Now in the text box type the following:
# I'm a useless comment
print 'Hello World!'
Now click Ok, and another ok at the event page. Run the game and press the action key when facing the event you created (talk to it) and you should get a message box with a "Hello World!" message. Cool, isn't it?
So, what did we do? The first line (# I'm a useless comment) is a useless comment. Comments are ignored by the compiler (RMXP), only you can read them when you're viewing the source code. To add a comment start a line with # and add whatever text you want (on one line). Comments are useful to clarify the code and remind yourself of things. The next line (print 'Hello World!') displays a message box with the text "Hello World!". The print command (method, actually) displays stuff on the screen, you just do print stuff. Note that we enclosed the text in quotation marks ' ', we always do that so that the computer knows that we aren't writing instructions (codes), but just some text to be displayed. Also note that each instruction (command) is on a separate line, this is important (there's a way around it though).
Well, that's all for today. The aim of this tutorial was to briefly introduce you to different programming concepts. You aren't expected to understand everything but just have a basic idea about how things work, I recommend reading the summary. In future tutorials I'll discuss everything we talked about in details and with real code examples, until then you can try playing around with the print function and displaying different things like numbers (you don't add quotes around numbers). The next tutorial will teach you more about variables, see you later!
- Computers are dumb, you need to give them detailed instructions. A programming language is basically a set of instructions with rules and keywords.
- Blocks are used to group related instructions.
- A function is a block that you can call.
- Variables store some values for later use.
- An array is a variable that has many values.
- We can control the flow of programs using things like if statements and loops.
- Objects are entities that has some data (variables) and methods to operate on the data (functions).
- We use ( # stuff ) to add comments.
- The print function displays numbers or text (or something else) on the screen. (or a message box)
RGSS For Dummies Tutorial 1: The Basics