Casting sometimes "Fails" when actor actually exists in the world

Okay so getting straight to the point:

I got a simple blueprint that spawns a room and when the room spawns, it will set 6 different ChildActor classes randomly from the game instance’s array using ForEachLoop.

And it actually works, no questions asked.
When ForEachLoop completes, the blueprint casts the existing door that is in the room to check if it’s locked, if it is, it will pick a random lootable out of the 6 (which is either drawer or bookshelf) to spawn a key in as seen here:

Most of the times it normally works, it checks for the door and if it is locked it spawns the key.
Problem is that even though if it picks a random lootable out of the array which EXISTS in the world, it will sometimes fail and the key doesn’t spawn even though the cast node to door detected if the door is locked, which means that the player is stuck in the room unable to go proceed any further. This can be seen here:
image_2024-10-17_010149691
It CHOOSES and DETECTS the actor but still manages to fail the cast? I don’t think that is how it’s supposed to work.
Even adding delay after ForEachLoop completes doesn’t fix the annoying issue. I also tried like my 3 other methods and yet no luck towards fixing it and I am left grey in this situation.

1 Like

The random node is called every time there is a connection. In your case here, 3 times

If you want things to function predictably, then just choose a random item and put it in a variable, before going into the end of the loop.

aaand that managed to fix it, thank man
I wonder why I haven’t thought of this before…

1 Like

It’s not obvious :wink:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.