If checks or conditions for custom code to protect for index out of bounds errors

306 viewsGeneral
0

Hi guys,

I have set up a reaction in such a way that when I press a button I reset all sends of my selected track.

The custom code I used is this for the action:
self.song().tracks[self.get_selected_track_num()].mixer_device.sends[0].value = 0
self.song().tracks[self.get_selected_track_num()].mixer_device.sends[1].value = 0
self.song().tracks[self.get_selected_track_num()].mixer_device.sends[2].value = 0
self.song().tracks[self.get_selected_track_num()].mixer_device.sends[3].value = 0
self.song().tracks[self.get_selected_track_num()].mixer_device.sends[4].value = 0
self.song().tracks[self.get_selected_track_num()].mixer_device.sends[5].value = 0

It works but throws errors for the case when the no of sends is less than 6.
My question is can I somehow add some if’s in between such that I try to set the value to 0 only when the actual index is a valid one.

I tried already
if self.song().tracks[self.get_selected_track_num()].mixer_device.sends[0] is not None:
self.song().tracks[self.get_selected_track_num()].mixer_device.sends[0].value = 0
and does not seem to work.

Cheers!

admin Changed status to publish May 22, 2024
Attached Files:
0

Hi noima,
Did you get an error in the log?

There could be some reasons for code not to work as intended:
1) When you use an if statement, there has to be an indent (TAB) on the second line. The inline editor does not allow you to do that (yet). I use Notepad++ (any text editor will work) and copy the code over.
2) try using “!=” instead of ‘is not’
3) if you are using an if statement in a Reaction make sure ‘Custom Code’ is selected instead of ‘Text’ or ‘Number’
4) you might want to try ‘self.song().view.selected_track.mixer_device.sends[0].value’ instead of ‘self.song().tracks[self.get_selected_track_num()].mixer_device.sends[0]’

Hope you get something out of this answer.

admin Changed status to publish May 22, 2024
Attached Files: