Error with textbox ;-;

E

EridanLOKO

Guest
so, I made a simple textbox to one NPC, but in the first interaction, the text goes out of the box, but if you interact again, it's normal

help pls ;-;

my code:
create:

Code:
text = "nothin";
page = 0;

xBuffer = 5;
yBuffer = 5;

boxWidth = sprite_get_width(textbox);
stringHeight = string_height(text);
creator = noone;
charCount = 0;
name = noone;
step:

Code:
if(keyboard_check_pressed(ord("E")))
{
    if(page+1 < array_length_1d(text))
    {
        page += 1;
        charCount = 0;
    }
    else
    {
        instance_destroy();
        creator.alarm[1] = 1;
    }
}
draw:

Code:
draw_sprite(textbox,0,x,y);

//Draw text

draw_set_font(fontezinha);

if(charCount < string_length(text[page]))
{
    charCount += 1;
}

textPart = string_copy(text[page],1,charCount);

//nome----------------------
draw_set_color(c_white);
draw_set_halign(fa_center);
draw_text(x+boxWidth/2,y+yBuffer,name);
draw_set_halign(fa_left);

//textin--------------------
draw_set_color(c_white);
draw_text_ext(x+xBuffer,y+stringHeight+yBuffer,textPart,stringHeight,boxWidth - (2*xBuffer));
 
Last edited by a moderator:

TsukaYuriko

☄️
Forum Staff
Moderator
Please take a look at our guide about how to post code so that your indentation won't be gobbled up the next time you post code here. I fixed the formatting in the opening post for you. :)

That aside... I see you're changing the font halfway through the Draw event and probably not any earlier. string_height and all related functions are relative to the currently active font, so make sure that you set the font to the one you will be using for drawing before calling those functions.

The second time you spawn a text box, the font will already have been set to the correct font. That's the reason why it displays correctly henceforth.
 

CloseRange

Member
it's as @TsukaYuriko except that i believe he meant to reference the create event.

In the draw event you are fine you use draw_set_font pretty much right away but in the create event you never set the font however you do use the function string_height.
As said already that function is based on what font you have currently said. The first time you interact with the NPC your font is still the GML default font (not your desired font).
Simply put the draw_set_font in the create event before you use that function and you'll be fine
 
Top