• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Legacy GM How do I input a variable[SOLVED]

J

juanchacon99

Guest
Like (for those who program in C) the scanf() function or cin << "variable", is there a way I can input a variable and store it in GML? If so, can you tell me how? I really want to know to do a cheats menu, thanks for taking your time to read! :)

Edit 1: I know I can use the keyboard_check() function, but I'm too lazy to do it, and I still don't know how to store a variable using that method, ok?
 
T

The5thElement

Guest
Here is a little example of how you could create a basic cheat system that can be used in conjunction with what @makas
has suggested with a little modification. This system uses a ds_map with the key being the cheat code string
and the value being a script to be executed.

Code:
//Create Event.

//Create a ds_map("code", script);
CheatCodes = ds_map_create();

//Add codes to the CheatCode map.
ds_map_add(CheatCodes, "resupply", ResupplyAmmo_SCR);
ds_map_add(CheatCodes, "healme", RestoreHealth_SCR);

//Is accepting cheat codes.
CheatCodes_Busy = false;
Code:
//Game End Event

//Destroy the cheat codes ds_map.
ds_map_destroy(CheatCodes);
Code:
//Key Release Event for <Escape> Key.

if(CheatCodes_Busy != true){
    CheatCodes_Busy = true;
    var input = get_string("Enter a cheat code.", "");
    if(input != ""){
        if(ds_map_exists(CheatCodes, input)){
            script_execute(ds_map_find_value(CheatCodes, input));
        } else {
            show_debug_message("The cheat code entered does not exist.");
        }
        CheatCodes_Busy = false;
    } else {
        CheatCodes_Busy = false;
    }
}
 
Last edited by a moderator:
N

Never Mind

Guest
Create Event
Code:
Mode = 0; //0=waiting 1=typing
CheatString = "":
Step Event
Code:
if(Mode==0){ // waiting
if(keyboard_check_pressed(vk_insert)){
Mode = 1; // start typing
keyboard_string = CheatString;
}

if(Mode ==1){ // typing
CheatString = keyboard_string;

if(keyboard_check_pressed(vk_enter)){
Mode = 0;// stop typing

//is it a valid cheat code?
switch(CheatString){
case "this_cheat_code":
global.health = 100;
break;
case "this_other_cheat_code":
global.ammo = 10000;
break;
}
}

}
}
This code is just to get you thinking. Not meant for copy and paste.
Press insert and you change a variable named Mode from 0 where we wait into Mode 1 where you type.
You can read up on Game Makers' build in variable keyboard_string here.

Once your done typing we simply switch back to Mode 0 where typing isn't enabled.

If you wanted the string to reset each time you could just add
Code:
CheatString = "";
after the switch() statement.
 
Last edited by a moderator:
J

juanchacon99

Guest
Oh I see what I can do, I'm getting some ideas.
Here is a little example of how you could create a basic cheat system that can be used in conjunction with what @makas
has suggested with a little modification. This system uses a ds_map with the key being the cheat code string
and the value being a script to be executed.

Code:
//Create Event.

//Create a ds_map("code", script);
CheatCodes = ds_map_create();

//Add codes to the CheatCode map.
ds_map_add(CheatCodes, "resupply", ResupplyAmmo_SCR);
ds_map_add(CheatCodes, "healme", RestoreHealth_SCR);

//Is accepting cheat codes.
CheatCodes_Busy = false;
Code:
//Game End Event

//Destroy the cheat codes ds_map.
ds_map_destroy(CheatCodes);
Code:
//Key Release Event for <Escape> Key.

if(CheatCodes_Busy != true){
    CheatCodes_Busy = true;
    var input = get_string("Enter a cheat code.", "");
    if(input != ""){
        if(ds_map_exists(CheatCodes, input)){
            script_execute(ds_map_find_value(CheatCodes, input));
        } else {
            show_debug_message("The cheat code entered does not exist.");
        }
        CheatCodes_Busy = false;
    } else {
        CheatCodes_Busy = false;
    }
}
Yes! something like that was what I was thinking.
Create Event
Code:
Mode = 0; //0=waiting 1=typing
CheatString = "":
Step Event
Code:
if(Mode==0){ // waiting
if(keyboard_check_pressed(vk_insert)){
Mode = 1; // start typing
keyboard_string = CheatString;
}

if(Mode ==1){ // typing
CheatString = keyboard_string;

if(keyboard_check_pressed(vk_enter)){
Mode = 0;// stop typing

//is it a valid cheat code?
switch(CheatString){
case "this_cheat_code":
global.health = 100;
break;
case "this_other_cheat_code":
global.ammo = 10000;
break;
}
}

}
}
This code is just to get you thinking. Not meant for copy and paste.
Press insert and you change a variable named Mode from 0 where we wait into Mode 1 where you type.
You can read up on Game Makers' build in variable keyboard_string here.

Once your done typing we simply switch back to Mode 0 where typing isn't enabled.

If you wanted the string to reset each time you could just add
Code:
CheatString = "";
after the switch() statement.
THATS IT! That thing you did it's just perfect(I wont copy-paste though) Thanks!

And thanks everyone for your useful replies :D
 
N

Never Mind

Guest
Ah Ha!
No problem. Yeah keyboard_string is really useful. With my experience the code didn't take long to write.
I'm sure you would have got things working either way.. although I'd probably give it a little better search next time if I was you.

Have you ever used keyboard_check() or keyboard_check_pressed() ?
The manual is actually the first place I'd look in a search. There is a page related to keyboard functions. It has information about keyboard_string and all the keyboard map stuff.
But seriously it seems to me like your question isn't getting too much response. I'm surprised nobody has mentioned good ol' get_string() or get_string_async(). There's usually more than one way to do something.

If your learning to program and you don't fully understand the code, try to analyze it and experiment.
Split the problem up into simple steps and figure out the logic behind what needs to happen, then look at my code and think about what you know needs to happen compared to what you see being done.
Always ask, why. Why is that this way? What would happen if I changed this?
 
Last edited by a moderator:
Top