The place for your creative needs

Programming in Ruby – Tutorial 2 – Baby Steps

Jul 1st, 2009 | By GubiD | Category: Game making

Welcome to the 2nd tutorial offered by me (GubiD) to help you learn and understand the Ruby Programming Language. Specifically for its uses in RPG Maker. If you have not already followed my first tutorial then you should follow this link! Well, shall we get under way now?

Writing Code in RPG Maker – The Editor

Well, before we get underway let me teach you some tricks and some useful tidbits that everyone programming in RPG Maker should know.

  1. F1 is help! – You can actually be surprised at the amount of information held in the help files. Telling you how to track down errors and more. Take a read of some of the material if you have a moment.
  2. Current Section Search( Shift + F) or All Sections Search (CTRL + Shift + F) – I cannot tell you how often I use these hot keys! Without these, I would be scrolling through code for hours trying to find a particular something.
  3. Replace ( CTRL + H ) – Ever name something that you didn’t like? I do this from time to time when I am trying to rename something, but this function makes it much faster. Be sure to always look at what you are replacing and that it is replaced correctly before proceeding!
  4. F3 – Repeats the previous search criteria on the current document.
  5. Forums – There are lots of scripters out there that are willing to lend a helping hand if you get lost in your own code or just want to talk theory with. If you are having a tough time trying to resolve a problem and the help isnt ‘helpful’, then post it here on the Creation Asylum Forum’s to get some help.

Other useful tidbits about the editor include:

  • Highlights – nearly all keywords when typed correctly highlight themselves to let you know that it is reserved. ( Green for comments, Blue for key words, Purple for text, Red for Numbers, etc)
  • Anytime you want to comment out an entire section of code but don’t want to add a # to the beginning of each of the lines. Just use the commands =begin and =end. One note about these comment codes, is that they must be placed at the beginning of the line (No extra spaces/tabs/etc before it) or it will not work and will produce a syntax error.

Well, have I drummed your eye’s enough with useless banter on the editor yet? If you don’t think so, then you should read it again. Otherwise I think we are ready to finally begin.

Your first real script

Well, it has been a long time coming, but you are finally here. You have been informed of many of the useful tools and tips to help you through this process. Now it is finally time to begin. To start we will be creating a small window to display some information. I will describe some of the things you will need to know as we go along.

To create a blank script first open RPG Maker to your desired project, then press F11 to open the script editor. Scroll to the bottom of the script section list and choose main. Press Insert on your keyboard to insert a new blank script entry. Highlight the new script entry and give it a name by typing in the box on the bottom of the dialog.

Now we are going to create a small window. Nothing really special, but we will have it print the name of the current party leader. So… lets begin.

Type in the following: (All code displayed here is in Blue, but your editor will display only keywords in blue etc)

class MyName_Window < Window_Base
end

As of right now, the window can be called and displayed. The Window_Base ‘super class’ already has some existing methods for creation(initialize)/update/refresh etc. However it requires 4 arguments(variables). These arguments are x,y,width, height. Now to call this window in game, I want you to create an event on the map, and have it use the ‘call script’ command on the 3rd page. Using that type in the following to ‘initialize’ the window.

@window = MyName_Window.new(0,0,240,64)

But we don’t want to have to tell it this stuff when we call it, so we are going to change it to be a little more friendly when calling.

class MyName_Window < Window_Base
def initialize
super(0,0,240,64)
end
end

What this does is change the initialize method for the current class, by calling the ‘super class’ with a preset x,y,width, and height. Now depending on what you are actually trying to do this may be desirable or not, but you can now call the window by doing the following in the call script:

@window = MyName_Window.new()

Now, shall we put something in the window? I thought you might say so. Before we continue though, you need to finish ‘initializing’ the window. To do this we have to create a ‘bitmap’ that is the size of the window minus borders. You can see this done in every default window script in RPG Maker. As for why they didn’t have an auto method is beyond me, but regardless, here you have it:

class MyName_Window < Window_Base
def initialize
super(0,0,240,64)
self.contents = Bitmap.new(width-32, height-32)
end
end

