• 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!

Script that can return user input to any variable?

S

shanedaman112

Guest
Ok, sorry for the beastly title, but it's basically the only way to say it. I'd say i'm pretty experienced in gml, but I just upgraded to gm:studio (humble bundle, surprise, surprise) and I need something equivalent to show_message_ext. I need to set a variable in the middle of a code. Like for example, midway into the step event, var1 = msg_choice("Pick","1","2","3"). If I was targeting the same variable every time that wouldn't be so bad because I could implement it directly into an object that displays the message (e.g. obj1.specificvar = choice). Problem is, I want to be able to pick up the code and stick it in any game, and use it on any variable in that game. show_message_ext had that universality.
So, the question here is, Is there a way to set a variable specified by obj1's code from obj2 with the same code run by obj2 each time? even if obj1 calls the script multiple times, assigning the script's return value to different vars? (e.g. "var1 = msg_choice(args)" then later in the code "var2 = msg_choice(args)") (run on sentence of the year). If there isn't a way to do that, is there a way to break a script, get the var, and resume the script where left off? Any way this message box is possible would be great, if not, I'll find some other way to get my input. The same concept here goes to text input.
 
Unfortunately, you can neither halt execution mid-script to get a value from the player, nor can you dynamically assign a value to a variable based on its variable name. You'll have to refactor your code to allow for getting values from the player over the course of multiple steps. Depending on exactly what you need to do, there's a multitude of different ways to do so. If I understand roughly what you want, that being a modular piece of code that you can slap into any project, then the simplest way to achieve what you need is to create a dedicated object to display the message box, grab the input, then store it into a global or instance variable that any other piece of code can grab. A VERY quick and dirty example is as follows:

Create:
Code:
global.msg_value = -1;
global.show_msg = false;
Step:
Code:
if (show_msg)
{
    var button_index = -1,
        index = 0;

    repeat (num_buttons)
    {
        if (button_clicked[index])
        {
            button_index = index;
            break;
        }
        index++;
    }

    if (button_index != -1)
    {
        global.msg_value = button_index;
        global.show_msg = false;
    }
}
Draw:
Code:
if (global.msg_show)
{
    // Draw your msg box!
}
... And any time you need to use the message box, you'll have to do something like the following:

Code:
if (!global.msg_show)
{
    switch (state)
    {
        case "greeting":
            text = "Hello!  What is your favorite color?";
            msg_box_show("Red", "Green", "Blue"); // This would just be a simple script to set the values of your msg box, plus would also set 'global.msg_show' to true.
            state = "answer";
            break;
        case "answer":
             var colors = 0;

             colors[0] = "Red";
             colors[1] = "Green";
             colors[2] = "Blue";
             text = "Wow!  " + colors[global.msg_value] + " is a great color!";
    }
}
Do note that this code will not actually work, as it glosses over a lot of functionality, like actually getting button presses, but it's a skeleton as to how to go about getting values from a pop up over multiple steps instead of halting execution.
 
Last edited:
S

shanedaman112

Guest
I had a dedicated instance already, I just wanted to know if it was possible to pause the script like the legacy version. That example is a neat way to do it though. Thanks for the help!
 
Top