Help LUA: insert a variable in "vb:slider.max" without error s

I have a problem with the new question build a vb:slider. The problem is insert a especific variable number in max (or min) inside the slider:

The code:

tracks_ics = function()
  vb.views['nav_track'].max = renoise.song().sequencer_track_count + 1 + renoise.song().send_track_count
  vb.views['nav_track'].value = renoise.song().selected_track_index
  vb.views["index_value"].text = ( "%s" ):format( renoise.song().selected_track_index )  
end

function ini_tracks()
  renoise.song().selected_track_index_observable:add_notifier(tracks_ics)
end
renoise.tool().app_new_document_observable:add_notifier(ini_tracks)

------------------------------------------------------------------------------------------
... ...
vb:horizontal_aligner {
  width = 690,
  mode = 'left',
  vb:slider {
    id = 'nav_track',
    min = 1,
    max = 2, --renoise.song().sequencer_track_count + 1 + renoise.song().send_track_count, ------<< PROBLEM HERE!!!
    value = 1,
    width = 670,
    notifier = function(value) renoise.song().selected_track_index = value end,
    },
   vb:text {
      id = 'index_value',
   text = '---',
   }
}
... ...

I need insert inside the slider: max =renoise.song().sequencer_track_count + 1 + renoise.song().send_track_count

beforevb.views[‘nav_track’].max = renoise.song().sequencer_track_count + 1 + renoise.song().send_track_count take the control.If I insert max = 2 , the slider does not work as desire to start and use the slider.

renoise.song().sequencer_track_count + 1 + renoise.song().send_track_count” is equal to total number of total tracks/groups + Master Track + total Send Tracks (equal to maximum number of index in Pattern Editor).

The problem is that I insert max = renoise.song().sequencer_track_count + 1 + renoise.song().send_track_count, the code work, but fail in start Renoise.

For variablerenoise.song().selected_track_index is solved with “renoise.song().selected_track_index _observable”:

function ini_tracks()
renoise.song().selected_track_index_observable:add_notifier(tracks_ics)
end
renoise.tool().app_new_document_observable:add_notifier(ini_tracks)

But, not exist “_observable” to “renoise.song().sequencer_track_count” and “renoise.song().send_track_count” to prevent failure to start Renoise. I suppose it has to do.Exist any solution to avoid this error???

Exist another way to show the variable numberrenoise.song().sequencer_track_count + 1 + renoise.song().send_track_countwith _observable???

For more information this topic is related:https://forum.renoise.com/t/solved-help-lua-print-in-real-time-the-value-of-selected-track-insi/46442

Please, Help! Thanks in advance!

Yes, if you are trying to access the Renoise song before the song object is accessible you’ll hit a snag.

However, the idea of assigning a number to the maximum of a slider is flawed in itself. Because, this value is assigned during construction time, and not evaluated later. So, it will stay at that initial value and not reflect the number of tracks as they potentially change over time - which could result in an error later on.

You probably want to do it a little differently: instead of assigning a min, max value to the slider simply leave it at the default 0-1 range and scale it to a relevant range in the notifier method.

Something like this:

notifier = function(val)
  local num_tracks = renoise.song().sequencer_track_count + renoise.song().send_track_count
  local track_idx = math.floor(val * num_tracks) + 1
  renoise.song().selected_track_index = track_idx 
end

Thanks, excellent solution,I like it!

Ok,now I have this code:

tracks_ics = function()
  --vb.views['nav_track'].max = renoise.song().sequencer_track_count + 1 + renoise.song().send_track_count
  --vb.views['nav_track'].value = renoise.song().selected_track_index
  vb.views["index_value"].text = ( "%s" ):format( renoise.song().selected_track_index )  
end
 
function ini_tracks()
  renoise.song().selected_track_index_observable:add_notifier(tracks_ics)
end
renoise.tool().app_new_document_observable:add_notifier(ini_tracks)
 
------------------------------------------------------------------------------------------
... ...
vb:horizontal_aligner {
  width = 690,
  mode = 'left',
  vb:slider {
    id = 'nav_track',
    min = 0,
    max = 1,
    value = 0,
    width = 670,
    --notifier = function(value) renoise.song().selected_track_index = value end,
    notifier = function(val)
      local num_tracks = renoise.song().sequencer_track_count + renoise.song().send_track_count
      local track_idx = math.floor(val * num_tracks) + 1
      renoise.song().selected_track_index = track_idx 
    end
   },
    vb:text {
      id = 'index_value',
      text = '---',
   }
}
... ...

