Help me understand array/structure for iterating and sorting. For dummies [CLOSED]

I’ve been trying to understand iterating with arrays/structures with “for each child variable (array or structure)” for two weeks now, to no avail. I’ve scrolled through a few people’s topic about array but they either have no idea either or it’s too advanced for me and are missing too much how-to-steps.

If there are good tutorials for beginners (related to iterating or using arrays/structures) that you can link them for me?

These first two screenshot belong together.
Here’s some screenshot post of ‘how far my understanding got’. Which isn’t far.


These second two screenshot belong together. Basically the same effect as previous screenshots


These four screenshot belong together. Capable of adding new structures but not capable of updating the new structures inside the parentStructures2




I also have been trying to figure out how to sort or rearrange and iterate it in correct order I want. But I have no clue how to make it work.

I know this is a demanding request, i’ll just be glad if you can find the legendary GDevelop tutorials about using arrays/structure iterating, sorting, etc… that is understandable.

I feel your frustration. Variables are far from no code. Most of GD can be done with clicking and little typing but arrays and structures are more difficult. I can’t stand how structures are sorted alphabetically. It makes things more complex.

Instead of 2 structures, I’d recommend an array and a structure. Arrays don’t get sorted alphabetically.
When you use for each child with an array, the first return variable is the child while the second variable returns the index. You can use that value instead of adding an index counter.

These are the variables.

And these are my events.

My results:
image

If you add the child to the structure through actions, the children don’t get sorted. So, if possible it might be easier to add values through events. You can still set up the structure, so you get the autocomplete but you could clear the structure and then add them in the order of your choosing. Your original concept would then work. You can assign each one to either a value or just 0. Either way, it would create the structure in that order.

Events

variables

I don’t know if that answered all of your questions or just created new ones. I’m willing to help you if I can. It’s not an easy subject. There were a lot of events and I admit it, I just skimmed it. I felt I could address the primary issue and then go from there.

2 Likes

Here is a way to do your more advanced structure.

Variables

Events:

Result:
image

1 Like

Thanks for the reply! I’m indeed frustrating at trying to comprehend utilizing iterating arrays / structures. I’m just blankly staring at my screen event codes, my brain overheating in trying to comprehend what-is-going-on and how-to-do-it.
And although it hasn’t answered the questions I need about automatically sorting the iteration once new childrens are in a structure.
The references you gave are still good in helping me understand arrays.

You did say; *

“If you add the child to the structure through actions, the children don’t get sorted., – you could clear the structure and then add them in the order of your choosing.”

*. So i think you understood what I’m trying to use it for.
But I don’t know how to approach it. Unless I could selectively choose which children to remove and re-add inside a structure OR simply rearrange the children, I won’t be able to sort things. I only know how to add them in either the first or last child position (with in-build GDevelop actions), once added their ordering is fixed (according to my lacking knowledge.).

I’m currently thinking of events for sorting, i’m thinking about giving each children structure an ‘sortNumberIs’ number variable.
During the iteration I will create an panel object with the same ‘sortNumberIs’ variable to corresponded and ‘link’ with the NewText objects by having the two variable be the same number.
Then using those ‘sortNumberIs’ variable I will sort them using the ‘for each instance of —’ event.

This is just a rough idea I have in my head though, whether it is plausible in sorting arrays, i don’t know yet. If you could show me a better way, I’d appreciate it! Until then I’ll just have to keep experimenting, because the idle-game project I’m making really requires optimization with iterations.

1 Like

GD doesn’t have any built-in sorting capabilities. It doesn’t even allow you to compare strings with greater and less than. I put in a request for that before. You can’t even create a simple bubble sort without being able to compare text.

Your best option for sorting would be to use Javascript. The array tools extension might help although it doesn’t have a text sort option. It can be modified or used for inspiration. I’ve never tried sorting a structure. You might be able to create a numerical cross reference and sort the numbers with the array tools. Again, that’s just a workaround the more bandages and duct tape you add, the uglier the code gets.

I can’t think of an easy workaround. Your presorted array would be a good start. You could go through the array and check if the structure has the animal or if the animals quantity is greater than zero.

There are ways. What is your ultimate goal. Is it a way to display the list alphabetical or a particular order. You posted a lot of images and my eyes glazed over a bit. It’s tough to read could without knowing the context and the bigger picture.

Is there going to be 1 entry per animal or multiple?

Hey, sorry for the late reply. I was quite busy with other things.
With the few free time I had I tried to solve iterations, I made it work. Probably.
I think of giving up on sorting though and force players to unlock materials linearly.
Still, if you know how and can show me an example i’d appreciate it!

Here’s screenshot of the events of how I made it work.



The results looks decent, I can make player unlock new materials now.
image

To give you context about the idle game it kinda goes like this:
Player unlocks and gathers materials,
refine/craft/sell/buy new materials.
And material lists (UI Sidebar) gets updated.
Here’s how the scene variables of the main projects look like. I haven’t fully added everything yet. But it’s going to be a decent amount.
If the untitled project had children in children, then the main project would be children in children in children.
image

I think, i got a simple understanding of iterating by now and can solve some problems!
Thanks for the help.

1 Like