Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

RGSS for NEWBS

Let me first begin by stating that I did not originally create this tutorial, I repeat... to get through to thick people's skulls... I DID NOT CREATE THIS TUTORIAL!

However, I do have permission to repost it on this forum by the author named RPG from gaming world forums. He created a series of seven tutorials in all, in which in the end he discontinued. Yet, from time in and out I'll continue to post another tutorial in this topic. I'm really gonna try to avoid double posting  ;), so don't be afraid to reply, and share your feelings on this tutorial.

EDIT:
PeteD2S made a good point, I should have mentioned clearly that these are made for people with little to no programming experience, and that people with much experience can simple skim through a majority of it.

So now I share with you the tutorial that has had a lovely impact on learning RGSS for me.





Tutorial 1 : The Basics



Into the World of Scripting
Greetings, brave dummy! I'm RPG and I'll try my best to teach you Ruby Game Scripting Language (RGSS). This series of tutorials is aimed at people who have no programming experience, but such experience can also help you.

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.

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.

Functions
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 Draw
   Call Input Check
   Call Calculations
   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.


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

Arrays
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 [1], another person would be John Smith [2],.... and so on until you have John Smith [99999]. 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).

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?"
   Choices:
   - Yes
     Open the shop menu
   - No
     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.

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

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:



   
Code:
# 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).

Conclusion
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~

Summary

- 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)
 
Well, I thought I was going to learn something about RGSS, but the only thing I learned was how to do a comment and how to show text to the user. You should rename this to "Programming for Dummies/NEWBS".

But it was a very good tutorial, anyone that doesn't know a thing about programming will surely know alot more after reading this.
 
@pete  XD

Well, originally it's called "RGSS for Dummies," I just wanted to use a "synonym" for the title  ;)


Anywho, here is the second part of the tutorial for any of you new to RGSS.



Tutorial 2 : Variables


Introduction
Forget all about the first tutorial (the basics), I only wrote it to give you a general idea about scripting/programming. You need to understand the example from the last tutorial though, you need to know that ( print stuff ) displays stuff on the screen (in RMXP's case, it displays a message box) and that ( # stuff ) is a comment ignored by the compiler, it's just there to remind you of stuff or comment stuff out. Enough with stuff, let's get to some fun stuff. Variables are storage places for your stuff, you stuff your stuff in variables and then use them for other stuff. Did I mention that the Finnish word for stuff is Juttuja? And no, I'm not Finnish but I thought it was some interesting stuff.

Values
There are cases (many of them!) where you'd need to store some value in the computer's memory. But before discussing how or why would we do such a thing, let's try to understand what a value is. Most of the time, the value is a number. It could be someone's age, an enemy's HP, an ID number, a random number, number of saves, etc. The values of those 'variables' (they vary from person to person, enemy to another, game to game, etc.) could be 34 for age, 9999 for Enemy HP, 1921 for ID, 0.3563 for random numbers, 3 for saves. So basically, a numeric value is a number! Isn't that awesome? Now, a number could be an integer or a float. An integer has no fraction, like: 1, 34, -57, 9834, 0, -346. Integers are useful because you don't need fractions often, you can't have 3.5 living cows for example. Most of the time you'll be dealing with integers. (Actually, the variables used in RPG Maker XP/2K/3's point scripting are always integers) Sometimes you might want to use fractions, if you and your friend had one apple that you wanted to share, each of you would get half an apple. If we're using integers, you can't divide the apple so one would get the whole thing and the other won't get anything. We need to use fractions so that each of you get 0.5 (1/2) apple, and we use floats to represent such values. I can't think of many situations where you'd need to use floats in programming, they're useful in programs where you need to get scary things like angles, circles, flying cows and other stuff that gets you an F in school.
A special case of integer values are Booleans, values that can be either on or off, true or false. They are like RPG Maker XP/2K/3 switches, you either opened the chest or didn't, talked to the king or didn't, killed the cow or didn't. I said they're a special case of integer values while they are not, but thinking of them as the integers 1 and 0 could be useful. 0 could be off (false) and 1 could be on (true), but Boolean values aren't integers and you can't add two Booleans for example. Basically, the value of a Boolean is either true or false. You can either turn a light on (true) or off (false), and your nose could be green (true) or red (false), etc. The real use of Booleans is to evaluate expressions, which we'd discuss in the 3rd tutorial. (next one)
The final type of values that we are going to discuss here is strings. A string is basically a string (array) of characters, but that doesn't really tell anything, does it? Basically, a string is a sentence, or maybe a group of them, or maybe just some random words. Just think of strings as text, you might need to store the someone's name, some messages, etc. Strings in Ruby (RGSS) are placed within quotation marks (either ' ' or " "), some examples of strings would be: 'Alex RTPson', "Hello!", 'I had a big monkey and it was funny', "sretpok jifrjio uhure", 'abc'. It is recommended that you use single quotes ( ' ' ) whenever possible, I'd rather use double quotes myself (because most programming languages do) but using in Ruby isn't very efficient because the compiler (RMXP in this case) will need to check more things and that might slow things down. Back to strings, you store text as string values, you can also add some strings together, for example 'I like ' + 'cows'. Enough said.

