Repeat for each instance - what am I missing?

Hi there!

I’m pretty sure I am misunderstanding how “repeat for each instance” works and doing something seriously backward as it’s doing something weird.

The logic I am building here (in the image) dynamically adjusts the width of a semi-transparent overlay image placed on top of a resource building to visually represent the level-up progress in real time

I have two issues here:

  1. In the attached image the two Debug texts spit out different value. One says Farm, the other Woodcutter. They are both set to display the exact same Variable, there is nothing in between the two Events where the DebugText and DebugText2 sit that would change the value of Group_ResourceBuildings_LevelUpButtons.BuildingType but somehow they return different text :confused:

  2. Which, I suspect, may be causing the second issue - the last Event, the one that is changing the width of Group_Overlay_Progress_Bars to 0, not working at all.

Not sure if these two issues are driven by the same error in code, or are separate, but would surely appreciate some help with this.

Also, any chance someone could offer a NOOB FRIENDLY :smiley: explanation in plain English how this whole “repeat for each instance” work as I think I am seriously lost here? (yes I’ve read the help pages but getting lost in the jargon and mental cuts so need more of a down to earth explanation please!)

(just FYI I didn’t create this whole logic all by myself but very closely worked with chatgpt to get it to this stage as my logic and understanding how coding works is literally -100 but not letting it discourage me from trying to make the game happen!)

1 Like

Hi sunbathingecko, here is a link to the For each/Trigger once guru, user Rasterisko. I didn’t examine the rest of your code, someone else will know if there’s any other problems.

Hi @Bubble , thanks for getting back and sharing the post. I did see it actually and still need to digest properly their explanation. But also I noticed it was more than 3 years old so wasn’t sure if it was outdated and patched up already or not…

I’m curious why you are repeating over each Group_Overlay_Progress_bars, when you don’t reference the progress bars in the actions. The only thing it’s used for is to match the Buildings name with the progress bar’s name. It seems redundant.

And in the second event you update the progress bar widths, but don’t select to which progress bars to apply the width change. So it’ll apply the the width change to all of the progress bars.


The conditions are different, so they can produce different results.

Also, the second event will not kick off immediately after the first event - it has to wait for the timer.


The repeat for each instance is what it says - it iterates over each instance of the object in the scene.

It’s the trigger once that’s the cause of the kerfuffle. It’s actually unnecessary because you have the “LMB is released condition”, which is only true once when it happens.

But the trigger once is causing you a problem. A trigger once is given an internal id by GDevelop, and the state of each trigger once is tracked - is it’s condition are true or false. If, after a couple of game frames, the conditions are no longer true, the trigger once is marked as false,

In a repeat event, if the conditions are met, then the trigger once is marked as true. That means any objects still to be repeated over that meet the conditions will not process the event actions because that trigger once has been marked as true.

Let me know if this is n00b friendly enough. Otherwise I’ll try to explain it a bit better or more succinctly.

MrMen these are some really good questions you’re asking! And yes your explanations are definitely a lot easier to understand.

I’ve removed the trigger once from all Events and nothing has stopped working yet, so indeed it does seem like it was unnecessary.

I’m curious why you are repeating over each Group_Overlay_Progress_bars, when you don’t reference the progress bars in the actions. The only thing it’s used for is to match the Buildings name with the progress bar’s name. It seems redundant.

Tbh… I have no idea what I’m doing as half of it are chatgpt suggestions, hence Im really keen to actually understand how it works!
I am “repeating over each Group_Overlay_Progress_bars” because I was randomly trying different combinations of “maybe I’ll put it here and see if it works. No? ok lets put that thing there…”, sort of a random hit or miss approach which is exactly why I’d really appreciate a human plain English explanation of how it all works. I imagine you’re probably starting to understand the gravity of the situation :slight_smile:

The context - The game is a Castle Builder with all the standard functionality that comes with build, recruit, level up, conquer etc. Ogame style.

Now, what I am trying to create is a single code that uses dynamic variables via Structures so it manages the whole cascade of Events whenever any of the buttons to “Level Up A Farm/Woodcutter/Quary” is pressed, and then obv executes the logic for the correct button / building.

(two super useful gents on this forum pointed me in this direction, now I am trying to understand how to actually do it).

And what I struggle to understand is:

  • how do I actually structure all the Events in that “for each object” loop? As in, what Conditions and Actions do I put in this very specific Event block “Repeat of each instance of”, and what Conditions and Actions I put in the following Event blocks? This is one thing that I don’t understand.
  • Another, how those Events that come after “Repeat of each instance of” Event block connect to it? Automatically? or do I need to connect them somehow to the “for each object” Event block so that the entire Loop understands which Events exactly execute? If so, how do I connect them all?

MrMen, on your question - when you say

And in the second event you update the progress bar widths, but don’t select to which progress bars to apply the width change but don’t select to which progress bars to apply the width change

simply speaking I don’t know how to do it. Any chance you could share step by step how to do it? and why it works like this? I’d really appreciate that!

Also when you mentioned “The conditions are different, so they can produce different results.”, totally valid, and a couple of things on this:

  • In one of the top Events at the very top (the first screen grab) I set the value of “Group_ResourceBuildings_LevelUpButtons.BuildingType” to Farm; and then after there is no other Action that changes the value from Farm to Woodcutter, so I don’t understand why that DebugText2 shows Woodcutter… It implies that the value of that “BuildingType” has changed from Farm to Woodcutter but I don’t understand why, which is another things I am trying to investigate here

Also no idea why but the very last event is not firing at all… still under investigation.


Haha, ChatGPT bases it’s answers on what it’s been fed from the internet. I don’t think it’s worked out what information is valid or what is rubbish. You’d be better off asking here, where most of the members have a clue.


Events that are subevents of the repeat block will get run as part of each repeat loop.In the example below events 1 & 2 are ‘connected’ to the repeat block. Events 3 & 4 are not:

  • the repeat block is only processed if the click event was true
  • event #1 is processed every iteration of the repeat
  • event #2 is also processed every iteration of the repeat
  • event #3 is only processed once the repeat is finished (and the click event was true)
  • event #4 is processed regardless of the other events.


The rule of thumb I use is to first reduce the number of objects to work with using conditions. I also move conditions that will remain the same for all objects (like the mouse button state) to outside the repeat block. I put those with conditions in a standard event. I then make the repeat a subset of that.


The first variable is set to the last level up button object that was processed in the repeat event. I believe the second variable is set to the first level up button object that was created in the scene.


I’d like to first make some suggestions to make your events more easy to read:

  1. You don’t need to use GlobalVariable() any longer. GDevelop will detect the variable scope and use it.
  2. You don’t need to convert a number to string if it’s appended to a string. GDevelop with convert the type if needed. So "Door #" + ToString(door_number) can be written as "Door #" + door_number.
  3. I’d suggest you give add the “Button states” behaviour to all your button objects. And instead of checking for mouse on the object and LMB released, just check if the button is clicked (it’ll be a new condition available because of the behaviour).
  4. Don’t put the button clicked in a repeat. The mouse should only be over 1 button object (unless they’re overlapping - tsk tsk), so you can just have the conditions.

I’ll PM you on this - there’s a lot more to this than what I can glean from the screen shots.

First thing - I just discovered last night how to actually use Instances!! I was not even using Instances for the Button and had two separate Objects as Build Buttons for a Farm and a Woodcutter. I replaced them with a button as a single Object and added relevant Variables as Instances, tested it with a simple text debug that changes depending on which button is clicked… and voila!!!

Events that are subevents of the repeat block will get run as part of each repeat loop.In the example below events 1 & 2 are ‘connected’ to the repeat block. Events 3 & 4 are not:

  • the repeat block is only processed if the click event was true
  • event #1 is processed every iteration of the repeat
  • event #2 is also processed every iteration of the repeat
  • event #3 is only processed once the repeat is finished (and the click event was true)
  • event #4 is processed regardless of the other events.

MrMen this is gold! I somehow knew this is how it works, but whenever I would move Events to Subevents, the logic would stop working at all… so need to explore this a bit more now.

The rule of thumb I use is to first reduce the number of objects to work with using conditions. I also move conditions that will remain the same for all objects (like the mouse button state) to outside the repeat block. I put those with conditions in a standard event. I then make the repeat a subset of that.

This is good. I think bc I’m still learning how to structure the Events within the loop I am unnecessarily repeating lots of Conditions. I’ll play around with that now to see what I can get rid.

I’d like to first make some suggestions to make your events more easy to read:

  1. You don’t need to use GlobalVariable() any longer. GDevelop will detect the variable scope and use it.
  2. You don’t need to convert a number to string if it’s appended to a string. GDevelop with convert the type if needed. So "Door #" + ToString(door_number) can be written as "Door #" + door_number.
  3. I’d suggest you give add the “Button states” behaviour to all your button objects. And instead of checking for mouse on the object and LMB released, just check if the button is clicked (it’ll be a new condition available because of the behaviour).
  4. Don’t put the button clicked in a repeat. The mouse should only be over 1 button object (unless they’re overlapping - tsk tsk), so you can just have the conditions.

This is Gold square!! this is so helpful, I’ve been trying different ways to call in the Variables and been very confused what works when… this clears everything up :index_pointing_at_the_viewer: :muscle: