GMS 2.3+ Generate random request in minigame


Hey. I'm creating a drink making mini-game and wondering how to generate random requests. Like where do I store the recipes, and then how do I have the game pull one of the drink names to request from the player - then after they make the drinks, have the game check the ingredients to make sure they're correct?

My initial thoughts are store ingredients in an array, then on the last check have the game check the ingredients against each array position for the specific request that was made - but im not sure how to generate the random request (ie "Make drink X out of the 6 available")



You almost had it all though out ^^

requests = array_create(0); // array of requests

ingredients= 12;

    var d = array_create(0); // drink ingredients array

    repeat(3)array_push(d, irandom(ingredients-1) ); // fill it with 3 numbers

    array_push(requests, d) // add it to the requests
/* now request holds 2 arrays with drinks, which each hold 3 ingredients
At this point you could select a number:
pos = irandom(array_length(requests)-1);

and refer to the one you should make with:

or just copy the one you should make to a separate variable:

goal = requests[ irandom(array_length(requests)-1)];

then you have your mix array:
mix = array_create();

after you drop ingredients on it you do array_push on it with the number the ingredient should be, then check array_equal to see if it is completed


My initial thoughts are store ingredients in an array
Almost, but instead of storing the ingredients in an array, store the 'recipes' in an array. Now you might think - wait, but a recipie has many information, how? - then the answer is: structs. The recipie itself is a struct with all the information.

Maybe something like this:

function my_recipe (_name) constructor
    recipe_name = _name;
    ing_count = 0;
But we are not done. Maybe each recipe has a different amount of ingredients needed. So we store them dynamically with a trick.

We go somewhere else, maybe the request controller object, create event and use this struct like this:

all_recipes = array_create(); //(1)

var _recipe = new my_recipe("Buttered Fish"); //(2)
variable_struct_set(_recipe, "ing_0", "Butter"); //(3)
variable_struct_set(_recipe, "ing_1", "Fish"); //(4)
variable_struct_set(_recipe, "ing_count", 2); //(5)

array_push(all_recipes, _recipe); //(6)

//the other recipes
  1. Creates the array, that holds all recipes
  2. Uses our struct, we create one and assign a name to it. The struct has now recipe_name == "Buttered Fisch" and ing_count == 0
  3. creates a new struct variable in your struct named ing_0 and assignes "Butter" to it. Notice the name. The 0 is extremly important. No your struct has also ing_o == "Butter"
  4. creates a new struct variable in your struct named ing_1 and assigns "Fish" to it. Again, the name is important. ing_ is the same, but the number is incremented by 1 !!!
  5. ing_count exists, so it doesn't creates it, but it assigns the number to it. The number matches the ingredients you created (here hard coded)
  6. Push this recipe with a name, an ing_count and two ingredients into the array
Now the magic happens. How can you check your struct later. The recipes might have different ing_counts, so hard coding won't really work. But we can make it dynamic.

//accessing the struct from somewhere

player_choice = "Wasabi"; //what the player used
var _correct = false; //checker for later

var _recipe = all_recipes[0]; //the current recipe
var _count = _recipe.ing_count; //how many ingredients are there?

for(_i = 0; _i < _count; _i++)
    var _ing = variable_struct_get(_recipe, "ing_" + string(_i)) //(A)
    if(_ing == player_choice) _correct = true;

//just a random evaluation
text = (_correct) ? "Yay" : "Buuh, bad chef";
(A) is the magic. Because we builded the string with a specific method and the function needs a string for finding the struct variable, we can use this.

Random recipe should be obvios:
next_recipe_number = irandom_range(0, array_size(all_recipes) - 1);
is the easiest one.

How you acutally check your player choices against the recipes or if you wanna make the random requester smarter is up to you.

Edit: Out of my head, not tested, but the logic should work.
Last edited: