How to add children to structure variable via action?

How to add children to structure variable via action?

[EDIT - see my last comment where I managed to create children in structure variable via actions]

Hi, a newbie here. The end goal is to be able to compare 2 lists and validate whether the player chose right ingredients as per recipe (compare given list vs dynamically generated list based on player’s decisions).

  1. There is a recipe with list of ingredients: variable called “RequiredIngredients” with Structure data type. This list will be compared with the list of ingredients that the player picked.

  2. 2nd variable is called SelectedIngredients also with Structure data type (because I dont want to be limited by the order like in Array - the player can pick ingredients in random order).

Now, I want SelectedIngredients list to be populated when the player selects an ingredient (e.g. onions). I am trying to find a way to put in action to create a child in that structure variable (e.g. add child called “onions”).

However, I cant seem to find any options to add children, append, add or modify the SelectedIngredients variable structure. I am able to add text but the action changes the SelectedIngredients variable into Array.

Below screenshot (RequiredIngredients I called it here = “Pepper Onions Pizza Recipe”)

Thanks for any guidance.

I have a similar problem, but your topic wasn’t suggested when I posted mine.

How do I allow the player to create entire new variables - How do I…? - GDevelop Forum

No answers yet, here, either.

You don’t need any special way to add a structure variable. You just set it’s value to a number, string or boolean. Unlike an array, you can’t just add the item it needs a value.

It helps to have the structure setup in the respective variable screen. You need to at least have the variable name setup as a structure.

Here’s an example with 2 buttons and a sprite.
Click the one to add or set/change the value.
The other button removes the child.
The last event is just to test it. If it exists then it spins the green sprite.

You can set a variable with
List.Apple = 7

Or dynamically using a string or a variable or any expression that returns a string. You can also build a string using the plus symbol and another string or expression.

List.Apple = 7
List[“Apple”] = 7
List[VariableName] = 7
List[“Apple” + VariableName] = 7
List[“Apple” + ToString(number)] = 7

3 Likes

Thanks Keith for the information.
[EDITED - removed pointless comments]

My end goal is to be able to compare 2 lists and validate whether they have the same items (ingredients). So, what would be the most efficient way to achieve that? Strings seem to be limited by the order of items/ingredients (the order of items selected by the player will be random).
Im bit lost on how to do this. Ive looked into combination of “Compare Two Strings”, and creating temporary arrays via “add for each child variable” for both variables (lists) etc.
Do I actually need the 2nd list (SelectedIngredients by the player) set as structure? Any ideas?

I would need to see the events. You seem to have a structure with SelectedIngredents.Onions with a value of Onions.

The issue with comparing 2 lists is unless you know how many ingredients, you would have to compare the ingredients to the recipe and the recipe to ingredients. You need to know what is and isn’t in the player’s list. I would include a count or manually count it. Unfortunately, you can get a count the number of children in an array but not a structure. For a structure, you’d either have to count it yourself or add a count to the structure. Maybe use a structure with a structure. It might be easier to use an array for some parts.

As much as I want to help. It’s a bit much to think about at the moment. Here are some tips.

You can go through the player’s list and compare it to the recipe. Using a for each child of the user list and does item exist in recepie. You can use a variable to count the number of values in the player list and in the recipe. Then compare the number of items that match to the number in the recipe plus check how many items are in the players list to make sure they didn’t pick extra items.

Matches between user and recipe = number of children in recipe
total children in user list = number of children in recipe

If possible I can help later or someone else can.

OK thanks for your comments. Unfortunately counting method will probably not work, since each item (ingredient), although pre-defined and their total number is limited, each one is unique, so the player could still pick the same number of ingredients as per recipe but the wrong types. I just need to find a way to check if each child from recipe list exists in the list created through player’s actions (contain / equals etc.).
I guess I will have to learn a lot more about Gdevelop functionality before I can crack this challenge.
Thanks for the extra info though.

1 Like

Variables are tougher to learn, especially arrays and structures, but it’s worth it.

We can help you along the way but people here don’t always have time for in-depth situations. If I get the time, I’ll try to put something simple together. It’s a new concept to me as well. Your first idea isn’t always the best idea and your method will probably change along the way.

Are you sure you need a structure? If you just want a list of selected ingredients, an array would probably be easier.

Structures are only worthwhile if you really need to be able to access data by name.

I think my issue was I was trying to create your game instead of helping you understand a concept. I tend to dive in too deep and it seemed overwhelming. It’s not your fault.

This is an example of using a for each child to compare 2 structures.

Here’s my test project. There are no resources, it’s just a JSON file.
Click the green [code] and select [download zip]
https://github.com/doug13579/gdevelop-compare-structures

I don’t know how items are put on the list. If multiple items of the same ingredient need to be added then maybe you could increase the value of the structure value. You can’t add a child to a structure multiple times. It would just replace the value.

2 Likes

Thanks a lot Keith for the effort and I appreciate your attempt to prepare what I was trying to build. Your example gave me some ideas to work with and after discovering some stuff via Gdevelop’s UI, which I couldn’t see before, I managed to set up what I needed.

So,
I can create lists to compare as Strings, Arrays or Structures and I tried all 3 types. However, at the end structure is the best solution considering my requirements.
This is simply because I can easily remove children from the structure and, if needed, quickly rebuild it dynamically. The player will be able to check (add), uncheck (remove) and, if necessary, check again the same item back and forth.
Also, I did not have to set up the structure children before (only the target list has to be pre-defined but player’s choices list is blank) I left it blank and actions create children automatically. (see screenshot no.2)

Screenshot 1 shows 2 possibilities (string and structure) to validate (compare) against the target list (array uses the same logic as structure). However, as I mentioned, structure can easily remove items (children) which is what I needed as well.
I used extension called “Arrays tools” but I guess the condition you used (“exist”) will work the same way.
AllIngredientsMatch_Required_vs_Selected”: 0 = success/pass, 1= failure/not pass
Output” is just for information (possibly for text object) but I haven’t finished tweaking it.
Second loop (inversed) is so that if a player adds unnecessary (extra) ingredient, it will not pass the test. Objective is: these lists have to be identical.

Screenshot 2 shows how I add and remove items (actions for strings and arrays were just for testing purposes). As mentioned, no need to populate srtucture before actions. You just need empy structure variable and populate it via action:
Change the text of scene variable “SelectedIngredients_Structure.Peppers”: set to “Peppers”. Gdeveop will create child “Peppers” for you and add value “Peppers”.

2 Likes