How to loop through a structure without creating an extra variable

I want to loop through a structure variable and edit the child variables within the structures. This includes removing child variables as the player progresses through each in-game year. BUT there’s one problem I can’t fix. Looping through the structure creates an empty child.

Here is a screenshot of the Gdevelop code and the preview, since new users cant post multiple media I had to put them together:

I’m using the usual method of having an index variable that increments as the loop progresses. I’m also using the simple scrollable list extension to display the structure variables as a list.

The extra child variable does not appear when structure variables are edited in the chronological order that they appear in the scene variable menu. However, this method won’t work for the game I’m working on.

I’ve tried many different methods. One involved copying the scene structure variable and all its children into another variable. Editing only the text child variables I’ve indicated, then moving the edited variable back into the main scene structure variable. But after a while I got an error message. I believe the error message is because of the extra child that appears.

Why dont you use the “for every child of structure or array” event?

1 Like

I think the issue is 2-fold. First, if there’s no match then it’s not increasing the variable Variable (I wouldn’t use variable for the name of a variable. It’s confusing and not descriptive. I usually use i or index) Since it never increases the variable if there’s no match, it gets stuck in an infinite loop and will never leave the while event. You can probably put the add 1 to Variable on a line without a condition at the end and just increase it once.

The other issue is since each line is increasing Variable then if it’s “husband” or “lover”, it will increase Variable 1 place above the last index but the loop continues through until the end. The next event will check the value of a non-existent index but because of the way Gdevelop works, it will create an additional item. moving the variable +1 to the end, will also fix this.

If the action is the same for each event, you can use just one event with the 3 conditions inside an or condition. The add 1 to variable would still need to be on a line without a condition.

1 Like

I’ve tried it before but it produces the same results unfortunately.

Thank you, I gave it a go and the extra variable doesn’t pop up anymore. But for some reason I cant delete any child variables from a structure while iterating through the loop. Nothing happens.

I’m assuming it’s because I’m using the child count as a condition for looping but can’t think of a way to get around it.

My game idea is to delete child variables that are no longer necessary because they don’t apply to the current generation. (As the player ages they play as a new generation). Any suggestions would be much appreciated.

Here is what the current condition/action looks like (I’ve changed the Variable containing the index to ‘i’ as well.

I’ve tried something similar on a different structure variable and it works (although it leads to an extra empty variable being produced again)

Your variable is similar to an array except you’re using a number as a string. So, it’s a structure not an array. They’re both extremely similar except how arrays are indexed.

You might want to switch to an array of structures.

The issue with a structure is if you remove a child then a counter variable won’t work because the children won’t be sequential. Instead of 1,2,3,4 you might have 1,3,4. The counter would only go from 0 to 2 but since 2 no longer exist, it would create #2 as the default type like before.

Arrays automatically shift children or indexes. So, if you delete index 3 then the values under index 4 would be become 3 along with all the other indexes. They’d all move 1 index lower.

This uses an array of structures, it uses repeat and the variable i, it deletes any indexes with Mom. It then displays them in a text object using for each child It decreases the i variable if it deletes an index otherwise when the other indexes moved up a place the next index would be skipped because it would take the place of the index that was just removed. It would still repeat the same number of times, it just might check the same index multiple times if indexes are removed. The other indexes are moving towards that spot.


Think of it like a building and each index a floor. The variable i is the floor number. If the floor 3 had “Mom” then it magically gets removed. All of the other floors drop down. The floor above you dropped down around you. Since floor 3 was removed, the 4th floor is now 3.So, you need to check the new 3.

1 Like

Thank you, It finally worked!! I changed the structure to an array and subtracted 1 from the index counter Variable like you explained.

1 Like