- Joined
- Apr 28, 2012
- Messages
- 247
- Reaction score
- 99
- First Language
- English
- Primarily Uses
- N/A
Zetu’s Noob Guide to Scripting in RPG Maker
Introduction:
Welcome to Zetu’s Noob Guide to Scripting. For this tutorial, I will be using RPG Maker VX Ace. You may be using older versions of RPG Maker, which is fine until we get into the specifics of RGSS3. Open up a fresh new project and make sure that "Game" -> "Show Console" is checked. Open up Script Editor (F11) and insert a new line under "Materials". This will be where you will be adding code for testing.
The simplest command in Ruby is the "puts" command. It simply puts out a line of text, as you indicate. Copy and Paste the following line in the Script Editor and run the project.
puts "Hello World"If you did everything correctly, you should see a window, which should look like the following.
Variables and Types:
Numbers:
Ruby supports two types of numbers, Integers and Floating Point Numbers. These are not the only two types, but these will be the ones we will be using for this tutorial, as they are the ones used 99.9% of the time. To define an integer, using the following syntax:
myinteger = 3To define a float, you may use one of the following...
myfloat = 3.0myfloat = 3.to_f # to_f converts a value to a float
Strings:
Strings are defined either by being encased in single quotes or double quotes.
mystring = 'hello'mystring = "hello"There are a few differences between the two declarations. For example, if you have a string in single quotes, you may easily and double quotes within the string, and vise versa...
"my 'string' here"'my "string" here'"This don't break the string" #String is not broken'Don't this break the string?" #String IS broken
Operators:
An operator is a expression or function. For example...
one = 1two = 2puts one#=> 1puts two#=> 2puts one + two#=> 3If you copy and paste the above code into your project, you can see that the "+" operator performed a simple addition, getting 3 from 1+2. Make sure you using matching types when adding together. Try running the following code...
puts 1 + "A"#=> Script line 1: TypeError occurred. String can't be coerced into Fixnum.Arithmetic Operators:
Addition "+"
Subtraction "-"
Mutliplication "*"
Division "/"
Exponential "**"
Modulo "%"
Arrays:
An array is a collection of variables. Arrays are used when you need more than one value stored in a single space. For example...
myinteger = 3myintegers = [1, 2, 3]The 'myinteger' variable has a single number 3, whereas 'myintegers' variable has three numbers, 1, 2, and 3. In order to obtain a number at a particular position in the array, use the "[]" method, putting the position in the braces, starting with 0 being the first.
array = [1, 4, 9]puts array[0]#=> 1puts array[1]#=> 4puts array[2]#=> 9puts array[3] #Note, this is outside of the limit of the array#=> Note that the addition operator can be used to combine arrays.
evens_digets = [0, 2,4,6,8]odd_digets = [1,3,5,7,9]all_digets = even_digets + odd_digetsputs all_digets#=> 0 2 4 6 8 1 3 5 7 9String Formatting:
The "%" operator is used to format variables enclosed in a list into a string. Two of the common operators for this are the %s and %d, which represents string and number respectively. Here is an example, before we break down how to use this operator.
# The following puts "Hello, Zetu!"name = "Zetu"puts "Hello, %s!" % name # The following puts "Zetu is 26 years old."age = 26print "%s is %d years old." % [name, age]Here are a few basics you should review...
%s - String (or any object with a string representation, like numbers)%d - Integers%f - Floating point numbersString Operations:
As you have learned above, a String is a series of characters. There are a few operations that are useful for strings...
astring = "Hello!"# The following gives the character length of the Stringputs astring.length#=> 6# The position, or index of the character (0 being the first.)puts astring.index("o")#=> 4# The amount of times a substring occurs in the given string.puts astring.count("l")#=> 2# Gives a substring, from index to index.puts astring[3,5]#=> lo!# An all caps or all lowercase version of a string.puts astring.upcase#=> HELLO!puts astring.downcase#=> hello!Conditions:
A Boolean has two different values... TRUE or FALSE. Each one is a different class (TrueClass, FalseClass). There are operators that return a boolean value. Here are a few examples...
x = 2# The == operator checks if a value is an exact match.puts x == 1#=> falseputs x == 2#=> trueputs x == 3#=> false# The > operator checks if a value is greater than.puts x > 1#=> trueputs x > 2#=> falseputs x > 3#=> false# The < operator checks if a value is less than.puts x < 1#=> falseputs x < 2#=> falseputs x < 3#=> true# The >= operator checks if a value is greater than or equal to.puts x >= 1#=> trueputs x >= 2#=> trueputs x >= 3#=> false# The <= operator checks if a value is less than or equal to.puts x <= 1#=> falseputs x <= 2#=> trueputs x <= 3#=> true# The == can be used with data types other than numbers.puts "ABC" == "ABC"#=> true# The include? method checks if an array includes any match.puts [1,2].include?(3)#=> falseputs [1,2,3].include(3)#=> trueIndentation:
Just as a side note: when programming. This is to indicate how some lines of code are grouped together. Each indentation is considere a block of code. Most blocks are ended with "end".x =
if a line starts with "if, def, class, while, until, begin, module, ect..." it will indent the next lineelse you may notice some lines indent inwards in theendThe "if" operator checks a condition, creating a new block of code that only runs if the condition is met.
x = 3if x == 3 puts "This string will appear in the console."endif x != 3 puts "This string will not appear in the console."endThe "else" statement checks for when the condition was not met.
x = 3if x == 3 puts "This string will appear in the console."else puts "This string will not appear in the console."end Loops:
Loops is a method that iterates give code in sequence.
odd_digets = [1,3,5,7,9]odd_digets.each do |n| puts n #=> 1 3 5 7 9endEach type of loop creates a block indentation. Here is a few more examples.
# The "times" operator creates a loop, where it iterates itself that many# times, counting up from 0 each time.5.times do |n| puts n #=> 0 1 2 3 4end# A range is defined by two numbers. (X..Y) is each number, from X to Y,# including themselves.(3..6).each do |n| puts n #=> 3 4 5 6end# A range defined by (X...Y) is the same as (X..Y), except that it does# not include Y itself.(3...6).each do |n| puts n #=> 3 4 5endA while loop will repeat itself as long as a condition is true.
puts x = 12while x != 1 # Run as long as X is not if x % 2 == 0 # If X is an even number puts x = x / 2 else # If X is an odd number puts x = x*3 + 1 end #=> 12 6 3 10 5 16 8 4 2 1endFrom inside loops, the "break" and "next" statements will alter the flow of the code. Next causes the loop to start the next iteration, while Break cancels it completely.
Introduction:
Welcome to Zetu’s Noob Guide to Scripting. For this tutorial, I will be using RPG Maker VX Ace. You may be using older versions of RPG Maker, which is fine until we get into the specifics of RGSS3. Open up a fresh new project and make sure that "Game" -> "Show Console" is checked. Open up Script Editor (F11) and insert a new line under "Materials". This will be where you will be adding code for testing.
The simplest command in Ruby is the "puts" command. It simply puts out a line of text, as you indicate. Copy and Paste the following line in the Script Editor and run the project.
puts "Hello World"If you did everything correctly, you should see a window, which should look like the following.
Variables and Types:
Numbers:
Ruby supports two types of numbers, Integers and Floating Point Numbers. These are not the only two types, but these will be the ones we will be using for this tutorial, as they are the ones used 99.9% of the time. To define an integer, using the following syntax:
myinteger = 3To define a float, you may use one of the following...
myfloat = 3.0myfloat = 3.to_f # to_f converts a value to a float
Strings:
Strings are defined either by being encased in single quotes or double quotes.
mystring = 'hello'mystring = "hello"There are a few differences between the two declarations. For example, if you have a string in single quotes, you may easily and double quotes within the string, and vise versa...
"my 'string' here"'my "string" here'"This don't break the string" #String is not broken'Don't this break the string?" #String IS broken
Operators:
An operator is a expression or function. For example...
one = 1two = 2puts one#=> 1puts two#=> 2puts one + two#=> 3If you copy and paste the above code into your project, you can see that the "+" operator performed a simple addition, getting 3 from 1+2. Make sure you using matching types when adding together. Try running the following code...
puts 1 + "A"#=> Script line 1: TypeError occurred. String can't be coerced into Fixnum.Arithmetic Operators:
Addition "+"
Subtraction "-"
Mutliplication "*"
Division "/"
Exponential "**"
Modulo "%"
Arrays:
An array is a collection of variables. Arrays are used when you need more than one value stored in a single space. For example...
myinteger = 3myintegers = [1, 2, 3]The 'myinteger' variable has a single number 3, whereas 'myintegers' variable has three numbers, 1, 2, and 3. In order to obtain a number at a particular position in the array, use the "[]" method, putting the position in the braces, starting with 0 being the first.
array = [1, 4, 9]puts array[0]#=> 1puts array[1]#=> 4puts array[2]#=> 9puts array[3] #Note, this is outside of the limit of the array#=> Note that the addition operator can be used to combine arrays.
evens_digets = [0, 2,4,6,8]odd_digets = [1,3,5,7,9]all_digets = even_digets + odd_digetsputs all_digets#=> 0 2 4 6 8 1 3 5 7 9String Formatting:
The "%" operator is used to format variables enclosed in a list into a string. Two of the common operators for this are the %s and %d, which represents string and number respectively. Here is an example, before we break down how to use this operator.
# The following puts "Hello, Zetu!"name = "Zetu"puts "Hello, %s!" % name # The following puts "Zetu is 26 years old."age = 26print "%s is %d years old." % [name, age]Here are a few basics you should review...
%s - String (or any object with a string representation, like numbers)%d - Integers%f - Floating point numbersString Operations:
As you have learned above, a String is a series of characters. There are a few operations that are useful for strings...
astring = "Hello!"# The following gives the character length of the Stringputs astring.length#=> 6# The position, or index of the character (0 being the first.)puts astring.index("o")#=> 4# The amount of times a substring occurs in the given string.puts astring.count("l")#=> 2# Gives a substring, from index to index.puts astring[3,5]#=> lo!# An all caps or all lowercase version of a string.puts astring.upcase#=> HELLO!puts astring.downcase#=> hello!Conditions:
A Boolean has two different values... TRUE or FALSE. Each one is a different class (TrueClass, FalseClass). There are operators that return a boolean value. Here are a few examples...
x = 2# The == operator checks if a value is an exact match.puts x == 1#=> falseputs x == 2#=> trueputs x == 3#=> false# The > operator checks if a value is greater than.puts x > 1#=> trueputs x > 2#=> falseputs x > 3#=> false# The < operator checks if a value is less than.puts x < 1#=> falseputs x < 2#=> falseputs x < 3#=> true# The >= operator checks if a value is greater than or equal to.puts x >= 1#=> trueputs x >= 2#=> trueputs x >= 3#=> false# The <= operator checks if a value is less than or equal to.puts x <= 1#=> falseputs x <= 2#=> trueputs x <= 3#=> true# The == can be used with data types other than numbers.puts "ABC" == "ABC"#=> true# The include? method checks if an array includes any match.puts [1,2].include?(3)#=> falseputs [1,2,3].include(3)#=> trueIndentation:
Just as a side note: when programming. This is to indicate how some lines of code are grouped together. Each indentation is considere a block of code. Most blocks are ended with "end".x =
if a line starts with "if, def, class, while, until, begin, module, ect..." it will indent the next lineelse you may notice some lines indent inwards in theendThe "if" operator checks a condition, creating a new block of code that only runs if the condition is met.
x = 3if x == 3 puts "This string will appear in the console."endif x != 3 puts "This string will not appear in the console."endThe "else" statement checks for when the condition was not met.
x = 3if x == 3 puts "This string will appear in the console."else puts "This string will not appear in the console."end Loops:
Loops is a method that iterates give code in sequence.
odd_digets = [1,3,5,7,9]odd_digets.each do |n| puts n #=> 1 3 5 7 9endEach type of loop creates a block indentation. Here is a few more examples.
# The "times" operator creates a loop, where it iterates itself that many# times, counting up from 0 each time.5.times do |n| puts n #=> 0 1 2 3 4end# A range is defined by two numbers. (X..Y) is each number, from X to Y,# including themselves.(3..6).each do |n| puts n #=> 3 4 5 6end# A range defined by (X...Y) is the same as (X..Y), except that it does# not include Y itself.(3...6).each do |n| puts n #=> 3 4 5endA while loop will repeat itself as long as a condition is true.
puts x = 12while x != 1 # Run as long as X is not if x % 2 == 0 # If X is an even number puts x = x / 2 else # If X is an odd number puts x = x*3 + 1 end #=> 12 6 3 10 5 16 8 4 2 1endFrom inside loops, the "break" and "next" statements will alter the flow of the code. Next causes the loop to start the next iteration, while Break cancels it completely.
Code:
x = 10while true x = x - 1 if x % 2 == 0 # If X is an even number next # Skip the rest can continue end puts x #=> 9 7 5 3 1 if x == 1 # If X is equal to 1 break # Exit the loop. endend
Last edited by a moderator:
