How to display text in the screen?

I was trying to display text in the screen with a script call inside an event, tried some solutions around but nothing seems to work.

I tried this code snippet took from Khan Academy:

textSize(46);fill(8, 142, 204);text("Sophia", 114, 120);But my game crashes saying: textSize is not defined.

If I use only the line with text("Sophia", 114, 120); => then I got "text is not defined".

I tried to use this, taken from other plugins:

this.drawText("example", 50, 50, 120, 'center');But then I get a crash "undefined is not a function".

Is there any way to do this? I wanted to learn how to display a text with exact position in the screen with a certain font size.
If you are just using that code anywhere, it will crash. Only Bitmaps and Windows have the drawText function defined. You'll need to create one of those objects, draw the text on that, and then make sure it is positioned where you want it on the screen. I recommend using a Window, as Bitmaps require a sprite to go with them in order to be displayed on the screen.

Also, when drawing text on Bitmaps and Windows, the coordinates are relative to the bitmap/window's position, so 50, 50 will be drawn 50 pixels to the right and below the bitmap/window's x & y coordinates.

You should check out the manual (F1 in the editor), as it has a nice documentation section at the bottom to help you with what functions are defined, and what arguments they take. It'll help you learn everything faster 🙂
If you are just using that code anywhere, it will crash. Only Bitmaps and Windows have the drawText function defined. You'll need to create one of those objects, draw the text on that, and then make sure it is positioned where you want it on the screen. I recommend using a Window, as Bitmaps require a sprite to go with them in order to be displayed on the screen.

Also, when drawing text on Bitmaps and Windows, the coordinates are relative to the bitmap/window's position, so 50, 50 will be drawn 50 pixels to the right and below the bitmap/window's x & y coordinates.

You should check out the manual (F1 in the editor), as it has a nice documentation section at the bottom to help you with what functions are defined, and what arguments they take. It'll help you learn everything faster 🙂
Thank you for your help.

Unfortunately, I'm looking over and over again in the manual (F1), but I can't find anyway to add text, either in the screen, or in a window. I don't even know how to create a window, the documentation doesn't provide any examples to create a window, or code snippets. It's not exactly newbie friendly, and I'm having a hard time to figure out how to insert a window or place text inside it. Do you know any example that could help me?

Edit

The documentation of the window is:

Spoiler
The window in the game.

Constructor Window ()

Item Index Methods
  • addChild
  • addChildAt
  • addChildToBack
  • isClosed
  • isOpen
  • move
  • removeChild
  • removeChildAt
  • setCursorRect
  • setTone
  • update
Properties
  • active
  • backOpacity
  • children
  • contents
  • contentsOpacity
  • downArrowVisible
  • height
  • margin
  • opacity
  • openness
  • origin
  • padding
  • parent
  • pause
  • upArrowVisible
  • visible
  • width
  • windowskin
  • x
  • y

MethodsaddChild ( child ) Object
Adds a child to the container.

Parameters:
  • child ObjectThe child to add
Returns😮bject:

The child that was added

addChildAt ( child index ) Object
Adds a child to the container.

Parameters:
  • child ObjectThe child to add
  • index NumberThe index to place the child in
Returns😮bject:

The child that was added

addChildToBack ( child ) Object
Adds a child between the background and contents.

Parameters:
  • child ObjectThe child to add
Returns😮bject:

The child that was added

isClosed ()
Returns true if the window is completely closed (openness == 0).

isOpen ()
Returns true if the window is completely open (openness == 255).

move ( x , y , width , height )
Sets the x, y, width, and height all at once.

Parameters:
  • x NumberThe x coordinate of the window
  • y NumberThe y coordinate of the window
  • width NumberThe width of the window
  • height NumberThe height of the window
removeChild ( child ) Object
Removes a child from the container.

Parameters:
  • child ObjectThe child to remove
Returns😮bject:

The child that was removed

removeChildAt ( index ) Object
Removes a child from the specified index position.

Parameters:
  • index NumberThe index to get the child from
Returns😮bject:

The child that was removed

setCursorRect ( x , y , width , height )
Sets the position of the command cursor.

Parameters:
  • x NumberThe x coordinate of the cursor
  • y NumberThe y coordinate of the cursor
  • width NumberThe width of the cursor
  • height NumberThe height of the cursor
setTone ( r , g , b )
Changes the color of the background.

Parameters:
  • r NumberThe red value in the range (-255, 255)
  • g NumberThe green value in the range (-255, 255)
  • b NumberThe blue value in the range (-255, 255)
Returns:
update ()
Updates the window for each frame.

Properties

active Boolean
The active state for the window.

backOpacity Number
The opacity of the window contents (0 to 255).

children Array
[read-only] The array of children of the window.

contents Bitmap

contentsOpacity Number
The opacity of the window contents (0 to 255).

downArrowVisible Number
The opacity of the window contents (0 to 255).

height Number
The height of the window in pixels.

margin Number
The size of the margin for the window background.

opacity Number
The opacity of the window without contents (0 to 255).

openness Number
The openness of the window (0 to 255).

origin Point
The origin point of the window for scrolling.

padding Number
The size of the padding between the frame and contents.

parent Object
[read-only] The object that contains the window.

pause Boolean
The visibility of the pause sign.

upArrowVisible Boolean
The visibility of the up scroll arrow.

visible Boolean
The visibility of the window.

width Number
The width of the window in pixels.

windowskin Bitmap
The image used as a window skin.

x Number
The x coordinate of the window.

y Number
The y coordinate of the window.
This does not show me any example on how to create or call a window, or even insert text inside it, and there are no other document for text there... or am I missing something?

Edit2

Isn't there a way to display a text at runtime (through script call?) this way we could create any type of menus/windows/interface anytime anywhere, it would be much easier to work with custom interface.
Edit2

Isn't there a way to display a text at runtime (through script call?) this way we could create any type of menus/windows/interface anytime anywhere, it would be much easier to work with custom interface.
You'd need someone to create a plugin for that. After double-checking the manual, it really does do the bare minimum in terms of explaining how things work. I'll look into improving the documentation when I have the time. Anyway, a quick way to create a window and draw text would be this:

var window = new Window_Help(x, y);window.setText("This is my text!");Window_Help is an easy-to-use window that will let you easily set the text to display. As a tradeoff, however, you don't get as much control over where it's drawn or it's alignment. In order to do that, you'll need to use Window_Base and manually draw things yourself.

Code:
var window = new Window_Base(x, y, width, height)window.drawText("text", x, y, maxWidth, lineHeight, alignment)
I tried these two script calls:

3a2r.png


3a2s.png


And, unfortunately, nothing is appearing on the screen. No window nor text. This is more complicated than I imagined. 🙁

I also tried with other values to test, no luck.

Edit: Tried this as well:

3a2t.png


got a crash: addChild is not defined.

Update: Replied from another thread (thanks Zalerinian)

var window = new Window_Base(x, y, width, height);
window.drawText("Hello World", x, y, maxWidth, lineHeight, alignment);
window.drawIcon(iconindex, {x: xpos, y: ypos});

// Since we're in the terms of Game_Interpreter, we need to use a little workaround:
SceneManager._scene.addWindow(window);
That oughta do it.

EDIT:

for closing it on user input, you're going to want to make a custom window class, or change Scene_Map to close that window and not open the menu, or make sure that window isn't open first before trying to open the menu. It's a bit more involved to do that, and I have realized the documentation sucks on explaining how to do things, and instead just explains a small part of the core scripts.

Original thread: https://forums.rpgmakerweb.com/threads/how-to-display-text-in-the-screen.46875/