Blueprint to C++ and Engine Freezing?

Hi Alexa, sometimes it can make sense to step back again and rethink before getting deeper and deeper into trouble and ending up in a unecessarily growing complexity.

Just some thoughts here about your blueprint, from which you start. Before converting things 1:1 to C++, it could be worth to check, if the Blueprint code has potential for simplification.

From what I understand, you actually have an array of items you iterate through in order to spawn them at some location?/transform?, picked randomly from an array of available transforms you have stored in some Item Group.

Some points to potentially simplify the blueprint before going on to C++

  • Why do you call a separate function within the GameMode to get a random item from that array? At least it looks like this function does no more than this. Apart from the fact, that a Blueprint Function Library could be a better place to put such functions in, the Array already has a function to get a random element.
  • Next, you get some random array element and then use a loop to do a random again in case the element is already in a list of used locations to avoid, that it is set as spawn location again. Means, the nearer you come to the end of the list, the more iterations will be needed.

Have a look at this simplified blueprint version. I reduced your Items to a string array, also replaced the spawn by simple print string.

If things are transforms, naming them as such is useful, so I did not call it Location. This can avoid later trouble. Could even mean for your code, if you really need Locations only, make the arrays also Location type, saving memory and processing time.

Before starting the iteration, make a copy of the available transforms to choose from.
Simply get a random element from this and then reduce the array of choices by simply removing that element. No loop needed at all and guaranteed unique random spawn transforms.

The item array of course must not contain more elements than available spawn transforms.
In my case, it will return 0 bacause random does not find any entry in the empty array. Something that can be checked.
In your case, this will run into an endless loop - will create problems at runtime.

1 Like