Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

draw_text method

OS

Sponsor

I need some help with a method of window.contents. I would like to know the arguments for draw_text, and how to fix this little script:
EDIT::Made some changes:
Code:
class Window_PickLock < Window_Base
  #PICK_ID = Game_LocKeys::PICK_ID
  attr_accessor :pick_count
  attr_accessor :text #Starts as "You attempt to pick the lock!"
  def initialize(text)
    super(100, 100, 448, 64)
    self.contents = Bitmap.new(width - 32, height - 32)
    @text = text
    self.x = 320 - width/2
    self.y = 240 - height/2
    refresh(@text)
  end
  #--------------------------------------------------------------------------
  # * Refresh
  #--------------------------------------------------------------------------
  def refresh(text, pick_count = $game_party.item_number(Lock::PICK_ID))
    self.contents.clear
    cx = contents.text_size(text).width
    self.contents.font.color = normal_color
   #self.contents.draw_text(x, y, width, hieght, hieght, text[, align])
    self.contents.draw_text(4, 0, 120-cx-2, 32, text)
  end
  #--------------------------------------------------------------------------
  # * Text
  #--------------------------------------------------------------------------
  def text=(string)
    @text = string
    refresh(@text)
  end
end

I need to window to be in the center of the screen, and I need the text to appear (which won't for unseen reasons). Any help would be much appreciated. Peace Out!

~Broken
 
Arguments:
Bitmap#draw_text(x, y, width, height, text[, align=0])

You can change

Code:
refresh(@text)

to

Code:
self.x = 320 - width/2
self.y = 240 - height/2
refresh

and change

Code:
def refresh(text, pick_count = $game_party.item_number(Lock::PICK_ID))

to

Code:
def refresh(pick_count = $game_party.item_number(Lock::PICK_ID))
 

OS

Sponsor

Thanks for the help, but taking text out doesn't do anything...

So now all I need to know is how to show the text. If any one can help with that, please do...
 
Code:
# Type A (Using Rect)
<bitmap>.draw_text(<rect>, text [, alignment = 0)
# Type B (Using X, Y, W, H)
<bitmap>.draw_text(x, y, w, h, text [, alignment = 0))

# Alignment : 0 - Left, 1 - Center, 2 - Right

The draw_text method defines a rect, then draws the text within that rect. The text will never extend outside the rect.

I suggest changing your code to something like this:

Code:
class Window_PickLock < Window_Base
  #PICK_ID = Game_LocKeys::PICK_ID
  attr_accessor :pick_count
  attr_reader :text # This does not need to be accessor, since you have a writable method
  def initialize(text = "You attempt to pick the lock!")
    w, h = 448, 64
    super(320 - w / 2, 240 - h / 2, w, h)
    self.contents = Bitmap.new(width - 32, height - 32)
    refresh(text)
  end
  #--------------------------------------------------------------------------
  # * Refresh
  #--------------------------------------------------------------------------
  def refresh(text, pick_count = $game_party.item_number(Lock::PICK_ID))
    self.contents.clear
    @text = text
    cx = contents.text_size(text).width
    self.contents.font.color = normal_color
    self.contents.draw_text(0, 0, contents.width, 32, @text)
  end
  #--------------------------------------------------------------------------
  # * Text
  #--------------------------------------------------------------------------
  def text=(string)
    @text = string
    refresh(@text)
  end
end

Something similar to this. I personally would delete the access to your @text instance (delete line attr_reader :text line), and your text-(string) block. Since you now save the @text in the refresh block, you can change the @text instance with just calling the refresh method.

My suggestion for final product:
Code:
class Window_PickLock < Window_Base
  #PICK_ID = Game_LocKeys::PICK_ID
  def initialize(text = "You attempt to pick the lock!", pick_id = <place_your_default_here>)
    w, h = 448, 64
    super(320 - w / 2, 240 - h / 2, w, h)
    self.contents = Bitmap.new(width - 32, height - 32)
    refresh(text, pick_id)
  end
  #--------------------------------------------------------------------------
  # * Refresh
  #--------------------------------------------------------------------------
  def refresh(text = @text, pick_id = @pick_id)
    self.contents.clear
    @text, @pick_id = text, pick_id
    cx = contents.text_size(text).width
    self.contents.font.color = normal_color
    self.contents.draw_text(0, 0, contents.width, 32, @text)
    # I guess you want to put something with the pick id here...
  end
end
 

OS

Sponsor

Thanks! My script is nearly finished! Now I need help with one more thing...

How do I make that window stay on-screen until a key is pressed?

Do I have to change the window type to Selectable, or add a conditional in the Window for Input. I tried making an input conditional in the scene, but it didn't work.

Code:
def wait_key
   if Input(Input::C)
      @picklock_window.active = false
      @command_window.active = true
   end
end

This is the conditional from my scene. It didn't work at all for some reason (visibilty changes in the update method based on what is active, just so you know...)

If you can aid me in this, then I grant thee a thousand seconds of happiness, and a cookie made of enlightenment...and chocolate chips!

Peace Out!

~Broken

EDIT: I also need to double the size of the window so I can draw_item_name(@pick_id, x, y) with the amount of pick_locks in inventory, as well as how to draw an icon by itself.
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top