The line Bitmap.new(width, height) creates a blank foreground drawing board for the window. The window itself doesn’t change, but the foreground does. That is why scrolling etc is capable and when you don’t minus the borders you have a ‘arrow’ pointing down and right when created. This is to signify (from within the Window class) that there is more information that is not displayed in the current viewable area of the ‘window’. I hope I said that in a way you can understand it. But its basically like you looking out your bedroom window and only seeing part of the tree outside.

So.. anyway. Lets move on shall we? The next part is to actually draw the name on it. We will be doing this by adding that draw information to the ‘refresh’ method. To do this your code should look like the following:

class MyName_Window < Window_Base
def initialize
super(0,0,240,64)
self.contents = Bitmap.new(width-32, height-32)
refresh
end
def refresh
self.contents.clear #ensures that the current contents are erased
self.contents.draw_text(0,0,width-32, height-32, $game_party.actors[0].name)
end
end

Now, you will have noticed that I used what is called a ‘method’ of the bitmap class, 2 of them in fact. ‘.clear’ and ‘.draw_text’. Both are hidden methods of ‘Bitmap’ which is explained in the help file. The clear method takes no arguments and erases the current bitmap. Setting all the x,y coordinates of the bitmap back to clear (Color.new(0,0,0,0)). The draw_text command uses 5-6 arguments. When I say 5-6 arguments, I mean that 5 are required and the 6th is optional. The first 5 are x,y,width,height,text. The 6th is alignment, used for left justify(0), middle(1) or right(2). You don’t have to use these options, but they are available regardless. The width/height is the ‘area’ in which can be drawn on. In the case of text, it should normally re-size itself automatically to make itself fit if the text you are trying to display is too large for the ‘area’ in which you have chosen to draw it at the current font size.

So, enough explaining things, lets see the window again.

Repeat our last event command, no changes necessary.

You should get the following:

Now.. the thing about that window is that it never goes away right? So we need a method to get it to go away, commonly called disposed in programming. The good news is that its a built in method for ‘object’ which is the super class of well… everything in ruby. In fact we don’t even need to write anything extra to make this dispose, because that is handled inside of the Window hidden class. We simply need to tell this window when to dispose.

For this example we will dispose it when the ‘A’ button is pressed. We will add this to the ‘update’ method of the window so as to not clutter the script call event, but mind you it could be done there as well.

class MyName_Window < Window_Base
def initialize
super(0,0,240,64)
self.contents = Bitmap.new(width-32, height-32)
refresh
end
def refresh
self.contents.clear #ensures that the current contents are erased
self.contents.draw_text(0,0,width-32, height-32, $game_party.actors[0].name)
end
def update
super
if Input.trigger?(Input::A)
self.dispose
end
end
end

Now call your window again and press the ‘A’ button. Note that the ‘A’ button is according to the RPG Maker application and usually is not mapped to the actual A key on your keyboard. In most cases it is either the D or the Z keys.

You will find that it still doesn’t close as you would expect. Why is this? Its because you have to ‘loop’ to have it check for that input. To do this you need to update your command to be like this:

@window = MyName_Window.new()
loop do
Graphics.update
Input.update
@window.update
break if @window.disposed?
end
@window = nil

Now do you see why I didn’t want to clutter up that window? You only have so many lines in there and its the fastest way for me to show you stuff without creating an entire scene. Which we will do next tutorial. It will be something simple, but regardless you should grasp the main concept of these things.

Anyway.. go ahead and retest the game, and you will note that once the window is opened that you cannot do anything until the ‘A’ key is pressed. Once it is.. the window is closed and everything returns to normal.

I personally use this method to test my windows when I am creating stuff, because it is far faster than building an entire scene. Unless you make yourself a template scene etc. (which they have in VX, Scene_Base)

Well, I hope you enjoyed this brief tutorial and hope to see you back again soon. Our next tutorial will go over the Menu Scene (main portion only) to explain how it works and why, then we will play with it a little to help you get a better understanding of how things interact with each other.

Until next time!

About the author

GubiD I am me. Just a single person amungst the masses searching for something more out of life.
Tags: , , , , , , ,

2 comments
Leave a comment »

  1. This was a very good tutorial, it explained things clear and precise. I look forward to your next one.

  2. Thank you! You can also find some more updated tutorials on my youtube account which go much more into detail at http://www.youtube.com/user/gubid

Leave Comment