Submit Your Article


 
RPG Maker

Welcome Guest ( Log In | Register )


  Games Resources RPG Maker VX RPG Maker XP Scripts Tutorials Downloads

> [Scripting][Series]RGSS: Windows - Lesson 1: An Introduction to Windows
ccoa
post Jan 4 2008, 07:12 AM
Post #1


Storm Goddess
Group Icon

Group: Revolutionary
Posts: 989
Type: Scripter
RM Skill: Masterful




Ccoa's Guide To Windows In RGSS

What is a window?

Let's start out with the basics. What is a window? A window is essentially an image that you can draw on. The image is "skinned" with a windowskin that you specify. Windows are the backbone of all the scenes in RMXP.

Making a Simple Window

Let's jump right in and get our feet wet, yes?

Open up your script editor in a new project. Right click Arrow_Base and select Insert. Name the new section Window_First. Type the following in the script editor:

CODE
class Window_First < Window_Base
  
end


This tells the Ruby interpreter that there is a new class named Window_First, and that it is descended from Window_Base. The magic of inheritance means that Window_First has inherited all of Window_Base's data members and methods. In plain English, Window_First can now do anything Window_Base can do.

However, it can't do anything of its own. Let's remedy that, shall we?

CODE
class Window_First < Window_Base
  #--------------------------------------------------------------------------
  # * Object Initialization
  #--------------------------------------------------------------------------
  def initialize
    super(0, 0, 256, 96)
  end
end


The initialize method is special. It's what's automatically called when you create a new object of the class (in this case, a new window). The super keyword tells the interpreter to go to the parent class, and look for a method of the same name. In this case, it's looking in Window_Base for a method named initialize. Look in Window_Base:

CODE
  #--------------------------------------------------------------------------
  # * Object Initialization
  #     x      : window x-coordinate
  #     y      : window y-coordinate
  #     width  : window width
  #     height : window height
  #--------------------------------------------------------------------------
  def initialize(x, y, width, height)
    super()
    @windowskin_name = $game_system.windowskin_name
    self.windowskin = RPG::Cache.windowskin(@windowskin_name)
    self.x = x
    self.y = y
    self.width = width
    self.height = height
    self.z = 100
  end


See the (x, y, width, height) parameters? These line up with the (0, 0, 256, 96) arguments we passed into the super call. In other words, we're making a window that is a coordinate 0, 0, and is 256 pixels wide and 96 pixels high.

Not bad, but the window still isn't doing anything! Let's add some text to it.

CODE
class Window_First < Window_Base
  #--------------------------------------------------------------------------
  # * Object Initialization
  #--------------------------------------------------------------------------
  def initialize
    super(0, 0, 256, 96)
    self.contents = Bitmap.new(width - 32, height - 32)
    
    refresh
  end
  
  #--------------------------------------------------------------------------
  # * Refresh
  #--------------------------------------------------------------------------
  def refresh
    self.contents.clear
    
    self.contents.draw_text(0, 0, 256, 32, "Hello, World")
  end
end


Let's take this a little at a time.

CODE
    self.contents = Bitmap.new(width - 32, height - 32)


This creates a new bitmap for the window to draw on. This line is necessary if you want to place any text or draw images on the window. Because of the limitations of the Window classes, there is a 16 pixel border inside every window that cannot be drawn on. Thus the - 32's.

CODE
    refresh


This is a call to the method titled refresh. Everything in that method will execute until it reaches the end of the method or it hits a return statement, and only then will we return to the initialize method to finish.

CODE
  def refresh


We just defined a method called refresh. Looking around, you'll notice most of the RGSS Window classes have refresh methods. They usually do exactly what they sound like - refresh the image.

CODE
    self.contents.clear


This "erases" anything that might already have been drawn on the bitmap.

CODE
    self.contents.draw_text(0, 0, 256, 32, "Hello, World")


This asks the bitmap object to draw some text on itself. The draw_text method looks like this:

draw_text(x, y, width, height, text, alignment)

x is the x coordinate to draw it at, and y is the y coordinate. Note that these are coordinates on the window, not the screen. 0, 0 is the upper left corner of the window.

Width is how wide to fit the text into. If the text won't fit, the method will try to squeeze it in, making the text narrower. Height works similarly for height.

Text is the text you want to draw. In this case, Hello, World.

Alignment is optional. By default, it's 0, which is aligned left. 1 would center the text, 2 would right align it. So if we changed the above line to

CODE
    self.contents.draw_text(0, 0, 256, 32, "Hello, World", 1)


The text will be drawn centered inside the 256 pixel width you specified.

Now let's take a look at this window. Create a new event. Place the following in a Script event command:

CODE
$window = Window_First.new


$window is the name of your variable. The $ indicates that it's a global variable - that is, you can access it from anywhere. This is the name of your window object.

