Help - Search - Members - Calendar
Full Version: Displaying a Variable in the Screen Corner
RPG RPG Revolution Forums > Game Engines > RPG Maker XP Discussion
Donotfeedthemax
I don't even know if this is possible without scripting, but I'm hoping somebody smarter than me knows maybe a mathematical formula?

Basically, I'm trying to display the value of a variable in the bottom right corner of the screen, as part of a HUD. Basically, rupees, from Zelda (it is a Zelda fan-game, haha). The amount of rupees you have is stored in a specific variable, and I want the player to be aware of this throughout the game. The way I've started doing this is by having a conditional branch and if rupees is 1, show a 1, if 2, show a two... hypothetically all the way up to 999 or at least 99... but that's too much work!

So is there a way that I can do some math with variables, or if this requires a script, maybe this needs to be moved, so that I can display how many rupees the player has?

Thanks if anybody has any help!
yamina-chan
I am not a scripter by any means, but I was able to make you something.
Tested and works. At least if you don't mind the window beeing there also. I don't know enough about scripting yet to do that ^^'
CODE
# Corner Hud for displaying a Variable
# by yamina-chan for Donotfeedthemax
class Corner_HUD < Window_Base
  def initialize
    super(511, 427, 130, 54)
    self.contents = Bitmap.new(width - 32, height - 32)
    self.contents.font.size = 20
    refresh
  end
  def refresh
    self.contents.clear
    @corner_var = $game_variables[1]
    self.contents.draw_text(65, -6, 100, 32, @corner_var.to_s)
    self.contents.draw_text(5, -6, 100, 32, 'Rupees')
  end
  def update
    super
    refresh if @corner_var != $game_variables[1]
  end
end
class Scene_Map
  alias corner_hud_main main
  alias corner_hud_update update
  def main
    @corner_hud = Corner_HUD.new
    corner_hud_main
    @corner_hud.dispose
  end
  def update
    @corner_hud.update
    corner_hud_update
  end
end
Tsukihime
You can set the opacity of the window to make it transparent, as such:

CODE
def initialize
    super(511, 427, 130, 54)
    #make the window transparent so you don't see it
    self.opacity = 0
    self.contents = Bitmap.new(width - 32, height - 32)
    self.contents.font.size = 20
    refresh
  end


However now the text is a little hard to see.

You can use
CODE
self.contents.font.color= Color.new(0,255,0,255)


to change the color of the font. Maybe copy it under the line that sets the size.
The arguments are (red, green, blue, alpha), where each value goes from 0 to 255. So in my example I set it to pure green.
yamina-chan
Ah, it's self.opacity, not window_opacity! No wonder I couldn't get a result XD
I've learned something new and Donotfeedthemax hopefully got a working solution =)
Donotfeedthemax
Guys, thanks a lot! So basically, I type in Tsukehime's script over Yamina's at the def initialize point? I'll be trying this tomorrow, perhaps tonight, so I'll let you know how it works then. Just one question...

Where do I write what I want it to say? I mean, what I want the box to say is \n[x] where x is the number of the variable whose value I want displayed, I can't remember which variable it is at the moment, right? So say it's variable number 25, where do I put in the command to set the text of the text box as \n[25] ?

That is, I'm PRETTY sure that backslash n stuff is the right code...
yamina-chan
You'll have to set it twice.
Seach for the line:
@corner_var = $game_variables[1]
and replace the number in the brackets with your variable number. Then you do the same with this line:
refresh if @corner_var != $game_variables[1]
If you want to change the text in front of the Vaiable look for this line:
self.contents.draw_text(5, -6, 100, 32, 'Rupees')
There you can change Rupees to something else. If the word is much longer tough, you might need to change the size of the variable window.

If you want the variable to be shown in a normal textbox, you need to use the following command: \v[x] where \v is the shortcut for displaying a variable and x is the number of the variable. You don't need a backslash in the actuall code.
Donotfeedthemax
Oh yeah, V, not N, haha thanks. What's funny is that when you're on an apple device, those code quote box things are a bit weird, and aren't obvious about how long they are, and I thought this script was about 5 lines long, and it took me forever to realize that the thing you were telling me to search for WAS in there, I just had to scroll down.

Well thanks again. If I don't want it to say rupees (if I were to just put a picture instead of the word rupees, via a common event), should I just delete that whole line about self.contents.draw? Or just delete the word rupees?

Also... Hmm, how to get this script onto a computer incapable of connecting to the Internet?...

OH! One more thing, as long as I've got the attention of such helpful people, right now I'm turning the HUD off by a switch, when it's off the HUD goes away and there are letter boxes, can I make the rupee box go away with the same switch?

Thank you so much!
maximusmaxy
I edited a bunch of the old code to give it the enable/disable function, I also changed it to show a sprite instead of a window, this way you do not need to worry about windows being shown and may even be slightly less laggy (not that it lagged in the first place). I also included a simple outlined text method so you can see the text more clearly

CODE
# Corner Hud for displaying a Variable
# by yamina-chan for Donotfeedthemax

module CORNER
  #easy method of enabling hud
  def self.enable
    $game_system.corner_hud = true
    return
  end
  #easy method of disabled the hud
  def self.disable
    $game_system.corner_hud = false
    return
  end
