r/gamemaker • • 1d ago

Help! Trigger Multiple Text Boxes based on Player's X-Coordinates in a Single Room

Hi everyone.

I'm new to Gamemaker and more complicated coding like this (have some CSS and HTML experience from years back). I've been working with tutorials on Youtube (mostly Peyton Burnham & Friendly Cosmonaut), which cover a lot, but I'm struggling to get the above topic to work and I'm not finding a ton of useful help online. It seems like it should be simple enough, but my child mind apparently needs step-by-step instructions. Anyone got a solution for this?

I'm using Burnham's tetbox tutorial as a base.

Here is the code for my obj_textbox object.

CREATE:

global.in_dialogue = true;
depth = -9999;

//---------THE TEXT---------// 

page = 0; 
page_number = 0; 

text[0] = "I'm so glad you're back!" 
text[1] = "I've been waiting for you." 
text[2] = "I've missed you very much..." 

text_length[0] = string_length(text[0]); 
draw_char = 0; 
text_spd = 1; 

setup = false;

DRAW:

accept_key = keyboard_check_pressed(vk_space) or mouse_check_button_pressed(mb_left);

textbox_x = camera_get_view_x( view_camera[0] )+ 10;
textbox_y = camera_get_view_y( view_camera[0] ) + 250;

//---------SETUP---------//
if setup == false
{

setup = true;
draw_set_font(global.font_main);
draw_set_valign(fa_top);
draw_set_halign(fa_left);

//--------LOOP THRU PAGES---------//
page_number = array_length(text);
for(var p = 0; p < page_number; p++)
{

//---------Find how many characters on each page and store that # in the "text_length" array---------//
text_length[p] = string_length(text[p]);

//---------Get the x position for the textbox---------//
//--------No Character (center textbox on screen)---------//
text_x_offset[p] = 44;

}

}


//---------TYPING TEXT---------//
if draw_char < text_length [page]
{
draw_char += text_spd;
draw_char = clamp(draw_char, 0, text_length[page]);
}


//--------FLIP THRU PAGES----------//

if accept_key
{

//----------IF TYPING IS DONE---------//
if draw_char == text_length[page]
{

//---------NEXT PAGE---------//
if page < page_number-1
{
page++;
draw_char = 0;
}
//--------DESTROY TEXTBOX---------//
else
{
instance_destroy();
}

}
//---------IF TYPING NOT DONE---------//
else
{
draw_char = text_length[page];
}
}

//---------TEXTBOX PARAMETERS---------//

textbox_width = 200;
textbox_height = 64;
border = 8;
line_sep = 12;

line_width = textbox_width - border*2;
textb_spr = spr_textbox;
textb_img = 0;
textb_img_spd = 1/60;

//--------DRAW TEXTBOX---------//
textb_img += textb_img_spd;
textb_spr_w = ( sprite_get_width(textb_spr) / 2 )
textb_spr_h = ( sprite_get_height(textb_spr) / 2 );

//---------BACK OF TEXTBOX---------//
draw_sprite_ext(textb_spr, textb_img, textbox_x + text_x_offset[page], textbox_y, textbox_width/textb_spr_w, textbox_height/textb_spr_h, 0, c_white, 1);

//---------DRAW TEXT---------//
var _drawtext = string_copy(text[page], 1, draw_char);
draw_text_ext(textbox_x + text_x_offset[page] + border + 5, textbox_y + border + 

DESTROY:

global.in_dialogue = false;

if (text_length[0])
{
instance_destroy();
}
2 Upvotes

5 comments sorted by

1

u/RykinPoe 1d ago

Please share you code.

What you are trying to do should be simple. Just need to reference obj_player.x in your textbox drawing code. May need to run that through a function to convert room coordinates to camera or GUI coordinates if you are drawing the textbox to the GUI layer. I have no idea how Peyton did it in their tutorial.

1

u/Powerful-Dragonfly32 1d ago edited 1d ago

Code added to post. I think a big part of what I'm struggling with is exactly where to put the reference and how to phrase it. My goal is to have the first two messages in the array play at startup (it's the opening of the game), and then have the rest trigger as the player walks right, until the speaking character finally appears.

0

u/RykinPoe 1d ago

If you are trying to trigger them at a certain position in the room then you would probably want to use an instance of an invisible collision object in the room. When the player enters it you would lock control and spawn a new textbox instance with the text you want it to display. When the play dismisses the textbox you can return control and delete the collision instance.

The way it is currently coded you basically have the text hardcoded into the textbox in the create event. You can do this and just expand your array of text to add more lines but it would be better to create a system where you specify a line in a json file or something similar for the object to read in and display. I have never done this particular tutorial so I don't know how robust this system is but from what you are showing so far I would say it is not very robust. There are tons of tutorials out there on ways to do this so you might want to look at them or finish up Peyton's before you start trying to modify it.

1

u/Powerful-Dragonfly32 1d ago

I was trying to collision route prior to this (using the same code), but nothing seems to happen when the character enters the area. After trying half a dozen tutorials I thought maybe using the x-axis would be simpler.

Thank you for your suggestions. I'll take them into consideration and continue searching. I didn't realize Peyton's tutorial was multi-part, so I'm going to go ahead and try to finish it this week.