Window_First.new tells the interpreter that you want a new object of class Window_First. This will automatically call the initialize method we wrote just a moment ago.

Now put in a short wait, say 80 frames, using the Wait event command.

Finally, put the following in a Script event command:

CODE
$window.dispose


The dispose method will make the window disappear. It's very important to dispose windows, even if they're not currently visible. If you don't, they'll hang around forever and lag your game.

But wait, you say. We didn't write a dispose method!

That's okay, the dispose method exists in Window_Base:

CODE
  #--------------------------------------------------------------------------
  # * Dispose
  #--------------------------------------------------------------------------
  def dispose
    # Dispose if window contents bit map is set
    if self.contents != nil
      self.contents.dispose
    end
    super
  end


Remember when I said Window_First can do anything Window_Base can do? That means accessing any and all methods defined in Window_Base.

The end result:



Homework

Try it yourself. Play with the code, show me what you can do. Some time after I get at least 5 homework assignments, I'll write the next part.


__________________________
Go to the top of the page
 
+Quote Post
   
 
Start new topic
Replies
LOD
post Mar 17 2008, 07:20 AM
Post #2


Level 3
Group Icon

Group: Member
Posts: 34
Type: Writer
RM Skill: Intermediate




dammit what happened? i followed all the steps by copying the scripting and all i get is a message of error saying that an error ocurred on Window_First!


__________________________
" The entire world is a script. The problem is that God is a mapper."

Go to the top of the page
 
+Quote Post
   
storymasterq
post Mar 17 2008, 07:43 AM
Post #3


Level 7
Group Icon

Group: Member
Posts: 91
Type: Developer
RM Skill: Skilled




QUOTE (LOD @ Mar 17 2008, 09:27 PM) *
dammit what happened? i followed all the steps by copying the scripting and all i get is a message of error saying that an error ocurred on Window_First!


Can you be specific and tell us what the error was?

Cheers,
SQ


__________________________
Current Skills: Writing(Str) Lv. 4, Writing(Evt) Lv. 4, Scripting Lv. 3, Mapping Lv. 1, Spriting Lv. 0, Composing Lv. 0
Currently expanding: Scripting
[Show/Hide] My Personality

QUOTE (Arthur C. Clarke)
We have to abandon the idea that schooling is something restricted to youth. How can it be, in a world where half the things a man knows at 20 are no longer true at 40 - and half the things he knows at 40 hadn't been discovered when he was 20? (link)
Go to the top of the page
 
+Quote Post
   