end


class Game_System
  attr_accessor :corner_hud
  alias max_corner_initialize_later initialize
  def initialize
    max_corner_initialize_later
    #added a variable to enable/disable the hud
    @corner_hud = true
  end
end

class Bitmap
  #draws outlined text
  def draw_text_corner(x, y, w, h, text, align = 0)
    #save old font color
    old_color = self.font.color.clone
    #change font color to the outline color
    self.font.color = Color.new(0,0,0,100)
    #draw the outline around the text
    draw_text(x + 1, y + 1, w, h, text, align)
    draw_text(x + 1, y - 1, w, h, text, align)
    draw_text(x - 1, y + 1, w, h, text, align)
    draw_text(x - 1, y - 1, w, h, text, align)
    #reset the font color
    self.font.color = old_color
    #draw the original text
    draw_text(x, y, w, h, text, align)
  end
end

#changed to inherit from the sprite class instead of window
class Corner_HUD < Sprite
  def initialize(viewport = nil)
    super(viewport)
    #create the sprite bitmap
    self.bitmap = Bitmap.new(130, 54)
    #location variables
    self.x, self.y, self.z = 511, 427, 9000
    #refresh content
    refresh
  end
  
  def refresh
    #clear the bitmap
    self.bitmap.clear
    #change the corner variable to game variable 1
    @corner_var = $game_variables[1]
    #draw the word Rupee's
    self.bitmap.draw_text_corner(0, 0, 130, 32, 'Rupees')
    #draw the corner variable as a string
    self.bitmap.draw_text_corner(80, 0, 130, 32, @corner_var.to_s)
  end
  
  def update
    #refresh if needed
    refresh if @corner_var != $game_variables[1]
  end
end

class Scene_Map
  alias corner_hud_main main
  alias corner_hud_update update
  def main
    #create the corner hud if enabled
    @corner_hud = Corner_HUD.new if $game_system.corner_hud
    #run the old main method
    corner_hud_main
    #dispose corner hud if it exists
    @corner_hud.dispose unless @corner_hud.nil?
  end
  def update
    #run the old update method
    corner_hud_update
    #if the corner hud is enabled
    if $game_system.corner_hud
      #creat the corner hud if it doesn't exist
      @corner_hud = Corner_HUD.new if @corner_hud.nil?
      #update the corner hud
      @corner_hud.update
    #else if the corner hud is disabled and the corner hud exists
    elsif !@corner_hud.nil?
      #dispose the corner hud
      @corner_hud.dispose
      @corner_hud = nil
    end
  end
end


To enable/disable the hud simply type one of the following in the script event command
CODE
CORNER.enable
CORNER.disable


If you want to replace the word Rupee's with a picture you could replace the line:
self.bitmap.draw_text_corner(0, 0, 130, 32, 'Rupees')
with these two lines
bitmap = RPG::Cache.picture(NAME OF THE FILE)
self.bitmap.blt(0,0,bitmap,bitmap.rect)
That way it will look into your pictures folder and draw whatever the name of the file you specified. Make sure the name of the file is enclosed in "quotes"

BTW the PZE will be releasing a south clock town demo very soon including a bunch of scripts(Majora's Mask Time system, Navi, Ocarina, Minimap, Map name display) that may be useful to you.
yamina-chan
XD
There goes the professional. You should replace my name in the script with yours maximusmaxy. This is beyond my understanding, but I will take a closer look at it later to see if I can learn. Besides: it was your work.
I'm pretty sure that is what Donotfeedthemax wants.

(As for the question on how to get this on a computer without internet...I suppose you allready know the answer: either type it all manually, or save in in a dcoument on a pc that has internet and then transfer it to yours with a CD or an USB stick for example °-^)
maximusmaxy
Can't be bothered adding my name to anything wink.gif
I've been doing allot of HUD based scripts at the moment, and all I did was copy/paste a bunch of my snippets, change it up a bit and comment it.
Donotfeedthemax
Guys, thanks a lot! I got a chance to try it today, and it works like a charm, after I edited it a little to move the rupee symbol to the right and closer to the numbers. Thanks.

One thing, though, is it possible to make the black outline thicker?
maximusmaxy
Try changing these lines
CODE
    #change font color to the outline color
    self.font.color = Color.new(0,0,0,100)
    #draw the outline around the text
    draw_text(x + 1, y + 1, w, h, text, align)
    draw_text(x + 1, y - 1, w, h, text, align)
    draw_text(x - 1, y + 1, w, h, text, align)
    draw_text(x - 1, y - 1, w, h, text, align)

To these lines
CODE
    #change font color to the outline color
    self.font.color = Color.new(0,0,0,50)
    #draw the outline around the text
    for i in -2..2
      for j in -2..2
        draw_text(x + i, y + j, w, h, text, align)
      end
    end

Then change the 50 in self.font.color = Color.new(0,0,0,50) to a higher number if you want it thicker (no more then 255).
Tsukihime
Is that just drawing the same text with bigger font (and thus the illusion that it is an outline), or does it actually draw an outline.
maximusmaxy
Yeah it just draws the text of the same font size around it a couple times with a darker font colour to give the illusion of an outline, I don't think there's a simpler way to do it.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2013 Invision Power Services, Inc.