To ensure the index is never the same twice in a row, you can add additional checks and logging to verify the behavior. The issue might be related to the logic or the way the previous index is stored. Here’s a revised approach to ensure the index does not repeat:
-
Persistent Storage for Previous Index: Make sure the previous_index persists correctly between function calls.
-
Additional Logging: Add print statements to debug and verify the logic.
Here is the updated script:
# Import the necessary module
# Ensure these are stored at the module level to persist between calls
max_index = 2 # Maximum index, inclusive
def onValueChange(channel, sampleIndex, val, prev):
new_index = previous_index
# Debugging: Print previous index
print(f"Previous index: {previous_index}")
# Ensure the new index is different from the previous index
while new_index == previous_index:
new_index = random.randint(min_index, max_index)
# Debugging: Print new index
print(f"New index: {new_index}")
# Update the Switch TOP's index parameter
op('switch1').par.index = new_index
# Update the previous index variable
previous_index = new_index
# The function onValueChange is triggered when a value changes in the monitored CHOP
# Make sure to set the channel's parameter to value change in the CHOP Execute DAT settings
-
Create the CHOP Execute DAT:
- Right-click on the CHOP receiving your OSC signals and choose
DAT -> CHOP Execute.
-
Set the Callback Functions:
- Set the
off to on parameter to your desired trigger channel.
- Leave the
while on and on to off parameters unchecked unless you want to handle those cases too.
- Ensure
Value Change is checked to trigger the script when the value changes.
-
Script Logic:
- The script imports the
random module.
- Initializes
previous_index, min_index, and max_index variables at the module level.
- Defines the
onValueChange function to execute when the CHOP value changes. This function generates a new random index ensuring it doesn’t match the previous index and stays within the specified range (0 to 2).
- The new index is assigned to the switch’s index parameter, and
previous_index is updated.
- Added print statements to help debug and verify that the indices are changing as expected.
-
Link to Switch TOP:
- Ensure the
op('switch1') in the script matches the name of your Switch TOP. If your Switch TOP has a different name, update the script accordingly.
- The print statements will output the previous and new indices to the Textport. Monitor these prints to ensure the logic is working correctly and that indices are not repeating.
By following this updated approach, you should be able to ensure that the indices do not repeat consecutively, and you can use the debugging output to verify the behavior and identify any potential issues.