Making processes less intensive

I have some code to generate a big cube from several (currently 729) cubes as seen below

This seems to be a very resource heavy task and I am wondering how I could make it less intensive to run,
any advice?

Hi Jandre,

Nesting for loops within for loops is going to cause a hitch depending on how much is happening especially on construct. I would recommend restructuring your construction script so that the loops aren’t nested. The reason this may be an issue is because when you make any kind of change the construction script will update running through all of those loops and technically it has to run through the code before it can continue.

Keep in mind nested for loops aren’t always bad but in this instance I wouldn’t use them since something as simple as translating your BP across the scene will cause the construction script to fire off continuously.

If you want to keep things as they are you can also try using a Boolean and a branch node at the beginning to turn on and off the construction scrip so that it isn’t updating when not needed. Just expose the Boolean and check it on when you want the cubes to generate.

,

for the advice, would a sequence work better for this?

I see Unreal’s video on procedural generation mentions this, and using mesh instances instead of static mesh components, and it reduces the lag by a lot.

That may depend on the scenario. In this case if you plan to spawn several hundreds of cubes that could help out your situation. Just add an instanced static mesh component. To create additional meshes drag off from the reference to instanced static mesh component and use the “add instance” node then set the transform.

You will still get a performance hitch if you are continually generating 700 + meshes on construct since construct fires off every time you make any kind of edit the Blueprint within the scene (this can be like having your nodes hooked up to an event tick node sometimes). That is why I recommended using the Boolean to turn on the construction of the cubes so that you may make changes to the BP freely and then when done turn the Bool to true allowing the cubes to generate. Making the Boolean a public variable will allow you to check it on while in the main viewport.

Thank you very much this helps a lot, and I am using your idea for the boolean toggle for generating this, it works much better now.