Submit Your Article


 
RPG Maker

Welcome Guest ( Log In | Register )


  Games Resources RPG Maker VX RPG Maker XP Scripts Tutorials Downloads

2 Pages V   1 2 >  
Reply to this topicStart new topic
> [Scripting][Series]RGSS: Windows - Lesson 2a: Adding A Window to a Scene
ccoa
post Jan 9 2008, 07:12 AM
Post #1


Storm Goddess
Group Icon

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




Okay, Lesson 2, part 1.

Adding A Window to a Scene
Part One: Scene_Map


This time, we're going to do something you might actually use one day. wink.gif We're going to add a small HUD to a minigame that keeps track of the player's HP and number of items collected.

This is the minigame I've created:



Little Cecelia dropped her evil uncle's coin collection down a drain in his mansion. Now she needs to find them all and pick them up before he finds out she lost them! Unfortunately, her twisted uncle has a dangerous maze underneath his house. Cecelia needs to navigate the maze in under the time limit and pick up all the coins without falling off a ledge. Falling down costs her 10 HP. Feel free to download the game I'm linking at the end of the lesson and check out the events.

So we're going to need a new window, aren't we? Let's open up the script editor, and insert a new one named Window_HUD.

CODE
class Window_HUD < Window_Base

end


So far, so good. You should recognize this from last lesson.

CODE
class Window_HUD < Window_Base
  def initialize
    super(0, 0, 110, 96)
    self.contents = Bitmap.new(width - 32, height - 32)
    
    # get the first actor in the party to display their data
    @actor = $game_party.actors[0]
    # current hp of actor
    @hp = @actor.hp
    # current number of coins
    @coins = $game_variables[1]
    
    refresh
  end
  
  # clear the window and redraw it
  def refresh
    self.contents.clear
    
    # draw the hp icon on the window
    bitmap = RPG::Cache.icon('hp')
    self.contents.blt(0, 4, bitmap, Rect.new(0, 0, 24, 24))
    
    # draw the actual HP of the actor
    self.contents.draw_text(32, 0, 96, 32, @hp.to_s)
    
    # draw the coin icon on the window
    bitmap = RPG::Cache.icon('coins')
    self.contents.blt(0, 36, bitmap, Rect.new(0, 0, 24, 24))
    
    # draw the variable holding the number of coins
    self.contents.draw_text(32, 32, 96, 32, @coins.to_s)
  end
end


A little bit more meat than our last window, no? Let's go over the new stuff.

CODE
    # get the first actor in the party to display their data
    @actor = $game_party.actors[0]


Remember when I said $ indicates a global variable? $game_party is a global variable that contains a Game_Party object. Among other things, Game_Party keeps an array (numbered list of objects) of all the actors in your party named actors. So $game_party.actors[0] gets the first person in the party. Arrays are 0-based, which means that the object at the first position is always at index 0. It takes some getting used to, I know.

The @ symbol indicates that the variable is an "instance variable." In plain English, that variable belongs to Window_HUD, and no one outside Window_HUD can see it. So our Window_HUD object will always know what actor it's supposed to be looking at.

CODE
    # current hp of actor
    @hp = @actor.hp


This is how much HP the lead actor has. If you look at the Game_Actor class:

CODE
class Game_Actor < Game_Battler


Game_Actor is decended from Game_Battler. And Game_Battler has the following:

CODE
  attr_reader   :hp                       # HP


