Skip to main content

Command Palette

Search for a command to run...

Signposts and showing text mid-game

Updated
3 min read

we need two different objects here. First, we need a text Object which we can set a text value and it writes the text to the screen. Second, we need a sign which we can interact with that then, calls the text object to actually write on the screen

OText object

Create Event

We set some starting variables here, “letters” is a variable which gets increased by writing speed and when it gets a whole number, it shows the amount of letters in our “text_current” variable. Because we want to create a TypeWriter effect, we need to gradually increase this variable and show the letters one by one on the screen.

writing_speed = 0.25;
letters = 0;
text = "Test text \nDo not touch enemies !!!!!!!";
length = string_length(text);
text_current = "";
w = 0;
height = 0;
border = 10;

Step Event

Here we update our “letters” and “text_current” we also set the font and change the width and height of the string. Also, at the end we check that if player presses any key, it means they’re done reading and we need to bring back the focus to player and destroy the text object

//progress text

letters += writing_speed;
text_current = string_copy(text, 1, floor(letters));
draw_set_font(fSign);

if (height==0) height = string_height(text);
w = string_width(text_current);

if(letters >= length && keyboard_check_pressed(vk_anykey)){
    instance_destroy();
    with(o_camera){
        follow = o_player;
    }
}

Draw Event

Here we simply draw a black low transparency rectangle and the small triangle on the bottom. Then we write the text_current to the screen. Since this code runs in a loop, in creates the illusion of TypeWriter Typing

var halfw = w/2;

draw_set_color(c_black);
draw_set_alpha(0.5);
draw_roundrect_ext(x-halfw-border, y-height-(border*2), x+halfw+border, y, 15, 15, false);

draw_sprite(Sprite17,0, x, y);
draw_set_alpha(1);

//draw_text
draw_setup_text(c_white, fSign, fa_center, fa_top);
draw_text(x, y-height-border, text_current);

OSign object

our sign object only has one Event and a variable defined in Variable Definition called text. This code is written in Step Event. If we press “W” when we’re inside the 64 pixel border of the signpost and there’s no o_text object (we don’t want to write it twice), we create the o_text object and set its text to the signpost text. Also, the camera needs to follow the signpost till we click any key and become out of focus. We also set a nearby boolean. This boolean help us in creating a small little arrow on top of signpost when in range to attract the player’s attention

if((instance_exists(o_player) && point_in_circle(o_player.x, o_player.y, x, y, 64)) && (!instance_exists(o_text))){
    nearby = true;
    if (keyboard_check_pressed(ord("W"))){
        with(instance_create_layer(x, y-64, layer, o_text)){
            text = other.text;
            length = string_length(text);
        }
        with(o_camera){
            follow = other.id;
        }
    }
} else {
    nearby = false;
}

So inside the Draw event we go:

draw_self();
if nearby draw_sprite_ext(Sprite17, 0, x, y-50, 1, -1, 0, c_white, 1);

The Script

Now, let me show a script I would call before each writing in the game. We need to set all these variables before we write anything to our GUI so the fonts and other things don’t get mixed up.

///@arg color
///@arg font
///@arg halign
///@arg valign

function draw_setup_text(){
    draw_set_color(argument0);
    draw_set_font(argument1);
    draw_set_halign(argument2);
    draw_set_valign(argument3);

}