HI, this is X-Raym, for the 8th part of our ReaScript Tutorial.
Scripts can define new values to REAPER objects.
But you have to be careful.
Because these values can be invalid.
No error message may appear,
but the value inserted in the project will be invalid.
This is not fatal, be reassured,
it cannot corrupt a project and make it unable to be open.
We will take back our script which modify fade-out length
In order to show you how to prevent this kind of error.
In order to ease the understanding of the phenomenon,
I will simplify our script.
To do that, I will not run the Main function
but a new function that we will call Test.
I declare this function before it's execution.
Function Test()
And this function will only act on the first selected item.
0 for the current project,
0 for the first item.
I check that there is at least one selected item.
If there is one item selected
Our script will execute a custom function,
I call it SetltemFadeOut
She will have as parameter
an item
But also an item length
and a new value of fade out length.
We will get the item length thanks to this function here.
I replace item by first_sel_item.
We will write the new value here...
and it will be 0.
Now I declare this function
It has 3 parameters
The first parameter is an item.
The 2nd will be a length I will name it item_len
the 3rd parameter a new fade-out length value.
This function will define the fade-out length of the first selected item.
I copy this function here.
I replace the variables by the parameters of our function.
I add UpdateArrange
A define a fade on our item and if all happen as expected,
The fade-out length value of the first selected item should be equal to 0.
I run the script.
All happen as expected.
Now imagine the following thing.
If fade_out_new_value is equal to 100
Knowing that this value is in seconds and that the item length is only 1 second.
Let's see what happens.
As you can see the fade-out length seems to match the item length.
However, if I display the item properties, I can see that its fade-out length value is equal to 100 seconds.
And you can see that the mouse doesn't behave like usual, because of this invalid value.
Usually, you can see the fade icon-cursor being displayed hover the fade.
But in our case, there is a bug because of an invalid value.
So, we will use this function here to prevent this kind of error.
We will write the following thing.
If the fade length is bigger than the item length,
then the fade-out length will be equal to the item length.
I run the script.
Nothing change visually, but it seems that it is corrected.
I check the item properties.
The fade-out is not bigger than the item length.
Let's see a more complex case: what if there was a fade-in?
I run the script.
It seems to be good
but as you can see we can feel a problem.
If I check the item properties I can see that the fade-out as an invalid value
because it is as long as the item, and this is not possible as there is a fade-in.
So, I will write the following thing.
Here I will get the fade-in length value.
The parameter is "D_FADEINLENGTH"
and I write the following condition:
If fade-out length is bigger than item_length minus the fade-in length
then the fade-out length will be equal to item length minus fade-in length.
Rather that making the calculation twice I will write a max_value variable
and I place there here.
Oops, small mistake here
I run the script.
Nothing has visually change, but if I check the properties,
We see that the fade-out length has a vlaid value.
I can now use this function here
in our Main function.
Rather than setting a new fade-out length value directly here,
I use our function
which take as first parameter: the item
2nd parameter: the item length
which has already been calculated here
and as new value, the fade-out length of the item udner mouse
It was here.
I don't need the test function anymore so I delete it.
I replace the execution of the Test function by Main.
Main works on item in time selection on selected tracks.
I define a big fade-out length on our reference item.
I make these items sshorter.
I pute some fades-in.
I propagate the length value of this fade-out on these items and check if there is no errors.
As you can see, all seems to be fine.
I check.
It seems to be good, the sums seems to be good.
Voilà, with simple conditions, we have prevented invalid values insertions in our project.
In general, the rule is simple:
determine the minimum and maximum value that a parameter can have,
compare this to the new value we want to set,
and write some conditions to avoid this value to be lower than the minimum or greater than the maximum.
In the next video, we will see how to use tables. See ya!