Set variable not being recognised some of the time?

So, I have two actors (called “Waypoint_Actor”) of the same class in my scene that have the boolean variable “IsPickable?” Under begin play in these actors they add themselves to an array located in the player controller. The “IsPickable?” variable is exposed so I can change them once in the scene and that’s what I did. One is true and the other is not true. You can see in the 2nd image that I have one selected and see that it is not true.

In the 1st image you can see that in the player controller when I hit Z one of the actors will be picked at random and if the variable is true it will print out the name of the actor. So in this instance only one of the actors has this variable set to true, but as you can see in the second image both are being printed as if they were true? Why are both being seen as being true?

I had a much more complicated version of this where AI would randomly select one of the “Waypoint_actors” and move to it which would remove it from the pool of “pickable” way points, but AI would keep going to points that were not “pickable” so I scaled it down to this and I’m still getting the same issue. Something is causing the not pickable actor to be seen as pickable some of the time? You can see in the 2nd image that when I hit Z the false option does occur some of the time but then randomly the not pickable actor is seen as pickable again?

Thanks for any help that can be given.

Hey minirlz,

This is a deceptive aspect of visual scripting that can go under the radar until it causes problems such as this.

What’s actually happening in your blueprint is the GET function is getting called two separate times, once when checking checking the IsPickable bool, the second when getting display name to printstring.

328642-callstackbreakdown.png

So what does this mean? Each time it is called it will chose from a random index, getting a random Waypoint_Actor. So the Waypoint_Actor you check the IsPickable value of, might not be the same as the WaypointActor you get display name of, due to the non-deterministic aspect of the index chosing.

328643-nondeterministic.png

THE FIX: Once you’ve chosen a Waypoint_Actor, store it in a variable so that the GET function is only called once. From this variable reference, things like IsPickable and GetDisplayName will always point back to the same Waypoint_Actor.

Hope this helps :slight_smile:

Thanks for the explanation good to know the background info!

I’ve ended up separating the “search” and the “action” into two different key presses. So Z would select a random actor from the array and then I stored it in a variable like you said and then with a second key press it would use the stored variable to print the info. So I was able to go back and do what I was originally doing with all that in mind!

Thanks again for your help!