Blueprints are awesome and can do almost everything you can imagine. Their biggest drawnback are that they are significantly slower than C++ code. This does not matter in many cases, and they are fast enough for most things, but sometimes you might encounter problems that are hard to solve without using lots of processing power, and where the relatively slower speed of blueprint scripting will be noticable.
When I began making A* pathfinding in blueprints this was a constant worry, and my early algorithms would often cause a noticable drop in fps as they were running. Asking around on these forums I was sometimes adviced that I should try to split up my calculations over several ticks, but when I was just starting out I was uncertain how best to do this. So instead I focused on making my algorithm as fast and efficient as possible, and in the end I was able to create pathfinding in blueprints that it could calculate any path I wanted in less than a frame, making it less pressing for me to split it over multiple ticks.
As I have continued working on the toolkit, however, I have started working on more complex algorithms to improve my AI, and am also adding support for slower devices such as mobiles. This has brought back the thought of splitting up loops, and so I’ve made a couple of macros that can do this quite easily. I haven’t had to use them yet, but they will be great to fall back on if I encounter a problem that I cannot optimize further.
I thought I should share these with the community as this is something I would have found very useful when I was starting out. I have created macros both for ForLoops and ForEachLoops. The macros have an exec input for initializing the loop which opens up a gate for a tick event. After that the tick input runs the loop a number of loops equal to the Loops Per Tick input until it reaches the Last Index. The macro outputs Loop Body and Index just like regular loops as well as an exec that fires every tick and one that fires when the loop is completed.
I cannot create proper pastebins for the macros as inputs and outputs can’t easily be copied, so here are both the pastebins in Blueprintue form and screenshots to let you build it easily enough. I apologize that they are not the prettiest blueprint graphs. I’m usually pretty good at keeping the graphs tidy, but it’s hard to do with all the execs going back and forth.
(Edit: I’ve made some improved versions that can be found further down in this thread)
A few things to be aware of:
- The macros need to be contained within a master blueprint that contains the IndexTemp variable.
- Multiple loops should probably not run at the same time using the same macro (haven’t tested this).
- The macros should be set up in the master blueprint something like this, with a custom event that can be called from outside:
I hope these will prove useful to someone else who wants to do some heavy lifting with blueprints. Happy blueprinting!