[Solved] Repeat for each, to populate an inventory

Hello. At the beginning of the scene my game checks what items a player has collected and populates a simple inventory with item icons accordingly.

It all works as I want it to, but…

I am sure there is a better way of doing this than checking each item in a separate sub-event. The children of the global variable inv_items_collected have the same name as the inventory item objects, and those objects are in a group, so I think I have set things up for a ‘repeat for each’, but I’m not quite sure how to put it together.

What would a ‘repeat for each’ for my inventory items check look like? I have a bit of a mental block around reducing repetition that I’d love to unblock. Everything works as I want, but I know my events could be better.

An extra question about ‘repeat for each’: would a sub event of a ‘repeat for each’ event run after the cycle has finished, or would it become part of the cycle. Thank you.

Check this: Variables [GDevelop wiki]
You can replace your objects by the group name, and you can use groupName.ObjectName() if you need to access the name of each object.

Regarding your question, sub-events of a loop are part of the loop.

1 Like

Thanks but I’m still fuzzy on how to make this work. I did this…

…to replace this…

…and it doesn’t work. I just don’t quite understand how to cycle through the child boolean variables and if any are true, create the object of the same name. I read the linked page a few times over the weeks but can’t crack this. Would you mind telling me how my event should look? I think it will click into place for me after that. Words are enough: please don’t spend your time mocking anything up. Thank you.

Well aren’t you just using a regular create an object and not a create an object from its name.

I wonder could you just pull up a for each child of inv_items_collected store the variable in var1 and the name in var2 and do this: if var1 is true, of the group invicons create an object named var2 at positions 1010.

I’m not sure what you meant in your first sentence. I am using object names to create them. It works perfectly. My issue isn’t that my inventory system doesn’t work as it should. I’d like to reduce repetition by using ‘repeat for each’.

I don’t known what your second paragraph means, but thank you for trying. Do I really need to create more variables to turn my individual sub events into a ‘repeat for each’? I think I have all the data I need in the existing variables and children of. I just don’t know how to replace my long-winded way with a nice simple ‘repeat for each’.

I’ve only been using individual events and sub events for everything until now and not touched ‘for each child’ or ‘repeat for each’. Since my needs are very simple, I was hoping someone might look at my events and say how they should be turned into a ‘repeat for each’, if appropriate. I’m sure if that is explained I will get it and won’t ask about this topic again :smiley: Unless it’s not as simple as I feel it likely is.

You didn’t use the dynamic access I linked and you didn’t use ObjectName() either… so no, it’s not going to work.
If necessary, make yourself a sandbox scene to check how each of these work.

Also, you’re doing “for each instance of icons existing in the scene, create icons”. Either you already have all these icons in the scene, and you want to duplicate them, in which case it’s fine, but you should be probably be using object variables instead of global ones, which will allow you to not need dynamic access, either there’s a flaw in your logic (if the icons don’t already exist in the scene, the for each won’t do anything).

I don’t even know what dynamic access is, and the code on that link is a bit beyond the sort of expressions I’ve typed in so far. It’s a bit sparse, that section, for complete beginners to these ideas. I did look at it, but I haven’t had the lightbulb moment yet.

I think I fundamentally misunderstand, or don’t understand, ‘repeat for each’ and thought it might be a relatively quick replacement for my multiple sub events. I’ll do some experiments and crack it eventually.

My game checks global variables at the beginning of a scene to decide what items a player has and creates objects (icons) to populate a container. The icon objects need to be global because the player may have them between various scenes. I assumed the variable that stores what they have would need to be global too, since it’s going to be asking “does the player have the apple?” across various scenes. I can’t see how I could reduce the scope - if that’s the right word - down to object variables if I want this multi-scene approach to the inventory. Again, perhaps that’s just my lack of experience and I’ll understand it more with time.

I’ll mark this post closed, though I am none the wiser than I was this morning (that’s not your fault, and I appreciate your time). At least I now know that I need to get a little further with GDevelop before I even understand certain things enough to ask questions about them.

It’s a strange engine in one way: someone can get a game up and running, looking good and running well, with features they are pleased with and may very well be from-scratch, yet… not understand all that much at all. That’s not the case with other engines. I wouldn’t get this far in Unity without understanding the things we’ve talked about in this thread. That’s not a criticism of GDevelop. I love it. It’s just an observation after 6 months of on-off use. Anyway, it’s not all bad news: by some miracle, I’m not half bad at Yarn! :wink: Thank you.

Ok well i didn’t explain right. The “for each child” is an for each event. You hit the big plus where the other for each event is. You don’t make a bunch of new variables, you just fill out that info one time in the little “select what this for each event is for” text, and it repeats it (loops through) for each child in your structure or array. Maybe it is overkill for you since you only have 3 objects in your structure that you’re creating, so you might as well name them one by one like you’re doing. I have a ton of objects and 5 different tabs to create the first time a player clicks on a machine, and the objects change depending on what machine is clicked. So this special for each event is indispensable for me, and just because it is a for each child and not for each object event does not mean we should discriminate against it or think less of it.

Thank you for clarifying. I’m going to do some experiments and more reading around this topic so I understand it properly. It sounds very useful.

It is super useful you will love it. If I was at home now I’d send you a clip of my events. In my case because I want the objects to be created in the order of what game level they are unlocked, and not alphabetical, the “name” of the child is arbitrary level plus letter like 01a, 01b, 05a, etc and the “variable” of the child is the actual name of the object to be created. But in your case you’d be using the part that stores the name to create the object.

1 Like