[Solved] Lining up instances in a row by their object number variable


Background: I have a simple inventory set up. It creates instances of my inventory slot object on the scene, and changes their animation based on whether the player has collected the item. In a series of separate events I assign a number to each object’s ‘collectionOrder’ number variable. This number is passed from a scene variable that keeps track of what order items are collected in the world. It’s linear and doesn’t adjust if an item is removed e.g. you sell an item to the shopkeep that has the collection order of 5, means that the number 5 is effectively dead and can’t be assigned to an item collected in the world again. Scenarios become possible where I have three items in the inventory with collection orders 1, 5, 7. Currently, instances are lined up in the order that they are created, which is the alphabetical order of child variables that they inherit their animation names from. This all works fine, but I want to enhance my inventory by displaying items in the order they were collected.

Question: How could I rearrange the instances in a row based on the lowest collection order to highest, allowing for any gaps in the sequence? I can come up with XY positions for where things should go, but I have no idea how to tell GDevelop to fill those positions in the order I’d like. Thank you.


How are you storing the objects in the inventory? Structure, array, some other way?

I use a structure variable with boolean children e.g. hasCollected.banana = true. This event is the whole system, apart from some events that I’m using to assign the collectionOrder.

Items are positioned in increments of 120. But I realise I will likely need fixed XY for what I want to do. I’d do this separately, as I can’t imagine how to implement that into the main event.

Edit: the order and liveOrder numbers are not used for anything. Please ignore those.

Edit 2: it’s a very simple inventory that just shows items collected. Items are not interactive, draggable or anything else.

And how are you storing the order in which the objects were collected?

In a scene variable, but it will be global in my main project. The numbers are placeholder, for testing, but they will be set when an item is collected. So they will all be zero when the player begins the game. A separate counter number variable will have 1 added to it each time an item in the world is collected, and that new number can be assigned as the collection order number to the next item. But, for example, the banana might be one player’s collection order 3 and another’s collection order 5.

Then I give that order number to any instance of the inventory object, using a series of events:

That all works fine, albeit long-winded. Numbers are assigned properly. It’s the ‘using the numbers assigned to the instances to line them up’ that I am stuck on.

Have you considered making collectionOrder an array? When an object is collected, append it to the array. This will order the objects for you, and then you just need to iterate over the array children, which will happen from first to last:


Or you can loop over the children using a variable to store the index (remember it’s zero indexed, so the index of the first child is 0).

1 Like

Try what MrMen said and store the item name as strings inside the array childs.

Thanks for your reply. Unfortunately, though I know broadly what an array is, I haven’t used one before so I don’t know how to do what you described. I don’t know how to append things to an array with an event. I have used structure variables before, but I don’t understand how you’re suggesting I use one in your second paragraph. If you have the time to give me a little more detail I would greatly appreciate it. But I understand if not and appreciate your time so far.

Edit: Perhaps an array is similar enough to a structure variable that I can experiment and muddle through, I think. I didn’t know I could add a child variable to an array or structure variable while the game is running. I had this idea that I needed to define all children in the variable editors first. Creating them during gameplay would order them, I think, though GDevelop sorts children alphabetically when I create them in advance, which would mess up the ordering I want.

I don’t quite understand the suggestions, unfortunately, as I haven’t used arrays before. But thank you for your time.

Does the code snippet below help? The collectable objects are in an object group named “Collectable”. If a mouse release occurs on any of the objects in that group, then the clicked object’s name is added to the array and deleted from the scene. Adding object to an array appends them to the end of the array.

If space bar is pressed, the iteration over the array outputs the collected object names to console, and does so in the order they were collected.

Array elements do not get rearranged by GDevelop. When an element is added, it is to the end of the array.

Child order doesn’t matter in structures because you are accessing by key name, not child position.

1 Like

Thank you very much. The screenshot does help. Because of the way I struggle with certain concepts and with anything numbers orientated or math-y, a screenshot is often the point of breakthrough for me. I understand now. Thanks for the follow-up message and for your help.

1 Like