Advanced Note
I'll often use the term 'store' to refer to how variables and values (objects) interact. In Ruby's case, the variable doesn't really store the value but rather refer to it. The value is stored in the memory and a variable or more refer to it. A variable is a reference to some value and two variables might refer to the same value and changing one variable's value also changes it in the other. However, I think using 'store' might make variables easier to understand for beginners, but that's just me.

Variables
Now, sometimes we might need to store a value for later use. We use variables to do that. A variable is basically a name (identifier) that represents a value stored in memory. For example, my_age could be a variable that stores your age. You can later use variables as you use their values; for instance, you can do arithmetic operation on numeric variables. In Ruby (RGSS) variables are all objects, but you don't need to worry about that for now. Keep in mind that variable names (identifiers) can't start with a capital letter for some reason (except in certain cases), here are some valid names: age, enemyHP, id_number, RaNdOm, num_of_saves. Variable names are also case sensitive, password and passworD aren't the same thing. Here's a little example of variables:



 
Code:
number1 = 9
   number2 = 1
   number3 = number1 + number2
   print number3   # 10


Basically, we declare two variables (number1 and number2) with values 9 and 1. We add the two variables and store the sum in a third variable (number3). Finally, we display the value of the third variable (number3). You can test it yourself, make a new event, go to the third page of the command list and paste the example. When you activate the event you should get a message box with the number 10. (another way to do it is adding a new section to the script editor and pasting that, you'll see the message box when you start the game) As you can see, variables act just like their values. The same example could be written as ( print 9 + 1 ). Also notice the comment ( # 10), it's on the same line as the print command. You can always do that, as long as it's after the code. In this series of tutorials, I'll always place a comment with the display value after the print command (function), there are times when you don't know the output value though... actually, you use variables because you don't! Our example is really stupid actually, why would we need to store these values in variables if we can use them directly? Why use variables?

The Need for Variables
There are cases where you don't really know the values you're dealing with. Maybe you wrote a program that asks the user for two numbers and then displays the sum. The user might type any two numbers, you can't just guess the numbers or anything. So, if we store the number (without knowing what the number is) in a variable, we can operate on it with ease. Another example is the stats of the hero in some game, you can't always know what the HP, level, strength, etc of the hero is, and you might not be able to know how many potions does the party hold. Why not? Because the game stats vary from time to time and from a person to another; we can just store the hero's HP in a variable and then use it as we please. A final example of the uses of variables would be constant values that you change, sometimes you know the value of something (someone's age for example) and you use that value heavily in your code. But what if the value changed? (that someone's birthday came and they're one year older) You'll have to change all the occurrences of that value in your code. Note that the value is constant (it doesn't change during the execution of your program) but it could change outside your program, so you just use a variable and change it's initial value whenever you like. As we can see from the previous examples, variables are used for values that 'vary'. In other words, variables are used to store 'variable' (changing) data. It might not be very clear at the moment, but you'll see how important they are as you learn more.

Assignment
Assignment refers to assigning a value to a variable, i.e saying that this variable stores this value. We use the equal sign for assignment, it might look mathematically incorrect as in the following example:



 
Code:
my_var = my_var + 7


It doesn't mean that my_var is equal to my_var + 7, it means that we calculate the stuff on the right side of the equal sign and then store it in the variable on the left side. So, think of the equal sign as an assignment (assign some value to some variable) rather than an equality. The variable is declared (the program knows about it) when you assign some value to it, here are some examples of assignment:



 
Code:
cow = "Hello this is some random text"
   magic = 7 + 13 - 9 * 100 / (1 - 3)
   teh_var = true
   heroHP = 9999


And some different ways to assign things:



 
Code:
cow = magic = teh_var = 3
   cow, magic = 1, 2


The first line is like saying "store 3 in cow, magic and teh_var". The second line is like saying "store 1 in cow, store 2 in magic".

