GMS 2 Text Box Height

How would I go about capturing the height of text that is being written to the GUI? Not really sure what code to paste here. Right now I am drawing a box with 9 slice, the printing the text on top of it a character at a time (like most games). Below is what it current looks like. The height is "hard-coded" in. I would love for it to fit whatever text is displayed. The height only.

Larger amounts of text...
Screenshot_15.png
Few amounts of text...
Screenshot_16.png

This is the script I have set up to run the 9 slice.
GML:
sc_9slice(x, y, boxW, height*.15, s9slice, 0);
This is the text being displayed.
GML:
draw_text_ext((portW*.5) - 190, portH*.097, messageText, -1, _maxW);

Obviously there is a lot more code not shown that's running this. But I think I might just need a violent shove in the right direction to look.
 
Looks like it might be working....sort of. It seems like it is maybe the exact size of the first line of the string. But doesn't accommodate the height for when the line grows/wraps. Here is the entire DrawGUI event...

GML:
/// @description
// Get current message data

var _arr    = messages[| messageID];
var _stgH    = 0;
var _image    = _arr[MSG.IMAGE];

// Set text font & properties
draw_set_font(f_dialog_body);
draw_set_halign(fa_left);
draw_set_valign(fa_top);

// Draw textbox
boxW = width*.33;

x = (portW*.5) - boxW*.5;
y = portH*.10;

sc_9slice(x, y, boxW, _stgH, s9slice, 0);

// Draw position
var _drawX = x + padding;
//var _drawY = y + padding;

// Draw image
if (sprite_exists(_image)) {
    var _imageW = sprite_get_width(_image);
    //var _imageH = sprite_get_height(_image);
    
    // Draw
    draw_sprite(_image, 0, portW*.5, portH*.06);
    
    // Offset drawing position for text
    _drawX += _imageW + padding;
}

// Text color
draw_set_color(c_white);

// Get maximum width for text
var _maxW = boxW - padding;

// Draw text
draw_text_ext((portW*.5) - 190, portH*.097, messageText, -1, _maxW);
_stgH = string_height_ext(messageText, 10, _maxW);

// Reset
draw_set_color(c_white)
See, looks like it is hitting the base of the first line. Not sure if thats it working for that line or just a coincidence.
Screenshot_17.png
 
Not really sure what I changed that made it start working but it works now. Below is what i have now.

GML:
/// @description
// Get current message data
// Get maximum width for text
var _boxW    = width*.33;
var _maxW    = _boxW - padding;
var _arr    = messages[| messageID];
var _stgH    = string_height_ext(messageText, -1, _maxW);
var _image    = _arr[MSG.IMAGE];

// Set text font & properties
draw_set_font(f_dialog_body);
draw_set_halign(fa_left);
draw_set_valign(fa_top);

// Draw textbox

x = (portW*.5) - _boxW*.5;
y = portH*.10;

sc_9slice(x, y, _boxW, _stgH + padding, s9slice, 0);

// Draw position
var _drawX = x + padding;
//var _drawY = y + padding;

// Draw image
if (sprite_exists(_image)) {
    var _imageW = sprite_get_width(_image);
    //var _imageH = sprite_get_height(_image);
    
    // Draw
    draw_sprite(_image, 0, portW*.5, portH*.06);
    
    // Offset drawing position for text
    _drawX += _imageW + padding;
}

// Text color
draw_set_color(c_white);

// Draw text
draw_text_ext((portW*.5) - 190, portH*.097, messageText, -1, _maxW);

// Reset
draw_set_color(c_white)
 
Follow the running order of your original code in your head.

1. First you set _boxW to width*.33.
2. Then you set _stgH to 0.
3. You then draw the 9-slice sprite with a height of _stgH (which is currently set to 0).
4. You then, later on, set _stgH to string_height_ext(messageText, 10, _maxW); after everything has already been drawn.
5. Then this sequence of events is repeated.

_stgH will always be 0 when you actually draw the 9-slice with this code order.

In your fixed code, you do this:

1. First you set _boxW to width*.33.
2. Then you set _stgH to string_height_ext(messageText, -1, _maxW);.
3. You then draw the 9-slice sprite with a height of _stgH (which is currently set to string_height_ext(messageText, -1, _maxW);).
5. Then this sequence of events is repeated.

In this version, _stgH is set to string_height_ext(messageText, -1, _maxW); before you draw the 9-slice, so the 9-slice will be drawn at the appropriate height.

The only tweak I'll suggest is set draw_set_font(f_dialog_body); before setting var _stgH = string_height_ext(messageText, -1, _maxW);. This is because string_height_ext() actually takes the currently set font into account, which means you need to set the font before measuring the height. Yours probably works fine now because you are not changing the font in between that Draw event running, but if you happen to change the font in another object in that same room at any point, your box height will suddenly no longer be accurate as string_height_ext() will be measured with that new font before you then reset the font to f_dialog_body.
 
Top