Storing Strings in Global Variables

EDIT: I resolved this issue because my interface object utilizing the global.player_name variable was not anchored on screen. It was therefore not appearing. I apologize for the false alarm!

But in case anyone is curious about what I was making, I've left the original message.

Original message:
I've been developing a profile creator in my game so that players can track their progress. So far I am stuck trying to store strings as global variables.

Basically, I have a string input object that gathers 12 alphanumeric characters and displays them in a custom font. After hitting enter, I wanted the txt_input to be stored in global.player_name. That way, in the interface for the player title, a custom name is displayed and referenced freely by other objects.

I currently have a separate object that is supposed to draw the string value for global.player_name but unfortunately returns with nothing. And there's no way I would ever consider using the proprietary pop-up prompts, sorry. That's just cheesy.

As always, any help would be greatly appreciated! Thank you!

Code:
/// Create Event
input_font = font_add_sprite(slashtext_1, ord(" "), 0, -1);
draw_set_color(c_white);
draw_set_font(input_font);

last_key = 0;
last_char = 0;
char_num = 0;

x_anchor = 400;
y_anchor = 300;

txt_input = "";
can_edit = true;
Code:
/// Step Event
last_key = keyboard_lastkey;
last_char = keyboard_lastchar;

switch(can_edit)
{
case true:
if keyboard_check_pressed(vk_anykey)
{
    if (last_key == vk_backspace)
    {
        // delete last character
        txt_input = string_copy(txt_input, 1, string_length(txt_input)-1 );
        char_num -= 1;
    }
    else if (last_key == vk_shift)
    {
        return 0;
    }
    else if (last_key == vk_enter) and (txt_input!="")
    {
        global.player_name = txt_input;
        can_edit = false;
    }
    else if (last_key >= 48 or last_key <= 57) or (last_key >= 122 or last_key <= 192)
    {
        // 65-90  : uppercase letters
        // 122-192 : lowercase letters
        // 48-57  : numbers
        // 192   : unknown
        // 32   : space
        // Add c to txt_input
        // or last_key==32
        txt_input = txt_input + last_char;
        char_num += 1;
        if char_num < 12
        {
        sound_stop(triple_bleep);
        sound_play(triple_bleep);
        }
    }
    if char_num > 12
    {
        sound_stop(sorry);
        sound_play(sorry);
        txt_input = string_copy(txt_input, 1, string_length(txt_input)-1 );
        char_num -= 1;
    }
}
break;

case false:
if keyboard_check_released(vk_enter)
{
global.player_name = txt_input;
room_goto(main_menu);
transition_kind = 21;
}
break;
}
Code:
/// Draw Event
draw_text(view_xview + x_anchor, view_yview + y_anchor, string(txt_input));
image_alpha = 1;
I kept this here in case anyone wants to make a text input system using custom fonts and setting the strings as a global variable that can be referenced in later objects. I looked up tutorials all over the internet and did not find anything beyond using cheap fonts and hideous pop-up boxes.

So I leave the code for you to use. Enjoy!
 
Last edited:

YellowAfterlife

ᴏɴʟɪɴᴇ ᴍᴜʟᴛɪᴘʟᴀʏᴇʀ
Forum Staff
Moderator
Have you considered that using keyboard_string could make for a slightly shorter snippet, as well as allowing the user to enter characters in their native language if your game can render them?
 
And that is why I left the code open, JUST in case. And thank you for your suggestion! I will definitely give that a go and see if I can produce more efficient results.
 
Top