Now, this code work correctly, but the sliderremain staticwhen tracks are selected randomly directly into Pattern Editor.The intention is to work together, that the slider always check the selected track, that it moves also.

It seems to be missing a piece of code so that the slider reactwhen the track is selected in Pattern Editor.Any way to fix it?

In the first case, with the above code comment #1, it worked perfect in together, even adding or deleting any track, except the error starting Renoise.

In the first case, with the above code comment #1, it worked perfect in together, even adding or deleting any track, except the error starting Renoise.

It would eventually have caused problems. Think about it - you were initially setting the maximum to a value. Then, later on you set the current track based on this value.
All it would take was for the song to contain less tracks than originally - then the resulting track index would have been invalid.

It seems to be missing a piece of code so that the slider reactwhen the track is selected in Pattern Editor.Any way to fix it?

Yes. You need to change the slider value, for example through your existingtracks_ics method.

But since you are now both listening to and changing the value of the slider, it’s important to avoid a feedback loop.
Try it first and see if you need to implement anti-feedback measures (it’s really simple to do).

Btw: the way track index is set from the slider is a little naive. Isn’t it the point that you want to skip/ignore group tracks and only target sequencer tracks?

Yes. You need to change the slider value, for example through your existingtracks_ics method.

But since you are now both listening to and changing the value of the slider, it’s important to avoid a feedback loop.
Try it first and see if you need to implement anti-feedback measures (it’s really simple to do).

I’ve been trying things… If I add the codevb.views[‘nav_track’].value = renoise.song().selected_track_index jump these error:

*** std::logic_error: ‘ViewBuilder: invalid value for slider: ‘2’. value must be [0 - 1].’ because the value min = 0 and max = 1.

In the other hand, if I change the values min and max in vb.views (vb.views[‘nav_track’].min &vb.views[‘nav_track’].max), the math.floor of the notifier not work correctly or the selected track index is not correctly, I’m not sure what really happens… Also, this values min and max are always 0 and 1 to work correctly. So how do fix it?

Only a function invb.views[‘nav_track’].value? avb.views[‘nav_track’].notifier?

Btw: the way track index is set from the slider is a little naive. Isn’t it the point that you want to skip/ignore group tracks and only target sequencer tracks?

Yes,It could work only with sequencer tracks. I suppose that is applying a condition (with a “if … then”), but I would like to work also with the Master Track and Send Tracks.This would be a second step after solving the above problem.


Apart, I want to create other slider to navigate for sub-columns inside the tracks, range 1 until 12.But I see much more complicated.

*** std::logic_error: ‘ViewBuilder: invalid value for slider: ‘2’. value must be [0 - 1].’ because the value min = 0 and max = 1.

Yes, for the reasons we already discussed the slider range needs a range of 0-1, you have to manually convert back and forth between thisand the track count.
It’s easy - simply a matter of having the track count readily available and then dividing by this number. E.g. a selected track 4 out of a total of 8 tracks would be a slider value of 0.5 …

Of course, having the code to calculate the track count in both the slider notifier and the notifier method is not the best practice (repeated code == bad).
This is where I would create a new function with the single goal of computing the track count and then use that instead.

This would be a second step after solving the above problem.

Okay, that makes sense. You will most likely also have to handle a potential notifier feedback problem once the you’ve implemented the code above. One thing after another…

Yes, for the reasons we already discussed the slider range needs a range of 0-1, you have to manually convert back and forth between thisand the track count.
It’s easy - simply a matter of having the track count readily available and then dividing by this number. E.g. a selected track 4 out of a total of 8 tracks would be a slider value of 0.5 …

Of course, having the code to calculate the track count in both the slider notifier and the notifier method is not the best practice (repeated code == bad).

Thanks! Ok, I understand.

This is where I would create a new function with the single goal of computing the track count and then use that instead.

This would be great!The intention is that the slider work together with the manual selection in the pattern editor.I guess you have this in mind…

Okay, that makes sense. You will most likely also have to handle a potential notifier feedback problem once the you’ve implemented the code above. One thing after another…

Certain! In the first instance, thought that the code would be simpler.But it’s not like that, :unsure:solving a problem generates another problem.