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.

Showing Text

In RGSS, I know you can use 'print' to have a popup. But which module would you use to print text on the screen as would happen with the "Show Text" event?
 
Well, i think you would have to create a window to write text on the screen, but i'm not very good at scripting so i'm not sure.
You could do it like this:

go to your script editor and insert an empty script.
Create a new class and name it whatever you want, lets say,  Window_First, and make it so that it is descended from Window_Base

Code:
class Window_First < Window_Base

Window_First can now do anything Window_Base can do.

The next step is Object initialization:
Code:
class Window_First < Window_Base
  #--------------------------------------------------------------------------
  # * Object Initialization
  #--------------------------------------------------------------------------
  def initialize
    super(0, 0, 256, 96)
  end
end

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 for a method named initialize in Window_Base.

This is the whole script you need to write something:
Code:
class Window_First < Window_Base
  #--------------------------------------------------------------------------
  # * Object Initialization
  #--------------------------------------------------------------------------
  def initialize
    super(0, 0, 256, 96)
    self.contents = Bitmap.new(width - 32, height - 32)
    self.opacity = 0
    
    refresh
  end
  
  #--------------------------------------------------------------------------
  # * Refresh
  #--------------------------------------------------------------------------
  def refresh
    self.contents.clear
    
    self.contents.draw_text(0, 0, 256, 32, "Hello, World")
  end
end

I will explain a little bit what these lines do:

This line will create a new bitmap for the window to draw anything on. This is necessary if you want to draw any text or images on the window.
Code:
self.contents = Bitmap.new(width - 32, height - 32)




This line will make the window invisible so that you can only see the text. You can remove this line if you want the window visable.
Code:
self.opacity = 0



This line does exactly what it sounds like, it will refresh the window.
Code:
def refresh


This line will erase anything that might already have been drawn on the bitmap
Code:
self.contents.clear

This line will draw the actual text
Code:
self.contents.draw_text(0, 0, 256, 32, "Hello, World")

this will explain it better:
Code:
self.contents.draw_text(x-position, y-position, width, height, "the text you want to write", align)

The align thing will change where the text will be displayed, 0 = left, 1 = center, 2 = right.
The align is optional and is left as default.



To call this, make an event with a call script that says:
Code:
$window = Window_First.new

To dispose the window:
Code:
$window.dispose



By accident i discovered that you can make the window dispose itself after a few seconds.
If you want the window to be disposed by itself after a few seconds you call it like this:
Code:
window = Window_First.new

Just remove the $.

If you would like to move the window you just change the 0 in
Code:
super(0, 0, 256, 96)

Where the first 0 = x-position  and the second 0 = y-position.



And that's it!  I hope i didn't make it too complicated  :wink:
Pheeew, that was a loooong text! :lol:

//Gando
 

e

Sponsor

You don't necessarily need to create a new Window to show text on the screen.

To write text, technically, you need a Bitmap object. That's basically it.

To have your bitmap displayed, however, is something else; Bitmaps technically have no idea what a z-axis is, and since the game is made up of layers of Graphic objects, to have yours on top requires you to apply your Bitmap on any object which supports a Z-axis, mainly : Sprite (not RPG::Sprite), Window and Plane.

For example, you could do :

Code:
text = "Hello World!"
text_layer = Sprite.new
text_layer.bitmap = Bitmap.new(200, 200) # width, height
text_layer.bitmap.draw_text(text_layer.x, text_layer.y, text_layer.bitmap.text_size(text).width, text_layer.bitmap.text_size(text).height, text)

Of course, that could be optimized (get the text size's rect only once, etc.), and you would have to move the sprite around (assign it coordinates : text_layer.x = 100, for example)

Note : this wouldn't show the text in a Window, as Gando's answer does.
 
Code:
text_layer.bitmap.draw_text(text_layer.x, text_layer.y, text_layer.bitmap.text_size(text).width, text_layer.bitmap.text_size(text).height, text)

Should be,
Code:
text_layer.bitmap.draw_text(0, 0, text_layer.bitmap.text_size(text).width, text_layer.bitmap.text_size(text).height, text)

Otherwise if the sprite is moved, the text is drawn outside the bitmap (not drawn at all)
 

e

Sponsor

Eh, for all I know you could be right. I wasn't sure if the coordinates were relative to the text_layer sprite or to the whole screen in general.
 

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