Posts in this topic
- ccoa   [Scripting][Series]RGSS: Windows - Lesson 1: An Introduction to Windows   Jan 4 2008, 07:12 AM
- - Asi   Thanks this is is almost too easy (witch is perfec...   Jan 7 2008, 02:11 PM
- - ccoa   For playing around, you can try changing the windo...   Jan 7 2008, 02:48 PM
- - Asi   I already did all of the changes mentioned And I...   Jan 7 2008, 03:02 PM
- - Holder_of_Life   Ah I remember seeing this a while ago. I was tryi...   Jan 7 2008, 03:03 PM
- - ccoa   There's no way you could have seen this before...   Jan 7 2008, 04:42 PM
|- - Asi   thanks -Asi PS congarts on making staff   Jan 7 2008, 05:30 PM
- - Zeriab   What a cute tutorial ccoa. I like it Here's a...   Jan 8 2008, 06:34 AM
|- - Asi   QUOTE (Zeriab @ Jan 8 2008, 01:41 PM) @As...   Jan 8 2008, 07:49 PM
- - ccoa   Don't worry, I'm going to cover adding a w...   Jan 8 2008, 06:37 AM
- - Holder_of_Life   QUOTE (ccoa @ Jan 7 2008, 11:49 PM) There...   Jan 8 2008, 08:33 AM
- - Punk   Whoa! I was a member here? (OT and I have not ...   Jan 8 2008, 12:10 PM
- - ccoa   Hey, Punk! Good to see you stepping outside y...   Jan 8 2008, 12:24 PM
|- - Punk   QUOTE (ccoa @ Jan 8 2008, 02:31 PM) Hey, ...   Jan 9 2008, 03:54 AM
|- - bryandamage   *cries* I cant do a simple script I even copied a...   Jan 16 2008, 03:34 AM
- - Synthesize   QUOTE (Asi @ Jan 8 2008, 07:56 PM) not qu...   Jan 8 2008, 08:40 PM
|- - Asi   thanks I'll try that when I wakeup from my muc...   Jan 8 2008, 10:24 PM
- - Kinnison   Why I didn't notice this earlier XD It's ...   Jan 9 2008, 12:03 AM
- - SeeYouAlways   Are you using Postality Knight's (i.e. free) v...   Jan 16 2008, 04:17 AM
|- - bryandamage   QUOTE (SeeYouAlways @ Jan 16 2008, 03:24 ...   Jan 16 2008, 10:04 AM
- - SeeYouAlways   Sure. You have to buy it online. Go to http://www....   Jan 16 2008, 10:07 AM
- - ccoa   RPG Maker XP is sold as a download, so anyone anyw...   Jan 16 2008, 10:08 AM
|- - bryandamage   thank you both! although i try to restrain mys...   Jan 16 2008, 10:33 AM
- - SeeYouAlways   Good news is that you get a free 30 day trial with...   Jan 16 2008, 10:50 AM
- - bryandamage   K it will give me some time to learn some scripts ...   Jan 16 2008, 11:00 AM
- - SeeYouAlways   Well, let's not derail from the thread since i...   Jan 16 2008, 11:05 AM
- - ember2inferno   [Show/Hide] Obvious pro skillz: Emby did his home...   Jan 16 2008, 05:20 PM
- - ccoa   Great job playing around with it! That's ...   Jan 17 2008, 07:32 AM
- - bryandamage   i will buy the official version but i got around t...   Jan 18 2008, 10:02 AM
- - jamiez   Hey ccoa I know you're here!!! Why...   Jan 18 2008, 10:27 AM
- - SeeYouAlways   Err... Aren't her tutorials helping you enough...   Jan 18 2008, 10:31 AM
|- - jamiez   I'm Sorry ,but it's make me confuse cuz...   Feb 20 2008, 02:52 PM
- - animetor07   I am super new to scripting, I adjusted my codes t...   Jan 18 2008, 08:34 PM
- - ccoa   Your question is too vague to really be answered. ...   Jan 19 2008, 06:43 AM
- - Jadak   I get an error on line 9. CODE #----------------...   Jan 21 2008, 12:56 AM
- - jens009   Line 9? You're not suppose to have an error o...   Jan 21 2008, 02:14 AM
- - Kinnison   I think you got the arrow pointing the wrong way, ...   Jan 21 2008, 06:05 AM
- - Jadak   ah...that's probably why then. XD Thanks.   Jan 21 2008, 12:43 PM
- - darkkyros   Here's mine.   Jan 23 2008, 12:40 AM
- - S_n_a_k_e   Nice tutorial (Y) i have a question.. if you wou...   Feb 21 2008, 02:02 PM
- - Daldraeic   [Show/Hide] My first RGSS lesson. I tried to mak...   Feb 25 2008, 10:06 PM
- - ccoa   QUOTE (S_n_a_k_e @ Feb 21 2008, 02:09 PM)...   Feb 25 2008, 10:20 PM
- - Daldraeic   First Window... CODEclass Window_First < Window...   Feb 25 2008, 10:51 PM
- - ccoa   It's because you named the variables exactly t...   Feb 26 2008, 06:14 AM
|- - Daldraeic   I see. Thank you. Yeah lots of monday details with...   Feb 26 2008, 03:04 PM
|- - S_n_a_k_e   QUOTE Well, if you wanted to do it through events ...   Feb 27 2008, 10:07 AM
- - ccoa   That's planned on being part of the next lesso...   Feb 27 2008, 10:54 AM
- - S_n_a_k_e   Okaay, that sounds good, thanks ^^   Feb 27 2008, 12:46 PM
- - Someonation   Wait i have a question..... you said that CODEself...   Mar 5 2008, 08:01 AM
- - HeroOfHyla   What would I need to change in this to make it RGS...   Mar 8 2008, 10:46 PM
- - storymasterq   I think you need to call refresh from inside initi...   Mar 9 2008, 12:17 AM
|- - HeroOfHyla   QUOTE (storymasterq @ Mar 9 2008, 12:24 A...   Mar 9 2008, 09:03 AM
|- - Fieryarts   QUOTE (LOD @ Mar 17 2008, 09:34 AM) dammi...   Apr 11 2008, 04:17 PM
- - Tenchu-San   Okay, I've managed to tinker around with this ...   Jun 2 2008, 12:30 AM
- - woratana   You can create new window with Window_Base by call...   Jun 2 2008, 12:41 AM
- - Tenchu-San   When I try to use that (and put in values) it says...   Jun 2 2008, 12:51 AM
- - woratana   You may want to check my script called 'Charac...   Jun 2 2008, 02:52 PM
- - Tenchu-San   I'll definitely do that! Thanks a lot for ...   Jun 2 2008, 02:55 PM
- - duckdudetom   Thanks CCOA, could you make more tuts for RUBY, I ...   Jul 7 2008, 08:58 PM


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 19th June 2013 - 07:18 AM
RPG RPG Revolution is an Privacy Policy and Legal
eXTReMe Tracker