Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Bolt Script - Output Error with List Get Item When Using Random Range

Discussion in 'Visual Scripting' started by C_G_2021, Feb 14, 2021.

  1. C_G_2021

    C_G_2021

    Joined:
    Feb 14, 2021
    Posts:
    2
    Hi all!

    I've run into a strange issue that feels like a bug in Bolt (maybe?) and I'm hoping that somebody here might be able to help.

    I'm using Random Range to randomize the Index of a list in List Get Item. The list contains 3 items. I'm connecting the output of the List Get Item to 3 different boxes in my script. 2 of the connections output as the same list item, but the 3rd connection outputs a different item from the list. The 3rd connection is linked to On Button Click. If I don't hook up the Random Range and set the Index in List Get Item, the 3 output connections all use the same item from the list. Also, all 3 connections use the same item from the list the first time the script is run. If you leave the scene and return, the weirdness starts.

    It feels like something weird is happening with that Random Range and connecting the output of List Get Item to On Button Click. It feels like it should work properly. Have I missed something? I've used Random Range elsewhere in the script without issue. I've also tried deleting that box and adding a new one. I get the same issue.

    Any help is appreciated.

    Thanks!
     

    Attached Files:

  2. PanthenEye

    PanthenEye

    Joined:
    Oct 14, 2013
    Posts:
    1,825
    That's standard Bolt behaviour. You have two different flows connected into one via the Get Item unit, which causes the confusion.

    Whenever you click the button, it retroactively triggers random range and gets a new item. Meanwhile, the old flow displays cached connection values you got the last time that flow was activated. Clicking a button does not trigger/refresh the rest of the connections. So the button connection refreshes and gets a new value while the other connections display old, cached values.

    If you want the button to have the same item the below flow connections have, set a variable and use that variable as the On Button Click input. You should typically avoid connecting multiple green event units in the same flow unless it's at the very start. And you have to intentionally cache your values in variables, Bolt won't do that for you automatically, it will execute the logic again by triggering it retroactively.
     
    Last edited: Feb 14, 2021
    C_G_2021 likes this.
  3. C_G_2021

    C_G_2021

    Joined:
    Feb 14, 2021
    Posts:
    2
    Thank you so much for your reply! That's very informative! I had no idea that On Button Click would retroactively trigger Random Range.

    I've tried what you suggested to cache the info output by Get List in a variable using Set Variable (a Scene variable), then linking the Get Variable to On Button Click. For some reason, the Get Variable is only outputting the info I had set in the variable initially. It's not getting updated with the new info when I use Set Variable in the flow. This is only happening when connected to On Button Click. When I use Get Variable in the rest of the flow, it contains the updated info. Am I setting the variable incorrectly? Is the Get Variable connected to On Button Click not getting passed the updated info from Set Variable? I also tried using the Cache unit before setting the variable, but that didn't change anything. I also am now using the Round unit to round down the float output of the Random Range to an int.

    Is there a way to gate/block the button press from triggering the Random Range unit retroactively? Is there a different way to randomize an image on a button, then listen for that specific button to trigger an event using Bolt? Sorry, I'm new to Bolt. Just trying to wrap my head around the intricacies.
     

    Attached Files: