This is the third and final lesson in the series. Here we will go over a bit of math, look at how to work with pictures and finish off the tutorial with a look at arrays, hash's and modules.

The Numeric Class

Ok, so we know how to make text, and the fact we can make numbers show in windows, but do you know how to have some fun with them? Well, if you want to know how to add some sparkle to your script, keep reading.

 

Today, I will cover the 'Numeric' class, along with Integer and Float. It sounds long, but isnt. You are probably thinking 'Numbers? Math?! How can that be fun?!' Well, believe it or not, math, when used right, is extremely fun.

 

For example, let's say you want to make your window have 2 columns of items, do you know how to do that? Well, I'll teach you, so keep reading:

 

Let's start with the Numeric class. As it implies, it deals with numbers. There are only a handful of methods, but each can be useful in it's own way. Bear in mind 'self' means the variable or the number. SO that means wheter it says something like: -self, you would put -variable or -(80).

  • +self: Just returns the same number.
    Example: +80 = 80
  • -self: Makes the number negative.
    Example: -80 = -80
  • abs: Returns the absolute value of 'self'. Absolute value is how far from 0 the number is.
    Example: (-80).abs = 80
  • ceil: Retuns the smallest integer equal to or greater than self.
    Example: (8).ceil = 8
  • floor: Returns the largest integer that does not exceed self.
    Example: (2).floor = 2
  • round: Rounds the number off to the closest integer.
    Example: (8.28475).round = 8 or (8.7245) = 9 (If the first decimal place is equal to or greater than 5, it rounds up.)
  • truncate: Discards the decimal point and all numbers thereafter.
    Example: (8.28475).round = 8 or (8.7245) = 8 (Like round, but doesnt round up.)

 

Some of those will come in very handy. Others... not so much. In fact, some of these I had never heard of if not for this tutorial, but oh well. The ones I recommend familiarizing yourself with are round, and making a number negative. But, it is nice to know the different things availible, no?

 

I mean, floor and ceil could come in handy if youhave 2 variables being the lowest and highest numbers of a randomly generated number. Round is great if you are editing damage formulas, or multiplying/dividing.

 

Now, on to Integer. I haven't messed with many of the methods here, and I dont want to go into detail on things I am guessing about, so for now, we'll touch the basic methods of the Integer class. Remember, self is 1 number/variable, and other is another number/variable:

  • self + other: Returns the two values added together.
  • self - other: Returns self - other.
  • self * other: Returns self multiplied by other.
  • self / other: Returns self divided by other.
  • self % other: Returns the remainder of self / other.
    Note! In lesson 4, We'll cover how this can help us with making multiple columns!
  • self ** other: Returns self multiplied by an exponent.
    Example: 2 ** 3 = 2*2*2 = 8

 

That is basic math, as I am sure you realized, but hey, what can you do?

 

Now, let's look at comparisons. THese are things you would use in If statements like: if variable1 >= variable2. This works great for loops or switches:

  • self == other: Checks if self equals other.
  • self < other: Checks if self is less than other.
  • self <= other: Checks if self is less than OR equal to other.
  • self > other: Checks if self is greater than other.
  • self >= other: Checks if self is greater than or equal to other.
  • between?(min, max): Checks if the number falls between min and max. If so, returns true, else returns false.

 

The last two I'll cover from Integer for now are:
.to_s: Turns a number into a string. So if you did: (7).to_s it would come back "7" (String form.)
.to_f: Turns an integer into a float number. In other words, gives it a decimal and a '0' after it, then allows that number decimal places.

 

What is a float number? Just a number with decimal point and numbers after said point. At any rate, that is the basics of numbers. In the future, I'll do more details on numbers that will make your coding more efficient, but for now, we're just trying to get you to make your own systems.


Pictures

Now we'll talk about pictures, and all the fun involved. Now, with RPG Maker's scripting process, the window scripts and scene scripts are the guts of the script, but the pictures, and other related scripting techniques add the 'pizazz' to it. That extra sparkle.

 

Now, to call a picture, you must attain it from the 'cache' files. To do this, you use:

RPG::Cache.picture('filename')
Remember the 'RPG::Cache' function, because it is important. It allows you to pull any files from the imported files. Here is the entire list, however, they are also listed in the help file.

 

Cache Files

 

RPG::Cache.animation(filename, hue) 
Gets an animation graphic. Use hue to adjust its hue values.

RPG::Cache.autotile(filename) 
Gets an autotile graphic.

RPG::Cache.battleback(filename) 
Gets a battle background graphic.

RPG::Cache.battler(filename, hue) 
Gets a battler graphic. Use hue to adjust its hue values.

RPG::Cache.character(filename, hue) 
Gets a character graphic. Use hue to adjust its hue values.

RPG::Cache.fog(filename, hue) 
Gets a fog graphic. Use hue to adjust its hue values.

RPG::Cache.gameover(filename) 
Gets a "Game Over" graphic.

RPG::Cache.icon(filename) 
Gets an icon graphic.

RPG::Cache.panorama(filename, hue) 
Gets a panorama graphic. Use hue to adjust its hue values.

RPG::Cache.picture(filename) 
Gets a picture graphic.

RPG::Cache.tileset(filename) 
Gets a tileset graphic.

RPG::Cache.title(filename) 
Gets a title graphic.

RPG::Cache.windowskin(filename) 
Gets a window skin graphic.

RPG::Cache.tile(filename, tile_id, hue) 
Gets only a specified tile from a tileset. Use tile_id to specify the ID of the tile to retrieve and hue to adjust its hue values.

Used when a tile is specified in an event graphic (RPG::Event::Page::Graphic).

RPG::Cache.clear 
Empties the cache.
Now, when you call a picture, you need to call it using the Sprite superclass. to do this, you just use:

 

@sprite = Sprite.new #This sets the variable equal to a Sprite class item.
@sprite.bitmap = RPG::Cache.picture('filename') #Sets the sprite's bitmap to the picture 'filename'
Now, the Sprite class controls the picture (bitmap). This is real cool, because of some of the stuff the Sprite class can let you do with said picture.

 

For instance, let's say you only want to show a portion of the picture, you can use: src_rect
How to use it is you use: @sprite.src_rect.set(x, y, lx, ly) This is how they use a character set to walk. (search for src_rect in the script editor to see what i mean)
Here is an entire list of commands for sprites:

 

Commands

 

bitmap
Refers to the bitmap (Bitmap) used for the sprite's starting point.

 

src_rect
The box (Rect) taken from a bitmap.

 

visible
The sprite's visibility. If TRUE, the sprite is visible.

 

x
The sprite's X-coordinate.

 

y
The sprite's Y-coordinate.

 

z
The viewport's Z-coordinate. The larger this value, the closer to the player the viewport will be displayed. If multiple objects share the same Z-coordinate, the more recently created object will be displayed closest to the player.

 

ox
The X-coordinate of the sprite's starting point.

 

oy
The Y-coordinate of the sprite's starting point.

 

zoom_x
The sprite's X-axis zoom level. 1.0 denotes actual pixel size.

 

zoom_y
The sprite's Y-axis zoom level. 1.0 denotes actual pixel size.

 

angle
The sprite's angle of rotation. Specifies up to 360 degrees of counterclockwise rotation. However, drawing a rotated sprite is time-consuming, so avoid overuse.

 

mirror
Flag denoting the sprite has been flipped horizontally. If TRUE, the sprite will be drawn flipped.

 

bush_depth
The Bush depth for that sprite. This is a pixel value denoting how much of the sprite's lower portion will be displayed as semitransparent. A simple way to convey a sense of a character's feet being obscured by foliage and the like.

 

opacity
The sprite's opacity (0-255). Values out of range are automatically corrected.

 

blend_type
The sprite's blending mode (0: normal, 1: addition, 2: subtraction).

 

color
The color (Color) to be blended with the sprite. Alpha values are used in the blending ratio.

 

Handled separately from the color blended into a flash effect. However, the color with the higher alpha value when displayed will have the higher priority when blended.

 

tone
The sprite's color tone (Tone).

 

Just have fun with writing a class, then calling and manipulating a picture using each of these methods. If you need extra help, ask.


Arrays, Hashes, and Modules

By now, you should know how to use a good number of functions in RGSS:

  • How to build different types of variables.
  • How to build a class and methods.
  • How to build Windows, both basic and selectable.
  • How to build a scene.
  • How to use numbers.
  • How to use and manipulate pictures.

 

Now, before you go off thinking you can build a custom system from this, you may want to read on. Here, we will discuss the potential of arrays, hashes, and modules; all of which I find crucial in my scripts for intricate systems.

 

First, we'll start with the easiest of this trio:

 

Arrays

 

An array is a pretty cool thing. Let's use this old Gump analogy:
A variable is, say, a box for a single piece of chocolate. No matter what, it will always hold that one piece. An array is a box that can not only hold any number of chocolates, but you can do things with these! Sort them, take and dispose of them, use them for indexing... I know, not what you'd REALLY do with chocolates, but this is just an example.

 

The way this works is where a variable says : @variable = x, an array is simply @array = [].
The brackets are important here. This basically says we have an array that we can add any number of items to. But, what exactly can you do with an array, how do you set it, and how does it work? This is where we have fun.

 

@array = [1, 2, 3, 4, 5]
There is our array for our purposes. I just feel sorry for President Screwb, cuz that is the combo for his luggage. But, here is also a list of different, and most commonly used commands:

 

.include?
.sort - Creates a new array of the array you want sorted, and returns it sorted.
.sort! - Destructively sorts the array, and returns itself.
.size - Returns the size of the array.
[x] - Returns what is at position 'x' in the array. (if outside the array's specs, it returns nil.)
Probably doesn't mean much without an example, but that's what i'm here for!

 

@array = [1, 2, 3, 4, 5]
@array.include?(1) #returns true, because i is in the array.
@array.include?(6) #returns false, there is no 6 in the array.
include? is self explanatory. Just asks if that is included in the array.

 

@array = [5, 3, 1, 2, 4]
@array.sort
  #returns (in a new array) 1, 2, 3, 4, 5
@array.sort!
  #returns (and changes @array to) 1, 2, 3, 4, 5
There's sort and sort! for you.

 

@array = [1, 2, 3, 4, 5]
@array.size
  #returns 5
That size was simple. The array has 5 spots, so it returns the value of 5.

 

@array = [1, 2, 3, 4, 5]
  return @array[5]
   #returns fi-... wait, returns nil!
Okay... can anyone tell me why when we tried to call the '[5]'th spot in the array we got nil? No? Well, here's why. An array always starts counting at 0. So, although our array has 5 spots, the spots are number 0 through 4. A little hard to grasp at first, but just think of a number line. 0 is a number, and the most important one. So, in that previous example, to get the last number called, we'd use the number 4, not 5 in the brackets.

 

All of this may seem superfluous at first, but let's try something more real-scripted. Like a 'for' statement (from lesson2.2)

 

@array = [1, 2, 3, 4, 5]
for i in 0...@array.size
  @array[i] += 1
end
p @array #  Returns [2, 3, 4, 5, 6]
Now, the for statement, iin more detail works like this:
for - starts the loop
i - will be the number of times it goes through.
0 - the number 'i' starts at.
...@array.size - will go through however long the array is.

 

Then, you can do whatever you want to the array by using @array[i]. It will do it to EVERY item in the array.

 

For more information, check the RPG Maker help file under 'Array'.

 

If you have more questions, please, ask. For now, let's move to Hash.

 

Hash

 

Hash is a fun key word. A personal favorite. It will make any one thing another. First, you set it up using curly brackets rather than square ones {} <- those ones, right there. It is defined as @hash = {key => definition}

 

Now, another difference is every item leads to another, using the symbol: =>
Let me show you:

 

@hash = { 1 => 'one'}
This basically means in the hash, whenever you call up @hash[1], you will get the word 'one'. You are probably asking 'what the hell. why use this?'

 

Well, let's say you are building a mission script, and each mission has an id. you would use a hash to take the id, and identify different parts. so, let's say 1 is Missing Cat. You would have an array of all the names of the missions and the first one (number 0) would be missing cat. Now, in your selectable window, you have the names, and the cursor is on that first one. How would you get the data for the second window to tell about the mission? Use a hash. In the script, use the index of what it is on, call up the hash, and have that information with the same tag appear in the second window. Complex? yes. So here is an example:

 

@array = ["Missing Cat", "Lost Sword"]
@hash = {"Missing Cat" => "A little girl's cat has gone missing, and you must find it.", 
               "Lost Sword" => "An old soldier lost his prized sword. You need to find it for him."}

[Cursor is set to the mission that says missing cat in one window, once you code your scene, you can have the cursor call up the information for the Missing Cat in another window using this.]

 

I know, extremely complex, but you'll get it.

 

Now, all the fun stuff you can do with a hash?

 

.has_key?(x)
.keys
These two are the most common..has_key?(x) asks if the hash has the key 'x'; x meaning anything from a string to a number.
Then, .keys just calls up an array of all the keys.
For more methods for hashes, look in the instruction manual under 'Hash'.

 

Modules

 

The final thing we will cover today is modules. A module is nothing more than a scripted database, and this will be VERY easy to explain.

 

You put a method OUTSIDE of classes. These actually stand alone in code. To show you an example, it is like this:

 

module Example #you always define a module with the keyword module. Then give it a name.

  #Insert your variables, arrays, hashes
end
This is basically a place to store information. The point? Great for custom systems. Now, with this, you store everything as Constant variables. Then, to call them, you use: Module_Name::Variable_Name
Yes, you use two colons. Now, for the best example on how to use a module, array, and hash, I am going to redirect you all to my first script, Mission/Quest Script. It has all the elements we have discussed this far. If you cannot understand or read part of the script, I recommend re-reading the tutorial that covers it, and if you cannot get it then; ASK ME.

 

Now, until then, Take care.

Share


About the Author

No Comments

Quick Reply

Guest

Enter a username

Security check: What is 8 plus 2?