Variable Fun
Now, let's have some examples of variables. Each example is basically a comment describing how things work, some variable operations and then a print statement. I'll use ( p stuff ) instead of ( print stuff ) from now on (because I'm lazy), both of them are the same.



 
Code:
# Just some mathematics. 
   # The * sign is used to indicate multiplication (number x number),
   # the / sign indicates division.
   a = 2
   b = 4
   p a + b   # 6
   p a - b   # -2
   p b - a   # 2
   p a * b   # 8
   p b / a   # 2

   # You can join two strings together by using the plus (+) sign
   text1 = 'mon'
   text2 = 'key'
   p text1 + text2   # "monkey"

   # We store a 'lol' in v1, we store v1's value in v2, we store "!!!" in v3
   # Then we print v1 + ' ' (space) + v2 + v3
   v1 = 'lol'
   v2 = v1
   v3 = '!!!'
   p v1 + ' ' + v2 + v3  # "lol lol!!!"

   # One of the ways to add some numeric value to a string is embedding it
   # using #{}, you just put #{number or variable} inside the string.
   # When you do that, you must use double quotes (" ") for the string.
   # Double quotes lets the compiler check the string to find any #{}s
   # otherwise (single quotes), no checking is done and no conversion either.
   num = 1337
   p "The number is #{num}"   # "The number is 1337"

   # Another way is using number.to_s, like this:
   num = 1337
   p 'The number is ' + num.to_s   # "The number is 1337" 

   # You can also convert strings to numbers.
   # You see, 3 and '3' aren't the same thing, you
   # can't do '3' + 1 because '3' is a string.
   # To change it into an integer use string.to_i
   var = '3'
   p var.to_i + 1   # 4

   # Just a note, you don't need to use variables
   # to use .to_s and .to_i
   p 'The number is ' + 1337.to_s   # "The number is 1337"
   p '3'.to_i + 1   # 4



Conclusion
Variables allow us to store values that vary, although all the examples shown didn't really show useful variables. Understanding variables is essential; you won't go far without using them. I tried to make this tutorial very easy to understand but I might've failed in doing that. Don't worry though, you can find tons of variable tutorials on Google or something, and you can always read this boring tutorial again. The next tutorial will be about flow control statements such as loops and the if statement. Later~

Summary

- There are cases when you'll need to store some values in memory. The values could be either numeric, strings or Booleans. Numeric values can be either integers (no fractions) or floats (fraction). Strings must be enclosed in quotes (single or double). Boolean values can be either true or false.
- Variables are basically identifiers presenting values stored in memory. You use variables just like you use their values. The names (identifiers) of variables must start with a small letter (most of the time) and are case sensitive.
- We use variables to store values that might change. We might not know the value of something at a given time but we still need to use that value, so we use variables..
- We assign values to variables by using the equal sign, which assigns the values at the right side of the sign to the variable(s) at the left side ( var = value ). Some other ways to assign variables is ( var1  = var2 = var3 = value ) and ( var1, var2 = value1, value2 )
- We can do mathematical operations on variables, join strings together, embed numbers in strings and convert variables from a type to another.
 
Tutorial 3 : Control Flow - Part A



Introduction
Our little variable and print programs were executed line by line; they weren't interactive and just displayed some text on the screen. Real programs aren't like that, they interact with users and other things to produce the desired output. Ruby (and all high level languages) introduces some statements (commands) that we can use to add interactivity to our scripts, mainly it introduces conditional statements and loops. A conditional statement (or expression) basically checks if a condition is met. If it is met, then some commands will be executed, otherwise some other commands might be executed,. As for loops, they allow you to execute a block of code over and over until a condition is met. Loops and conditional statements are essential in programming, if you don't learn them then you'll probably stay a worthless noob till the end of time! ;)

Boolean Expressions
A Boolean value is a value that can either be true or false. Think of a switch that can be either on or off, or a path that only goes left and right; there's no gray about Booleans, everything is black and white. You might be asked: "is u alex???", thre are two valid answers for that question, either "lol yes im alex!!!", or "no im not r u???". So if we had a Boolean variable called is_u_alex?, (variables can't have spaces, so we use underscores) the value of the variable could be true if you are Alex and false if you aren't. Makes no sense, I know. Let's try something more simple, the mathematical statement "1 is equal to 2" is false, while the statement "1 is equal to 1" is true. Here are some other statements that can be true or false:



Code:
"3 is bigger than 2"
"98 is equal to 89"
"0.01 is smaller than 0.1"
"3 is smaller or equal to 3"


I think you know which ones are true and which ones are false, if you don't then check this statement: "RGSS for Dummies isn't equal to RGSS for Idiots". Now, in programming we can compare two values to get a Boolean value that evaluates the comparison. We use "==" to indicate equality. Remember when I talked about variable assignment and I said that the = operator means assignment and not equality? "var = 5" means assign 5 to var, while "var == 5" means that var is equal to 5. Let's just try an example:



Code:
p 1 == 1


This will output 'true'. Basically we're asking RGSS to evaluate the statement "1 is equal to 1", just like we were evaluating statements earlier. Now, change both numbers and see the various results. "p 1 == 4" would output false, p "0.1 == 0.1" would output true, etc. You can even use strings like "cow" == "cow" or 1 == cow. It's really simple, the statement can be either true or false and RGSS evaluates it for us. Other than the equality operator "==", we can use ">" for bigger than, "<" for smaller than, ">=" for bigger or equal to, "<=" for smaller or equal to and "!=" for not equal to. These examples will help:



Code:
p 1 > 2  # false
p 5 == 5  # true
p 0 >= 0  # true
p 4 < 1  # false
p -19 <= 0   # true
p 1 == 8  # false
p 1 + 3 == 2 + 2   # true
p 4 != 7  # true


It's all simple logic, all of those are Boolean statements that can be true or false. Now, since they're Boolean statements, their evaluation values can be stored in Booleans, for example:



Code:
my_bool1 = 9 == 9
my_bool2 = 3 <= 1
my_bool3 = true
p my_bool1   # true
p my_bool2   # false
p my_bool3   # true


I just stored Boolean values in Boolean variables, this should help you understand the difference between "=" and "==", you use the former to store the values on the right of the "=" in the variable on the left of it, and you use "==" to compare two values for equality. Also, note that we can directly store true or false values in variables, just like I did with my_bool3.
Another thing you can do is to evaluate multiple statements at once. You can use "and", "or" and "not". You place an "and" between two statements and the new statement (statement1 and statement2) will evaluate to true if both statements are true, and to false if one or both of the statements are false. Examples:



Code:
p 1 == 1 and "hello" == "hello"   # true
p 1 > 5 and 5 >= 1     # false
p 9 < 3 and 6 == 3     # false


You place an "or" between two statements and the new statement (statement1 or statement2) will evaluate to true if at least one of the statements is true (one is true or both are true), and to false if both statements are false. Examples:



Code:
p 1 == 1 or "hello" == "hello"  # true
p 1 > 5 or 5 >= 1     # true
p 9 < 3 and 6 == 3     # false


Finally, place a "not" before a statement and the new statement (not statement1) will evaluate to true if the original statement (statement1) is false, and to false if the original statement is true. In other words, it returns the opposite of the original statement. "not 1 == 1" is the same as "1 != 1" or "1 is not equal to 1", which is false. Example:



Code:
p not 1 == 1   # false
p not 4 < 2   # true
p not (3 <= 8)   # false


In the last example, I used parenthesis around the statement, it's generally a good practice because it can make your code more readable, especially if you have many statements joined with and's or or's.
There's one more thing you need to know about "and", "or" and "not"; you can abrriviate them to &&, || and !. Actually, there's a difference in precedence between the word forms and the symbol forms, but it's not really that important (at the moment). Basically, using "&&" is the same as using "and", "||" is like "or" and "!" is like "not". Why do we use two &s and two |s instead of one? Well, both & and | have different meanings in ruby so remember not to confuse them with && and ||. I've rewritten some of the previous and-or-not example with the new symbols:



Code:
p 1 == 1 && "hello" == "hello"   # true
p 1 > 5 || 5 >= 1     # true
p !(4 < 2)    # true


The last example is interesting, "not 4 < 2" works, but "! 4 < 2" doesn't. ! has higher precedence than "<" and than "not". So RGSS reads "! 4 < 2" as "the opposite of true is less than 2", wait... the opposite of true? Well, you see... every value in RGSS is true expect for the values false and nil, so the value 4 on its own is true. You don't understand? Never mind then! Just forget all about this precedence thing for now, but remember to always (most of the time actually) use parenthesis around statements when using ! instead of not.

Important Note
= is NOT the same as ==, I know I mentioned that already but this is one common mistake in programming. Even professionals working for Microsoft might fell for it and use = instead of ==. Always remember that = is only used to assign values, and that == is used to test for equality of two values. I know you'll do this mistake sooner or later (I did, a lot!), but when something isn't working in your program, it's a good idea to check the comparisons to see if you used = instead of ==.

Summary
- A Boolean expression is an expression that evaluates to either true or false.
-  Comparison operators such as (>, ==, <) are used to compare two values and produce a Boolean value.
- More complex Boolean expressions can be formed by combining two or more expressions with && (and) or ||. (or)
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top