[Solved] How to structures for a cookbook with player-created recipes?

I’m making a cooking game and I trying to think out a mechanic where players can create and title their own recipes with in-game items, and then they can use those recipes in other scenes (other parts of the game, like a cooking screen).
I don’t have a strong programming background, but I’m pretty sure I need to use structures for this, because the player should be able to:
-Create New Recipe
-Enter recipe title.
-add an ingredient (from a list of item ids I put in already). Then, automatically create a new space for add an ingredient, etc.
-save this info in a structure(?), eventually to a JSON string for permanent storage
-Create a new empty structure(?) ready for a new recipe

Should the recipe title be the structure name, or a child variable? Is it possible for users to continuously create new structures?
Then the player can go to the kitchen scene, select their recipe, and it pulls all the info saved in the structure: 1st ingredient & amount, 2nd ingredient & amount, etc.

I’m just not sure of the most efficient or practical way to approach it, I know GDevelop isn’t really meant for UI things, so maybe this isn’t possible. I thought about if the Inventory feature could do something similar.

It sounds like a reasonable way to do it. The info from wiki on how array and structure variables work are available here:Variables [GDevelop wiki]

Arrays are like lists: you could use one to contain all of your recipes. Each recipe itself could be in a structure where the key is the recipe name, and the value is… another structure. That child structure could have keys equal to ingredient name, values equal to number of that ingredient.

You can go to the scene menu, to add scene variables. You need to convert the variables to arrays or structures using the small wrench icon. You can add additional items to an array or structure using the plus sign button.

New items can be added inside your events. You can use the “Value of a variable” actions to add new things. For arrays and structures, square brackets [ ] are used to contain the name of the key. So, to add a new recipe it would be something like:

The string inside the [“string”] can be an actual string, or can be the value of a string variable. So if you get input from the user into a variable, you can place it in the structure using [VariableString(inputvariablename)]. You can add a text entry object to you game so the player can type in their recipe names, ingredient names, and amount of ingredients Text entry object [GDevelop wiki]

Next you want to display the text. Did you know you can right click the “add event” in the event sheet to get advanced options? One option is “For each child variable (of structure or array)”. You can use this to loop through the structure, one key at a time, and use other conditions and actions to do something with it (like display it to a text object). You might end up with something like this:

ShowList

You can use similar loops to do other things as well, like saving to json.

It would be nice to also have a drop-down list of recipes for the user to pick from. For me, this is the hardest part. Maybe someone who has done it before can share an idea below?

My best idea on that might be to use the “mouse is on object” condition to detect when user has mouse over the drop down box (a text object). Then, use similar “for each child in structure” loops to create more text objects, one for each recipe, underneath the drop down box object.

Then, use the “Mouse is on object” condition, as well as “mouse left button released” condition, to detect which option was picked.

2 Likes

Child node. Make the parent node (Recipe) an array of structures. Then you just need to jsonify and save one variable - Recipe.

Recipe (array)
  - 0  (structure)
    . Title (string)
    . Ingredients (array)
    . Steps (array)
  - 1  (structure)
    . Title (string)
    . Ingredients (array)
    . Steps (array)
  - 2 (structure)
    . Title (string)
    . Ingredients (array)
    . Steps (array)
  - 3  (structure)
    . Title (string)
    . Ingredients (array)
    . Steps (array)

Edit: yeah I was overthinking this a lot. Luxon5 got it right, I implemented it and it works as intended.

So I made a thing that kinda works. It declares a structure called recipeLists, then the player inputs a recipe title, then when they input an ingredient it forces the title to become another structure and you can add ingredients and amounts dynamically. There’s a bunch of extra stuff in the events for troubleshooting keyboard input with layers, buttons, etc., because the main problem right now is that activating keyboard input will activate ALL keyboard inputs, regardless of if their name and whether or not their conditions were met in order to activate.

Here’s what it looks like (with the keyboard error) and how the structures were ultimately a success:


If I rename the Ingredient it declares it as a new ingredient, which is what I wanted:

The end product is a structure with nested structures of recipe titles, with ingredients and amounts as child variables. However, it’s extremely clunky with the keyboard error and with how the variables are created in the first place, I don’t even know how to even begin accessing them from a different scene. Probably through the For Every Child Condition. I used global variables instead of scene because I wanted to skip the writing and reading for now.

I’ll probably go about this a different way anyway, but thanks for the help, it gave me some ideas.

1 Like