GML Visual Player in game typing

Can I make a way for the player to type something in my game?

I would like the player to be able to type in a number and it sets a variable to that number.

I am using GMS 2 and DnD

Thanks!
 
I've never tried this myself, but you could probably use a series of keyboard_check_pressed functions to convert the key that is pressed into an onscreen letter.
But it depends on how many characters the player is allowed to type/the max length.

For example, let's say you have a max character limit of five.
Code:
if current_character != 5{ //if you haven't reached the limit
if keyboard_check_pressed(ord("A")){

switch(current_character){
         case 1:
               character[0] = a; //set's character[0] to a, which can be drawn through a draw event.
               current_character = 1; //changes the current_character, since the previous one has been filled
               break;
         case 2:
              character[1] = a;
              current_character = 2;
             break;
         case 3:
               character[2] = a;
               current_character = 3;
               break;
         case 4:
              character[3] = a;
              current_character = 4;
             break;
        case 4:
              character[4] = a;
              current_character = 5;
             break;
}
}
}
Seems pretty complicated, but that's all I can really think of.
 
That is good insight! However, I am trying to make where the player can type in a number and the number typed becomes a variable. Literally what the programmer can do with setting a variable to a number I just need the player to be able to set the number in game
 
Short Answer:
You could make use of the functions variable_instance_get_names(), variable_instance_set() and variable_instance_exists()

I'm not sure if you can do this in DnD. I had a quick look at the manual and didn't see any equivalent functions.

If you are looking to do live editing of your code, @YellowAfterlife has a neat extension called GMLive which lets you edit stuff in realtime I believe (I've not used it myself)

Forum Link : GMLive

Here is a *very* rough example below (do not use it as-is in your project) using the functions I mentioned above to give you an idea, (Warning! may contain bugs!)
So if the user inputs a string, such as "obj_player.spd = 10", you would break the string into parts using the appropriate string functions, and then you could use:

Code:
// The input string - you would need to get this from the player using a get_string_async() for example
var _input_string = "obj_player.spd=10";

// Extract the object name as a string and convert to object_index
// NOTE : Preferably you would provide the player with a list of instances in your game to choose first, in
// which case you wouldn't need to use asset_get_index to retrieve the object_id, this
// example is just a quick mock up and would only work properly if there is just one instance of obj_player in
// the room.
var _object_name_end_pos = string_pos(".", input_string);
var _inst_name = string_copy(_input_string, 1, _object_name_end_pos - 1);
var _inst_id = asset_get_index(_inst_name);

// Extract the variable name from the string 
var _variable_name_end_pos = string_pos("=", _input_string);
var _variable_name_length = _variable_name_end_pos - _object_name_end_pos;
var _variable_name = string_copy(_input_string, _object_name_end_pos + 1, _variable_name_length);

// Extract the value from the string. (Assuming its meant to be a real - the final version you
// would need to add some checks, like if the player put quotation marks around the value
// then read it as a string.
var _value_length = string_length(_input_string) - _variable_name_end_pos;
var _value = real(string_copy(_input_string, _variable_name_end_pos + 1, _value_length);

// Check if the obj_player actually has a variable with the name given, and if so, set it to the value.
if ( variable_instance_exists(_inst_id, _variable_name) )
{
    variable_instance_set(_inst_id, _variable_name, _value);
}

I just wrote this, so the calculations of the lengths of strings and such may be off by plus or minus one, but this is the general idea.

Also, this is written specifically for the example input string "obj_player.spd=10", you would need to write a more complete text parser
to cover all possible inputs including invalid ones.
 
I think I actually found a way to make it work using the keyboard_string variable. I just didn't know it existed. Thanks!
 
Top