Attr are a special way of allowing things outside the class to access the class's instance variables. This is the only way anyone else can read or write to them. There are three types: attr_reader (read only, can't change), attr_writer (write only, can't see it), and attr_accessor (read or write). So if we were to change our class above to:

CODE
class Window_HUD < Window_Base
  attr_accessor :actor

  def initialize
    super(0, 0, 110, 96)
    self.contents = Bitmap.new(width - 32, height - 32)
    
    # get the first actor in the party to display their data
    @actor = $game_party.actors[0]
    # current hp of actor
    @hp = @actor.hp
    # current number of coins
    @coins = $game_variables[1]
    
    refresh
  end
  
  # clear the window and redraw it
  def refresh
    self.contents.clear
    
    # draw the hp icon on the window
    bitmap = RPG::Cache.icon('hp')
    self.contents.blt(0, 4, bitmap, Rect.new(0, 0, 24, 24))
    
    # draw the actual HP of the actor
    self.contents.draw_text(32, 0, 96, 32, @hp.to_s)
    
    # draw the coin icon on the window
    bitmap = RPG::Cache.icon('coins')
    self.contents.blt(0, 36, bitmap, Rect.new(0, 0, 24, 24))
    
    # draw the variable holding the number of coins
    self.contents.draw_text(32, 32, 96, 32, @coins.to_s)
  end
end


Then somewhere outside of this class we could do this:

CODE
@hud_window = Window_HUD.new
@hud_window.actor = $game_party.actors[2]


Which would change the @actor instance variable to point to the third (remember, 0-based!) actor in the party.

All of that means to say we just got the actor's current hp and stored it in an instance variable named @hp.

CODE
    # current number of coins
    @coins = $game_variables[1]


Remember those variables you set in events? $game_variables stores them all. So $game_variables[1] is the same as Variable 0001 in the event editor. So we've just taken whatever number is in variable 1, and put it in the instance variable @coins.

CODE
    # draw the hp icon on the window
    bitmap = RPG::Cache.icon('hp')
    self.contents.blt(0, 4, bitmap, Rect.new(0, 0, 24, 24))


This is how you draw an image on the window. The first line gets the bitmap of the image. I've imported an icon named hp.png and coins.png in the game. RPG::Cache gets the image and caches it for easier loading, the icon() method tells it to get something from the icon folder. For a list of RPG::Cache's methods, go to the help file and go to RGSS Reference Manual -> Game Library -> RGSS Built-In Modules -> RPG::Cache. There's a method for each type of resource. You can even add methods to it, but that's a lesson for another day.

The blt() (block transfer) method draws a bitmap on another bitmap (in this case, the window's bitmap).

This is the help file's description of blt:

QUOTE
blt(x, y, src_bitmap, src_rect[, opacity])
Performs a block transfer from the src_bitmap box src_rect (Rect) to the specified bitmap coordinates (x, y).
opacity can be set from 0 to 255.


Not all that helpful, is it?

x and y are the coordinates on the bitmap (window) to draw the new bitmap on. 0, 0 would be the upper left corner. src_bitmap is the bitmap to draw, in this case, the icon we just loaded. src_rect is trickier. This tells the interpreter how much of the bitmap to draw. Since my icon is 24x24 pixels, I've told it to draw a rectangle that starts at 0, 0 (upper left) and is 24 pixels wide and 24 pixels tall. In other words, the whole icon. However, I could just as easily done this:

CODE
Rect.new(12, 12, 12, 12)


Which would only draw the lower right 12x12 pixels, or this:

CODE
Rect.new(0, 0, 24, 1)


Which would only draw the very top row of 24 pixels.

Not that useful with icons, but this is how you can show just one frame of a characterset, among other uses. We may cover this later.

Opacity is optional. If you don't enter anything, as we have, it's assumed to be 255, or completely opaque. You can choose to enter a number between 0 (completely transparent, or invisible) and 255 if you wish. It could look like this:

CODE
    self.contents.blt(0, 4, bitmap, Rect.new(0, 0, 24, 24), 160)


CODE
    # draw the actual HP of the actor
    self.contents.draw_text(32, 0, 96, 32, @hp.to_s)


This is similar to what we did before, drawing some text on the screen. The only difference is that we're trying to draw a number, so we have to convert it to a string, first. the to_s() method takes a numerical object, like 50, and turns it into a string, '50'.

Okay, so now we have a basic window. Let's add it to the scene. Open up the script editor and click on Scene_Map. Find this line:

CODE
    # Make message window
    @message_window = Window_Message.new


Right after it, add:

CODE
    @hud_window = Window_HUD.new


We've now created a new window. But don't forget, we need to dispose it eventually! In the case of scenes, windows are disposed when the scene changes from one to another. You'll usually find them shortly below the main loop. Look for this line:

CODE
    @message_window.dispose


and add below it:

CODE
    @hud_window.dispose


Now we have a window that shows up directly on the map and is automatically disposed whenever the player enters the menu, battle, or any other scene. But wait! It doesn't do anything! No matter how the player's hp changes or how many coins he picks up, the HUD stays static. That can't be right.

We're going to add a method called update(). Update is a method intended to be called every frame.

CODE
class Window_HUD < Window_Base
  def initialize
    super(0, 0, 110, 96)
    self.contents = Bitmap.new(width - 32, height - 32)
    
    # get the first actor in the party to display their data
    @actor = $game_party.actors[0]
    # current hp of actor
    @hp = @actor.hp
    # current number of coins
    @coins = $game_variables[1]
    
    refresh
  end
  
  # clear the window and redraw it
  def refresh
    self.contents.clear
    
    # draw the hp icon on the window
    bitmap = RPG::Cache.icon('hp')
    self.contents.blt(0, 4, bitmap, Rect.new(0, 0, 24, 24))
    
    # draw the actual HP of the actor
    self.contents.draw_text(32, 0, 96, 32, @hp.to_s)
    
    # draw the coin icon on the window
    bitmap = RPG::Cache.icon('coins')
    self.contents.blt(0, 36, bitmap, Rect.new(0, 0, 24, 24))
    
    # draw the variable holding the number of coins
    self.contents.draw_text(32, 32, 96, 32, @coins.to_s)
  end
  
  # run each and every frame
  def update    
    # only refresh if something has changed.  Prevents lag.
    if (@hp != @actor.hp) or (@coins != $game_variables[1])
      # current hp of actor
      @hp = @actor.hp
      # current number of coins
      @coins = $game_variables[1]
      
      refresh
    end
    
    # do anything the parent windows need to do each frame
    super
  end
end


Let's take a closer look at this new method:

CODE
    # only refresh if something has changed.  Prevents lag.
    if (@hp != @actor.hp) or (@coins != $game_variables[1])


This is a conditional branch (yes, like the ones in the event editor). Like the ones in the event editor, it tests to see if something is true, and does something inside it if it is. The != means "not equal", so what this is saying is if the hp we stored isn't equal to the current hp or the number of coins we have stored isn't equal to the actual number we have picked up then do something. We could do without this and just refresh every frame, but that's a sure-fire way to cause lag. Only refresh your window if something on it changed.

CODE
# current hp of actor
      @hp = @actor.hp
      # current number of coins
      @coins = $game_variables[1]

      refresh


We reset our instance variables to the true current values in preparation to redraw them, then refresh.

CODE
    # do anything the parent windows need to do each frame
    super


Like the super in initialize, this calls the same method in the parent class. In this case, it's calling update in Window_Base.

Now we have an update method, but we need to set it up so it's called every frame. Head back to Scene_Map, and look for this line:

CODE
    @message_window.update


and add this below it:

CODE
    @hud_window.update


Now we almost have it, we just need a few refinements. First, we only want the window to show up during the minigame. Second, we want the window to be semi-transparent so the player can see what's behind it.

CODE
class Window_HUD < Window_Base
  def initialize
    super(0, 0, 110, 96)
    self.contents = Bitmap.new(width - 32, height - 32)
    
    # don't show this window by default
    self.visible = false
    
    # make the window semi-transparent so the player can see what's behind it
    self.opacity = 160
    
    # get the first actor in the party to display their data
    @actor = $game_party.actors[0]
    # current hp of actor
    @hp = @actor.hp
    # current number of coins
    @coins = $game_variables[1]
    
    refresh
  end
  
  # clear the window and redraw it
  def refresh
    self.contents.clear
    
    # draw the hp icon on the window
    bitmap = RPG::Cache.icon('hp')
    self.contents.blt(0, 4, bitmap, Rect.new(0, 0, 24, 24))
    
    # draw the actual HP of the actor
    self.contents.draw_text(32, 0, 96, 32, @hp.to_s)
    
    # draw the coin icon on the window
    bitmap = RPG::Cache.icon('coins')
    self.contents.blt(0, 36, bitmap, Rect.new(0, 0, 24, 24))
    
    # draw the variable holding the number of coins
    self.contents.draw_text(32, 32, 96, 32, @coins.to_s)
  end
  
  # run each and every frame
  def update
    # we're only visible if this switch is on
    self.visible = $game_switches[1]
    
    # don't update if we can't see the window.  Prevents lag.
    if !self.visible
      return
    end
    
    # only refresh if something has changed.  Prevents lag.
    if (@hp != @actor.hp) or (@coins != $game_variables[1])
      # current hp of actor
      @hp = @actor.hp
      # current number of coins
      @coins = $game_variables[1]
      
      refresh
    end
    
    # do anything the parent windows need to do each frame
    super
  end
end


Let's look at the changes.

CODE
    # don't show this window by default
    self.visible = false


This "hides" the window. The method visible is actually not in Window_Base, but in Window_Base's parent class, Window. Window is a hidden class, but you can find a list of its methods in the Help file under RGSS Built-In Classes. Since Window_HUD is descended from Window, it can do anything Window can do.

CODE
    # make the window semi-transparent so the player can see what's behind it
    self.opacity = 160


This makes the window semi-transparent. Feel free to play with transparencies until you get a feel for what's too transparent and what's not.

CODE
    # we're only visible if this switch is on
    self.visible = $game_switches[1]
    
    # don't update if we can't see the window.  Prevents lag.
    if !self.visible
      return
    end


Like $game_variables, $game_switches keeps all of the switches you use in the event editor. Switches are "booleans", which means they're either true or false (corresponding to on or off). The first line sets the visibility to the first switch (which I turn on when the minigame begins). In other words, if switch 1 is on, the window is visible. If it's off, it's not.

The next line is another lag prevention technique. The ! means not, so this says: If I'm not visible, then return. Return is a special keyword that tells the interpreter to exit the current method without looking at any of the code below it. You can also return a value, which we'll probably get into later. For now, all you really need to know is that this will exit the method without checking the number of coins or hp, saving on calculations.

Homework

Make a simple HUD that shows HP, SP, and gold. Or any other 2-3 values you choose. You can use plain text or icons. Hint: get the party's current gold via $game_party.gold.

Demo So Far

http://www.pcis-studios.com/ccoa/Window%20Demo.exe


__________________________
Go to the top of the page
 
+Quote Post
   
SeeYouAlways
post Jan 9 2008, 07:46 AM
Post #2


Demented Moogle
Group Icon

Group: Banned
Posts: 1,130
Type: None
RM Skill: Undisclosed




Hmm... Oh my. Wonderful! Another lesson! Yay!

Personally I think this deserves a new thread, but it's up to you. biggrin.gif


__________________________
Go to the top of the page
 
+Quote Post
   
Kinnison
post Jan 9 2008, 08:11 AM
Post #3


The not-so-revolutionary guy
Group Icon

Group: Revolutionary
Posts: 162
Type: Event Designer
RM Skill: Masterful




Cool, an update ohmy.gif

Yeah, I was wondering is it possible for me to put in a variable number and step count into the window and show it on the map?


__________________________
[Show/Hide] Current Game Project:

[Show/Hide] On Hold:
Go to the top of the page
 
+Quote Post
   
ccoa
post Jan 9 2008, 08:35 AM
Post #4


Storm Goddess
Group Icon

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




Certainly is.

I already showed you how to display a variable (read the lesson carefully!). You can access step count via $game_party.steps. Remember to convert it to a String before you show it!

If you want me to put them in seperate threads, that's fine with me. It will keep the homework and questions more organized. Could you do me a favor and split this one for me? I'll be sure to post the next one seperately.

EDIT: Thanks, SYA. happy.gif


__________________________
Go to the top of the page
 
+Quote Post
   
PK8
post Jan 9 2008, 01:37 PM
Post #5


I walk the long road to redemption.
Group Icon

Group: Local Mod
Posts: 179
Type: Scripter
RM Skill: Advanced
Rev Points: 40




[Show/Hide] old hud
Booyah! I made a nice hud. It shows the health, max health, SP, max SP, experience points, next experience points, character graphic and current level of the actor.

I also made it a bit customizable too. Users can easily edit the ID of the switch that activates and deactivates the hud. Users can also easily edit the variable ID of the hud. The value of the variable ID which uses the hud can also be adjusted from a range of 0 to 3. 0 will display the stats and graphic of the first party member in the hud, 3 will display the fourth party member in the hud.

And now, here's the code.

I called it "Window_HUD" and placed it underneath the other window classes.
CODE
#---------------------------------------------------------------------
# Hud by Punkid89
# Thanks to ccoa, Raziel and Trickster.
#---------------------------------------------------------------------
# Author's notes and comments
# .: Very first script. I'll have to credit ccoa, Raz and Trickster.
# .: Trickster for helping me with telling me the name of maxhp/maxsp
# .: Raz for teaching me how to draw a graphic of the party member
# .: It's customizable. Or so I think. happy.gif
#---------------------------------------------------------------------
class Window_HUD < Window_Base
  attr_accessor :actor
  def initialize
    super (0, 0, 164, 116)
    self.contents = Bitmap.new(width - 32, height - 32)
#------------------------------------------------------------------------------
    # Customize!
#------------------------------------------------------------------------------    
    self.visible = false # Show this window by default
    self.opacity = 160 # Adjust opacity of the window
    @hudswitch = 1 # Switch ID for activating/deactivating HUD
    @hudvariable = 1 # Variable ID for your HUD
    @hudsystem = $game_variables[@hudvariable]
#------------------------------------------------------------------------------
    # Gets first actor in party.
#------------------------------------------------------------------------------
    @actor = $game_party.actors[@hudsystem]
    # Gets actor's hp and sp.
    @hp = @actor.hp
    @sp = @actor.sp
    # Gets actor's max hp and sp.
    @maxhp = @actor.maxhp
    @maxsp = @actor.maxsp
    # Gets actor's exp.
    @exp = @actor.exp
    @name = @actor.name
    @level = @actor.level
    refresh
  end
#------------------------------------------------------------------------------
  # clears window and redraws it.
#------------------------------------------------------------------------------  
  def refresh
    self.contents.clear
    self.contents.font.size = 16
    # Draw HP and MaxHP of the party actor.
    self.contents.draw_text(36, 8, 200, 32, "HP: " + @hp.to_s + "/" + @maxhp.to_s)
    # Draw SP and MaxSP of the party actor.
    self.contents.draw_text(36, 24, 200, 32, "SP: " + @sp.to_s + "/" + @maxsp.to_s)
    # Draw EXP and Next EXP of the party actor.
    self.contents.draw_text(36, 40, 200, 32, "EXP: " + @exp.to_s + "/" + $game_party.actors[@hudsystem].next_exp_s)
    # Draw level of the party actor.
    self.contents.draw_text(36, 56, 200, 32, "Level: " + @level.to_s)
    # Draw party actor name
    self.contents.draw_text(36, -8, 200, 32, @name)
    # Draw party actor character graphic
    draw_actor_graphic($game_party.actors[@hudsystem], 12, 64)
  end
  def update
    # we're only visible if this switch is on
    self.visible = $game_switches[@hudswitch]
    # don't update if we can't see the window.  Prevents lag.
    if !self.visible
      return
    end
    # only refresh if something has changed.  Prevents lag.
    if (@hp != @actor.hp) or (@sp != actor.sp) or (@exp != actor.exp) or (@maxhp != actor.maxhp) or (@maxsp != actor.maxsp)
      # current HP and SP of actor
      @hp = @actor.hp
      @sp = @actor.sp
      # Current MaxHP and MaxSP of Actor
      @maxhp = @actor.maxhp
      @maxsp = @actor.maxsp
      # Current EXP
      @exp = @actor.exp
      refresh
    end
    # do anything the parent windows need to do each frame
    super
  end
end


Go to the Scene Map class.

Find:
CODE
@message_window = Window_Message.new

Add below:
CODE
@hud_window = Window_HUD.new


Find:
CODE
@message_window.dispose

Add below:
CODE
@hud_window.dispose


Find:
CODE
@message_window.update

Add below:
CODE
@hud_window.update


It should look like this. (Look in the top left)


Edit: I'm proud of this. happy.gif
Edit2: Oops, this script has a bit of a boo-boo. I'll fix it in a jiffy.


Intro
I modified my hud to make it a little better. It shows the Max HP, Max SP, HP, SP, EXP, Next EXP, Level, Name, Character Graphic and Gold of one actor in your party.

Features
Customizable.
.: Users can easily edit the switch ID that activates/deactivates the hud.
.: Users can also edit the variable ID that the hud will use.
.: The value of the variable ID which uses the hud could be adjusted from a range of 0 to 3. (0 displays first party member, 1 displays second, 2 displays third, 3 displays the last party member)
.: Font size can also be edited without much trouble.

Screenshots
(look in top left corner)


Scripts
Create a new class (is that what it's called?) under the other window scripts. Call it "Window_HUD" then copy and paste the code below.
CODE
#---------------------------------------------------------------------
# Hud by Punkid89
# Thanks to ccoa, Raziel and Trickster.
#---------------------------------------------------------------------
# Author's notes and comments
# .: Very first script. I'll have to credit ccoa, Raz and Trickster.
# .: Trickster for helping me with telling me the name of maxhp/maxsp
# .: Raz for teaching me how to draw a graphic of the party member
# .: It's customizable. Or so I think.
#---------------------------------------------------------------------
class Window_HUD < Window_Base
  attr_accessor :actor
  def initialize
    super (0, 0, 220, 132)
    self.contents = Bitmap.new(width - 32, height - 32)
#------------------------------------------------------------------------------
    # Customize!
#------------------------------------------------------------------------------    
    self.visible = false # Show this window by default
    self.opacity = 160 # Adjust opacity of the window
    @hudswitch = 1 # Switch ID for activating/deactivating HUD
    @hudvariable = 1 # Variable ID for your HUD
    @hudfontsize = 14 # Font Size of HUD
    @hudsystem = $game_variables[@hudvariable]
#------------------------------------------------------------------------------
    # Gets first actor in party.
#------------------------------------------------------------------------------
    @actor = $game_party.actors[@hudsystem]
    # Gets actor's hp and sp.
    @hp = @actor.hp
    @sp = @actor.sp
    # Gets actor's max hp and sp.
    @maxhp = @actor.maxhp
    @maxsp = @actor.maxsp
    # Gets actor's exp, exp that (s)he needs to reach and level.
    @exp = @actor.exp
    @nexp = $game_party.actors[@hudsystem].next_exp_s
    @level = @actor.level
    # Gets actor's name.
    @name = @actor.name
    # Gets the amount of gold the party possesses.
    @gold = $game_party.gold
    # Gets actor's graphic
    @actorgraphic = $game_party.actors[@hudsystem]
    refresh
  end
#------------------------------------------------------------------------------
  # clears window and redraws it.
#------------------------------------------------------------------------------  
  def refresh
    self.contents.clear
    self.contents.font.size = @hudfontsize
    # Draw HP and MaxHP of the party actor.
    self.contents.draw_text(36, 8, 200, 32, "HP: " + @hp.to_s + "/" + @maxhp.to_s)
    # Draw SP and MaxSP of the party actor.
    self.contents.draw_text(36, 24, 200, 32, "SP: " + @sp.to_s + "/" + @maxsp.to_s)
    # Draw EXP and Next EXP of the party actor.
    self.contents.draw_text(36, 40, 200, 32, "EXP: " + @exp.to_s + "/" + @nexp)
    # Draw level of the party actor.
    self.contents.draw_text(36, 56, 200, 32, "Level: " + @level.to_s)
    # Draw party gold.
    self.contents.draw_text(36, 72, 200, 32, "Gold: " + @gold.to_s)
    # Draw party actor name
    self.contents.draw_text(36, -8, 200, 32, @name)
    # Draw party actor character graphic
    draw_actor_graphic(@actorgraphic, 12, 64)
  end
  def update
    # we're only visible if this switch is on
    self.visible = $game_switches[@hudswitch]
    # don't update if we can't see the window.  Prevents lag.
    if !self.visible
      return
    end
    # only refresh if something has changed. (I added way too many ors)
    if (@hp != @actor.hp) or (@sp != actor.sp) or (@exp != actor.exp) or (@nexp != @nexp = $game_party.actors[@hudsystem].next_exp_s) or (@maxhp != actor.maxhp) or (@maxsp != actor.maxsp) or (@gold = $game_party.gold)
      # current HP and SP of actor
      @hp = @actor.hp
      @sp = @actor.sp
      # Current MaxHP and MaxSP of Actor
      @maxhp = @actor.maxhp
      @maxsp = @actor.maxsp
      # Current EXP, Next EXP and Level
      @exp = @actor.exp
      @nexp = $game_party.actors[@hudsystem].next_exp_s
      @level = @actor.level
      # Gets actor's name.
      @name = @actor.name
      # Gets the amount of gold the party possesses.
      @gold = $game_party.gold
      # Gets actor's graphic.
      @actorgraphic = $game_party.actors[@hudsystem]
      refresh
    end
    # do anything the parent windows need to do each frame
    super
  end
end


Go to Scene_Map and do the following.
Find:
CODE
@message_window = Window_Message.new

Add below:
CODE
@hud_window = Window_HUD.new


Find:
CODE
@message_window.dispose

Add below:
CODE
@hud_window.dispose


Find:
CODE
@message_window.update

Add below:
CODE
@hud_window.update


Instructions
The instructions of the script are pretty easy to follow I believe.

Credits and thanks
ccoa: For this awesome lesson.
Raziel: Teaching me how to draw the graphic of a party actor and drawing "next experience points" text.
Trickster: For telling me what's max hp (maxhp) and max sp (maxsp) in scripting.

This post has been edited by Punk: Jan 9 2008, 08:30 PM


__________________________
Go to the top of the page
 
+Quote Post
   
ccoa
post Jan 9 2008, 02:11 PM
Post #6


Storm Goddess
Group Icon

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




Sweet, Punk. If there were extra credit, surely you'd get some. happy.gif


__________________________
Go to the top of the page
 
+Quote Post
   
PK8
post Jan 9 2008, 08:32 PM
Post #7


I walk the long road to redemption.
Group Icon

Group: Local Mod
Posts: 179
Type: Scripter
RM Skill: Advanced
Rev Points: 40




I just modified my little HUD and thanks ccoa.


__________________________
Go to the top of the page
 
+Quote Post
   
Dark Dragon
post Jan 9 2008, 09:06 PM
Post #8


Level 8
Group Icon

Group: Revolutionary
Posts: 116
Type: Event Designer
RM Skill: Skilled




Ah I thought ccoa quit RMXP some time ago D= I'm glad I was wrong =D

Can you explain this function a bit more please ?
QUOTE
self.contents.blt(0, 4, bitmap, Rect.new(0, 0, 24, 24))

I see it several times in scripts made by other people. I try to understand what it means in the help file, but it doesn't seem to be any good >_>'

Edit : Never mind. looks like I missed the part about what I was asking happy.gif"
Great job though !

This post has been edited by Dark Dragon: Jan 9 2008, 09:09 PM


__________________________
وَلَقَدْ يَسَّرْنَا الْقُرْآنَ لِلذِّكْرِ فَهَلْ مِن مُّدَّكِرٍ



I have noticed that kewlness is mainly based on the behaviour of the kewl person, it doesnt matter wether u kewl or u nt kewl lol omg
I smell n00by, but is me ?
Go to the top of the page
 
+Quote Post
   
Kinnison
post Jan 9 2008, 11:07 PM
Post #9


The not-so-revolutionary guy
Group Icon

Group: Revolutionary
Posts: 162
Type: Event Designer
RM Skill: Masterful




Whoops, sorry. I did read the step-by-step method. I asked a question where the answer is already there tongue.gif

I try it out right away.

EDIT:
Hmm, the mount of HP doesn't go down when sustaining damage on the map. It only update when I open the menu and closes it. What did I forget to insert? sad.gif

This post has been edited by Kinnison: Jan 9 2008, 11:38 PM


__________________________
[Show/Hide] Current Game Project:

[Show/Hide] On Hold:
Go to the top of the page
 
+Quote Post
   
ccoa
post Jan 10 2008, 05:20 AM
Post #10


Storm Goddess
Group Icon

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




You probably forgot to add the update to Scene_Map.


__________________________
Go to the top of the page
 
+Quote Post
   
Guest_Black Shadow_*
post Jan 11 2008, 04:07 AM
Post #11





Guests





This is going to be so helpfull when Im working on my Bar Minigame.

A question though.

If I want the window to show the amount of gold you earn when you sell drinks in the bar, and the amount of money you need to earn to clear the minigame, should it be the same layout as this explanation in the lesson , but I remove these lines?

CODE
# get the first actor in the party to display their data
    @actor = $game_party.actors[0]
    # current hp of actor
    @hp = @actor.hp
    # current number of coins
    @coins = $game_variables[1]
    
    refresh
  end
  
  # clear the window and redraw it
  def refresh
    self.contents.clear
    
    # draw the hp icon on the window
    bitmap = RPG::Cache.icon('hp')
    self.contents.blt(0, 4, bitmap, Rect.new(0, 0, 24, 24))
    
    # draw the actual HP of the actor
    self.contents.draw_text(32, 0, 96, 32, @hp.to_s)
Go to the top of the page
 
+Quote Post
   
Kinnison
post Jan 11 2008, 06:43 AM
Post #12


The not-so-revolutionary guy
Group Icon

Group: Revolutionary
Posts: 162
Type: Event Designer
RM Skill: Masterful




Is it this one?

CODE
@hud_window.update


I did put it into Scene_Map

CODE
  #--------------------------------------------------------------------------
  # * Frame Update
  #--------------------------------------------------------------------------
  def update
    # Loop
    loop do
      # Update map, interpreter, and player order
      # (this update order is important for when conditions are fulfilled
      # to run any event, and the player isn't provided the opportunity to
      # move in an instant)
      $game_map.update
      $game_system.map_interpreter.update
      $game_player.update
      # Update system (timer), screen
      $game_system.update
      $game_screen.update
      # Abort loop if player isn't place moving
      unless $game_temp.player_transferring
        break
      end
      # Run place move
      transfer_player
      # Abort loop if transition processing
      if $game_temp.transition_processing
        break
      end
    end
    # Update sprite set
    @spriteset.update
    # Update message window
    @message_window.update
    # Update HUD window
    @hud_window.update #<<<<<<<<I added it here
    # If game over
    if $game_temp.gameover
      # Switch to game over screen
      $scene = Scene_Gameover.new
      return
    end


or did I placed it at the wrong spot? sad.gif


__________________________
[Show/Hide] Current Game Project:

[Show/Hide] On Hold:
Go to the top of the page
 
+Quote Post
   
ccoa
post Jan 11 2008, 07:39 AM
Post #13


Storm Goddess
Group Icon

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




QUOTE (Black Shadow @ Jan 11 2008, 04:14 AM) *
This is going to be so helpfull when Im working on my Bar Minigame.

A question though.

If I want the window to show the amount of gold you earn when you sell drinks in the bar, and the amount of money you need to earn to clear the minigame, should it be the same layout as this explanation in the lesson , but I remove these lines?


What you'd do is change the names and values of the instance variables. One would be set to amount of gold earned (I'm assuming this is a variable, and not the party total gold) and one to a constant amount. Like so:

CODE
  @gold_earned = $game_variables[1]
  @amount_needed = 500


This is assuming your variable is variable 0001 and the amount you need to earn is 500 gold.

Then to display them:


CODE
    self.contents.draw_text(32, 0, 96, 32, @gold_earned.to_s)
    self.contents.draw_text(32, 32, 96, 32, @amount_needed.to_s)


Does that make sense?


QUOTE (Kinnison @ Jan 11 2008, 06:50 AM) *
Is it this one?

CODE
@hud_window.update


...

or did I placed it at the wrong spot? sad.gif


No, looks right. The error must be with your update method itself. Can you show it to me?


__________________________
Go to the top of the page
 
+Quote Post
   
Kinnison
post Jan 11 2008, 07:58 AM
Post #14


The not-so-revolutionary guy
Group Icon

Group: Revolutionary
Posts: 162
Type: Event Designer
RM Skill: Masterful




Here's the script I made.

CODE
class Window_HUD < Window_Base
  attr_accessor :actor

  def initialize
    super(0, 0, 185, 185)
    self.contents = Bitmap.new(width - 32, height - 32)
    
    # make the window semi-transparent so the player can see what's behind it
    self.opacity = 255
    
    # get the first actor in the party to display their data
    @actor = $game_party.actors[0]
    #current level of actor
    @level = @actor.level
    # current hp of actor
    @hp = @actor.hp
    # current mp of actor
    @sp = @actor.sp
    # current max hp of actor
    @maxhp = @actor.maxhp
    # current max hp of actor
    @maxsp = @actor.maxsp
    # current number of coins
    @coins = $game_variables[1]
    refresh
  end
  
  # clear the window and redraw it
  def refresh
    self.contents.clear
    
    # Draw level of the party actor.
    self.contents.draw_text(0, 0, 200, 32, "Lv: " + @level.to_s)
    
    # draw the hp icon on the window
    bitmap = RPG::Cache.icon('HP')
    self.contents.blt(0, 32, bitmap, Rect.new(0, 0, 24, 24))
        
    # draw the actual HP of the actor
    self.contents.draw_text(45, 32, 128, 32, @hp.to_s + " / " + @maxhp.to_s)
    
    # draw the coin icon on the window
    bitmap = RPG::Cache.icon('MP')
    self.contents.blt(0, 64, bitmap, Rect.new(0, 0, 24, 24))
    
    # draw the actual MP of the actor
    self.contents.draw_text(45, 64, 128, 32, @sp.to_s + " / " + @maxsp.to_s)
    
    # draw the coin icon on the window
    bitmap = RPG::Cache.icon('Coin')
    self.contents.blt(0, 96, bitmap, Rect.new(0, 0, 24, 24))
    
    # draw the variable holding the number of coins
    self.contents.draw_text(45, 96, 128, 32, @coins.to_s + " " + $data_system.words.gold)
    
    # draw location of the player happy.gif
    self.contents.draw_text(0, 120, 640, 32, $data_map_infos[$game_map.map_id].name)
  end
end
  
  # run each and every frame
  def update
    # we're only visible if this switch is on
    self.visible = $game_switches[1]
    
    # don't update if we can't see the window.  Prevents lag.
    if !self.visible
      return
    end
  end
  # run each and every frame
  def update    
    # only refresh if something has changed.  Prevents lag.
    if (@hp != @actor.hp) or (@sp != @actor.sp) or (@coins != $game_variables[1]) or ($data_map_infos != $data_map_infos[$game_map.map_id].name)
      # current level of actor
      @level = @actor.level
      # current hp of actor
      @hp = @actor.hp
      # current number of coins
      @sp = @actor.sp
      # current number of coins
      @coins = $game_variables[1]
      #current location of player
      refresh
    end
    
    # do anything the parent windows need to do each frame
    super
  end


I added the three other statements in Scene_Map
Both Window_HUD.new and hud_window.dispose works just fine but not hud_window.update. Did I forgot to add anything else in the coding above?


__________________________
[Show/Hide] Current Game Project:

[Show/Hide] On Hold:
Go to the top of the page
 
+Quote Post
   
ccoa
post Jan 11 2008, 08:47 AM
Post #15


Storm Goddess
Group Icon

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




Well, at a glance you defined the update method twice. If you define a method twice, the later definition is used and the old one is ignored.

Take out the second instance of this:

CODE
  end
  # run each and every frame
  def update


I tried out your code and it works just fine. Are you trying to add this to a game that is already using scripts? If you are, you may have a script conflict. Try doing a global search (Shift+Ctrl+F) for "class Scene_Map." See if there's another script that overwriting Scene_Map's update method.


__________________________
Go to the top of the page
 
+Quote Post
   
Kinnison
post Jan 11 2008, 09:56 AM
Post #16


The not-so-revolutionary guy
Group Icon

Group: Revolutionary
Posts: 162
Type: Event Designer
RM Skill: Masterful




Oh, okay.
It's working fine now, thanks.


__________________________
[Show/Hide] Current Game Project:

[Show/Hide] On Hold:
Go to the top of the page
 
+Quote Post
   
Heavyblues
post Jan 18 2008, 01:54 PM
Post #17


Level 11
Group Icon

Group: Revolutionary
Posts: 188
Type: Artist
RM Skill: Beginner




I just typed up the entire thing, but it's giving me a name error in system_map

Script 'Scene_Map' Line 17: Name Error Occurred.
uninitialized constant Scene_Map::Window_HUD

17 is where I added the @hud_window.new or something like that [it IS typed properly, and nothing LOOKS wrong >.>] can you help ;.;

oh, and it gives me this error when I select new game, not at the main screen
Go to the top of the page
 
+Quote Post
   
ccoa
post Jan 18 2008, 02:02 PM
Post #18


Storm Goddess
Group Icon

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




I really need to see the actual code to be of any help. :/


__________________________
Go to the top of the page
 
+Quote Post
   
Heavyblues
post Jan 19 2008, 05:15 PM
Post #19


Level 11
Group Icon

Group: Revolutionary
Posts: 188
Type: Artist
RM Skill: Beginner




QUOTE (ccoa @ Jan 18 2008, 01:09 PM) *
I really need to see the actual code to be of any help. :/

[Show/Hide] Window_HUD
CODE
class Window_Hud < Window_Base
attr_accessor :actor

def initialize
super(0, 0, 110, 96)
self.contents = Bitmap.new(height - 32, width - 32)

#Don't show this window by default
self.visible = false

#sets the window's transparency
self.opacity = 160

#Gets the first actor's data and displays it
@actor = $Game_Party.actors[0]
#actor's current HP
@hp = @actor.hp
#number of gems collected
@coins = Game_Variables[1]

refresh
end

#clears the window and redraws it
def refresh
self.contents.clear

#draws the HP icon
bitmap = RPG::cache.icon('hp')
self.contents.blt(0, 4, bitmap, Rect.new(0, 0, 24, 24))

#Draw the actor's HP
self.contents.draw_text(32, 0, 96, 32, @hp.to_s)

#Draw the coin Icon
bitmap = RPG::cache.icon('coins')
self.contents.blt(0, 32, bitmap, Rect.new(0, 0, 24, 24))

#Draw the variable containing the number of coins
self.contents.draw_text(32, 32, 96, 32, @coins.to_s)
end
#Run this each and every frame
def update
#we're only visible if something has changed [to prevent lag]
self.visible = $game_switches[1]

#don't update if we can't see the window
if !self.visible
return
end

#only refresh if something ahs changed
if (@hp != actor.hp) or (@coins != $game.variables[1])
#current HP of actor
@hp = @actor.hp
#current number of coins
@coins = $game.variables[1]

refresh
end

#Do anything the parent windows need, to each frame
super
end
end


[Show/Hide] Scene_Map
CODE
#===================================================
=============
==============
# ** Scene_Map
#------------------------------------------------------------------------------
# This class performs map screen processing.
#==============================================================================

class Scene_Map
#--------------------------------------------------------------------------
# * Main Processing
#--------------------------------------------------------------------------
def main
# Make sprite set
@spriteset = Spriteset_Map.new
# Make message window
@message_window = Window_Message.new
#Opens the HUD
@hud_window = Window_HUD.new
# Transition run
Graphics.transition
# Main loop
loop do
# Update game screen
Graphics.update
# Update input information
Input.update
# Frame update
update
# Abort loop if screen is changed
if $scene != self
break
end
end
# Prepare for transition
Graphics.freeze
# Dispose of sprite set
@spriteset.dispose
# Dispose of message window
@message_window.dispose
@hud_window.dispose
# If switching to title screen
if $scene.is_a?(Scene_Title)
# Fade out screen
Graphics.transition
Graphics.freeze
end
end
#--------------------------------------------------------------------------
# * Frame Update
#--------------------------------------------------------------------------
def update
# Loop
loop do
# Update map, interpreter, and player order
# (this update order is important for when conditions are fulfilled
# to run any event, and the player isn't provided the opportunity to
# move in an instant)
$game_map.update
$game_system.map_interpreter.update
$game_player.update
# Update system (timer), screen
$game_system.update
$game_screen.update
# Abort loop if player isn't place moving
unless $game_temp.player_transferring
break
end
# Run place move
transfer_player
# Abort loop if transition processing
if $game_temp.transition_processing
break
end
end
# Update sprite set
@spriteset.update
# Update message window
@message_window.update
@hud_window.update
# If game over
if $game_temp.gameover
# Switch to game over screen
$scene = Scene_Gameover.new
return
end
# If returning to title screen
if $game_temp.to_title
# Change to title screen
$scene = Scene_Title.new
return
end
# If transition processing
if $game_temp.transition_processing
# Clear transition processing flag
$game_temp.transition_processing = false
# Execute transition
if $game_temp.transition_name == ""
Graphics.transition(20)
else
Graphics.transition(40, "Graphics/Transitions/" +
$game_temp.transition_name)
end
end
# If showing message window
if $game_temp.message_window_showing
return
end
# If encounter list isn't empty, and encounter count is 0
if $game_player.encounter_count == 0 and $game_map.encounter_list != []
# If event is running or encounter is not forbidden
unless $game_system.map_interpreter.running? or
$game_system.encounter_disabled
# Confirm troop
n = rand($game_map.encounter_list.size)
troop_id = $game_map.encounter_list[n]
# If troop is valid
if $data_troops[troop_id] != nil
# Set battle calling flag
$game_temp.battle_calling = true
$game_temp.battle_troop_id = troop_id
$game_temp.battle_can_escape = true
$game_temp.battle_can_lose = false
$game_temp.battle_proc = nil
end
end
end
# If B button was pressed
if Input.trigger?(Input::cool.gif
# If event is running, or menu is not forbidden
unless $game_system.map_interpreter.running? or
$game_system.menu_disabled
# Set menu calling flag or beep flag
$game_temp.menu_calling = true
$game_temp.menu_beep = true
end
end
# If debug mode is ON and F9 key was pressed
if $DEBUG and Input.press?(Input::F9)
# Set debug calling flag
$game_temp.debug_calling = true
end
# If player is not moving
unless $game_player.moving?
# Run calling of each screen
if $game_temp.battle_calling
call_battle
elsif $game_temp.shop_calling
call_shop
elsif $game_temp.name_calling
call_name
elsif $game_temp.menu_calling
call_menu
elsif $game_temp.save_calling
call_save
elsif $game_temp.debug_calling
call_debug
end
end
end
#--------------------------------------------------------------------------
# * Battle Call
#--------------------------------------------------------------------------
def call_battle
# Clear battle calling flag
$game_temp.battle_calling = false
# Clear menu calling flag
$game_temp.menu_calling = false
$game_temp.menu_beep = false
# Make encounter count
$game_player.make_encounter_count
# Memorize map BGM and stop BGM
$game_temp.map_bgm = $game_system.playing_bgm
$game_system.bgm_stop
# Play battle start SE
$game_system.se_play($data_system.battle_start_se)
# Play battle BGM
$game_system.bgm_play($game_system.battle_bgm)
# Straighten player position
$game_player.straighten
# Switch to battle screen
$scene = Scene_Battle.new
end
#--------------------------------------------------------------------------
# * Shop Call
#--------------------------------------------------------------------------
def call_shop
# Clear shop call flag
$game_temp.shop_calling = false
# Straighten player position
$game_player.straighten
# Switch to shop screen
$scene = Scene_Shop.new
end
#--------------------------------------------------------------------------
# * Name Input Call
#--------------------------------------------------------------------------
def call_name
# Clear name input call flag
$game_temp.name_calling = false
# Straighten player position
$game_player.straighten
# Switch to name input screen
$scene = Scene_Name.new
end
#--------------------------------------------------------------------------
# * Menu Call
#--------------------------------------------------------------------------
def call_menu
# Clear menu call flag
$game_temp.menu_calling = false
# If menu beep flag is set
if $game_temp.menu_beep
# Play decision SE
$game_system.se_play($data_system.decision_se)
# Clear menu beep flag
$game_temp.menu_beep = false
end
# Straighten player position
$game_player.straighten
# Switch to menu screen
$scene = Scene_Menu.new
end
#--------------------------------------------------------------------------
# * Save Call
#--------------------------------------------------------------------------
def call_save
# Straighten player position
$game_player.straighten
# Switch to save screen
$scene = Scene_Save.new
end
#--------------------------------------------------------------------------
# * Debug Call
#--------------------------------------------------------------------------
def call_debug
# Clear debug call flag
$game_temp.debug_calling = false
# Play decision SE
$game_system.se_play($data_system.decision_se)
# Straighten player position
$game_player.straighten
# Switch to debug screen
$scene = Scene_Debug.new
end
#--------------------------------------------------------------------------
# * Player Place Move
#--------------------------------------------------------------------------
def transfer_player
# Clear player place move call flag
$game_temp.player_transferring = false
# If move destination is different than current map
if $game_map.map_id != $game_temp.player_new_map_id
# Set up a new map
$game_map.setup($game_temp.player_new_map_id)
end
# Set up player position
$game_player.moveto($game_temp.player_new_x, $game_temp.player_new_y)
# Set player direction
case $game_temp.player_new_direction
when 2 # down
$game_player.turn_down
when 4 # left
$game_player.turn_left
when 6 # right
$game_player.turn_right
when 8 # up
$game_player.turn_up
end
# Straighten player position
$game_player.straighten
# Update map (run parallel process event)
$game_map.update
# Remake sprite set
@spriteset.dispose
@spriteset = Spriteset_Map.new
# If processing transition
if $game_temp.transition_processing
# Clear transition processing flag
$game_temp.transition_processing = false
# Execute transition
Graphics.transition(20)
end
# Run automatic change for BGM and BGS set on the map
$game_map.autoplay
# Frame reset
Graphics.frame_reset
# Update input information
Input.update
end
end
Go to the top of the page
 
+Quote Post
   
ccoa
post Jan 19 2008, 05:17 PM
Post #20


Storm Goddess
Group Icon

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




Aha!

Ruby is case sensitive. Window_Hud and Window_HUD are two different things, as far as it's concerned. You either need to rename your class Window_HUD or change the line in Scene_Map to Window_Hud. smile.gif


__________________________
Go to the top of the page
 
+Quote Post
   

2 Pages V   1 2 >
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: 24th May 2013 - 12:17 AM
RPG RPG Revolution is an Privacy Policy and Legal
eXTReMe Tracker