I’m using visual scripting, I have a For Each loop that iterates through a list of objects.
The loop is directly connected to a custom function. The custom function spawns an actor in each iteration and manages its rotation and location. I wanted to print an integer at the start of the function (to count iteration counts), and also print the final position of the actor as the final node of the function.
However, when I test this out, each time it messes up something and cannot print some of the location variables as they are empty. I’m suspecting the for loop trying to run the next iteration before the previous iteration is done. Here’s a screenie for clarification.
The function call in the Blueprint:
I don’t think for loops work asynchronously in UE, what might be causing this issue? It’s a really simple function and loop. I wrote the logic as if I’m working in any programming language.
That array is a class type array and it is predefined manually. It has several actor blueprints stored in it and it pulls a random one with each loop iteration. The integer is just a count number it never misses. What misses is the location vector. The location vector is calculated depending on some factors. Ill try to post the whole function here so it is more clear.
I added printing the random element display name and it works fine btw
There’s nothing wrong with the loop itself. The way pure (they’re always pure) random nodes are evaluated is somewhat tricky. What and how is accessing those random classes?
The integer is just a count number it never misses.
Not what I meant.
This integer. It returns the accessed index. Check it out like so:
LogBlueprintUserMessages: [LevelGenerator_2] 0
LogBlueprintUserMessages: [LevelGenerator_2] ConnectorPiece_2_C_0.Capsule3
LogBlueprintUserMessages: [LevelGenerator_2] X=0.000 Y=10.000 Z=0.000
LogBlueprintUserMessages: [LevelGenerator_2] 1
LogScript: Warning: Script Msg: Attempted to access random index from empty array!
LogBlueprintUserMessages: [LevelGenerator_2]
LogScript: Warning: Script Msg: Attempted to access random index from empty array!
LogBlueprintUserMessages: [LevelGenerator_2] 2
LogBlueprintUserMessages: [LevelGenerator_2] ConnectorPiece_2_C_1.Capsule
LogBlueprintUserMessages: [LevelGenerator_2] X=420.000 Y=0.000 Z=0.000
The error occurs when a placement of an actor is failed. I do not know what is causing this issue. Here are the random parts which I have tested and the names are printed out correctly even if the error occurs for that iteration. As can be seen above
(loop count,
random element name,
final vector location)
I did check the first random too, that takes a random elemnt from the predefined array.
I do not know how to paste the whole Blueprint here and I couldn’t find amethod so here is a few sections.
EDIT: Oh I understand what you mean now. It returned a -1 yeah. I removed the other debugs again, so here it is:
LogBlueprintUserMessages: [LevelGenerator_2] 0
LogScript: Warning: Script Msg: Attempted to access random index from empty array!
LogBlueprintUserMessages: [LevelGenerator_2] -1
LogScript: Warning: Script Msg: Attempted to access random index from empty array!
LogBlueprintUserMessages: [LevelGenerator_2] 1
LogBlueprintUserMessages: [LevelGenerator_2] 1
LogBlueprintUserMessages: [LevelGenerator_2] X=-210.000 Y=200.000 Z=0.000
LogBlueprintUserMessages: [LevelGenerator_2] 2
LogScript: Warning: Script Msg: Attempted to access random index from empty array!
LogBlueprintUserMessages: [LevelGenerator_2] -1
LogScript: Warning: Script Msg: Attempted to access random index from empty array!
I dont want to spam this area with messages as I know in forums double commenting is not appreciated. But probably editing posts do not send notifications. So I added an edit to my last post, and also, should I just use random integer to pull data, is the Random node not reliable? Or is the array somehow not working correctly?
I am so sorry, I found out the issue. I should’ve looked harder.
The array is filled by the randomly picked Actor’s capsule components, which is picked by the first predefined array. I have to manually place the capsules and the BP procedurally generates the level. I forgot to add the capsules to one of the actors. I feel so stupid. Sorry for wasting your time. But your debugging helped me out quickly so thanks for that!
Yup! Removed it too! I thought removing it caused the errors The thing is the actors are randomly picked so rarely I didn’t get errors, I related the errors to removing that node haha. Everything is smooth rn thanks to your help!
I understand. Then I should always store the random element in a variable right after it is returned and use it for any further actions. Or I can simply get a random one by generating a random integer less than array’s size.
These are the two ways I can think of with my limited knowledge of UE nodes. What would you suggest doing instead?