Why does execution continue when a delay is hit

I am working on a tile based game. I have a board generator and on the hard level I have to create 900 game tiles and add them to the screen. This takes about 3 to 4 seconds to complete. I started moving the UI code to C++ but ran into tons of issues and read that UI in C++ isn’t recommended as its bug prone and difficult to work with. So I moved it back to blueprints and to avoid hogging all the CPU for 3 to 4 seconds I implemented a system to layout the board one row at a time with a short delay between to free up the CPU. This works beautifully and the board is laid out one row at a time and with the delay the FPS still hangs around 50 while doing it so sweet.

But I am running into a problem with the delay nodes. I layout a row, delay for a short period and layout the next row and so on. The proplem is as soon as I hit the first delay node, execution picks up AFTER the event containing the delays finishes.

  1. While debugging, this breakpoint is the first one hit as expected.
  2. Execution then enters the Layout10x10EasyBoard event as it should where a delay is triggered between rows.
  3. Before the delay is finished execution picks up here and continues down the line before the delay even finishes.
  4. Then this breakpoint is hit and we continue showing the rest of the game tiles.

It’s my understanding that execution will stop at a delay so what I think should happen is the Layout10x10EasyBoard should finish execution before picking up at number 3. But that is not happening. The code at the end of the line requires all the tiles to be created before continuing and since the delay seems to have no effect on execution, that code is not working correctly. Does anyone know why the delay is not stopping execution?

Hey there, if you are sure that the event is executing then check the value of the delay variable. Try setting a manual delay instead of using a variable (something like 10 seconds to be evident).

I changed the delay to 2 seconds, and one row is laid out every 2 seconds as expected however the execution is still the same, the first delay is hit, then execution goes immediately to #3 before the delay is finished. Then after 2 seconds execution goes back to #4.

That is doing what it’s supposed, i know it doesn’t seem to be very logical but delay nodes can’t block the execution thread completely or else you would run the game rendering 1 frame per delay duration (if your delay was 2 seconds then you would be running the game at 0.5 FPS). I’m not sure exactly how delay works inside but think that when you do a delay it kinda schedules the rest of the functionality on a separate thread and the top code continues as normal on the main thread. To make that work, i think you have to structure the code differently.

How can it if its all the same execution line? I’ll try and see if changing things around to ensure the tiles are created before continuing but I’m confused how execution can jump around like that. The way I understood it is when a delay is hit, it pauses in a different thread so it doesn’t hog the CPU. Then after the time is hit the execution continues out the completed pin. So I would think the delay would cause Layout10x10EasyBoard to not return until the delays have completed.

So I added an event dispatcher to call when tile creation is finished. And it does work now the code that has to wait till all tiles are created doesn’t run until all tiles are created but this is definitely an unexpected behavior. Very good to know thanks for the assistance.

Good to know :slight_smile: