Jump to content


Photo

Assign multiple variables = true (or false) ??

variables

  • Please log in to reply
2 replies to this topic

#1 Raul (ulneiz)

Raul (ulneiz)

    Guruh Motha Fakka is Levitating and Knows Everything About Renoise Member

  • Normal Members
  • PipPipPipPipPipPipPipPipPipPipPipPipPip
  • 1028 posts
  • Gender:Male
  • Location:Spain

Posted 06 November 2017 - 00:51

I need a little help with how to sort and compress code...

 

For the following code, is there any way to write the shortest code, iterating or something like that, to avoid repeating so much?  

  if ( value == 0 ) then
     add_point_1 = true  add_point_2 = true  add_point_3 = true  add_point_4 = true  add_point_5 = true  add_point_6 = true  add_point_7 = true  add_point_8 = true  add_point_9 = true add_point_10 = true
    add_point_11 = true add_point_12 = true add_point_13 = true add_point_14 = true add_point_15 = true add_point_16 = true add_point_17 = true add_point_18 = true add_point_19 = true add_point_20 = true
    add_point_21 = true add_point_22 = true add_point_23 = true add_point_24 = true add_point_25 = true add_point_26 = true add_point_27 = true add_point_28 = true add_point_29 = true add_point_30 = true
    add_point_31 = true add_point_32 = true
  end
  if ( value == 1 ) then
     add_point_1 = false add_point_2 = true  add_point_3 = true  add_point_4 = true  add_point_5 = true  add_point_6 = true  add_point_7 = true  add_point_8 = true  add_point_9 = true add_point_10 = true
    add_point_11 = true add_point_12 = true add_point_13 = true add_point_14 = true add_point_15 = true add_point_16 = true add_point_17 = true add_point_18 = true add_point_19 = true add_point_20 = true
    add_point_21 = true add_point_22 = true add_point_23 = true add_point_24 = true add_point_25 = true add_point_26 = true add_point_27 = true add_point_28 = true add_point_29 = true add_point_30 = true
    add_point_31 = true add_point_32 = true
  end
  
  --etc etc etc

  if ( value == 32 ) then
     add_point_1 = false add_point_2 = false  add_point_3 = false  add_point_4 = false  add_point_5 = false  add_point_6 = false  add_point_7 = false  add_point_8 = false  add_point_9 = false add_point_10 = false
    add_point_11 = false add_point_12 = false add_point_13 = false add_point_14 = false add_point_15 = false add_point_16 = false add_point_17 = false add_point_18 = false add_point_19 = false add_point_20 = false
    add_point_21 = false add_point_22 = false add_point_23 = false add_point_24 = false add_point_25 = false add_point_26 = false add_point_27 = false add_point_28 = false add_point_29 = false add_point_30 = false
    add_point_31 = false add_point_32 = false
  end
 

Any way to do this with tables?

 

Thanks!


Edited by Raul (ulneiz), 06 November 2017 - 18:36.

:excl: Development of my tool: GT16-Colors

 

:excl: My API wishlist R3.1 (updated 24 July 2017):

Spoiler

 

:excl: My Renoise 3.1 wishlist (updated 26 September 2017):

Spoiler

#2 joule

joule

    Guruh Motha Fakka is Levitating and Knows Everything About Renoise Member

  • Normal Members
  • PipPipPipPipPipPipPipPipPipPipPipPipPip
  • 1502 posts
  • Gender:Not Telling
  • Location:Sweden
  • Interests:music, philosophy, engineering

Posted 06 November 2017 - 08:49

-- creating a table for the example.
local points = {
  add_point_1 = true, add_point_2 = true, add_point_3 = true,
  add_point_4 = true, add_point_5 = true, add_point_6 = true,
  add_point_7 = true, add_point_8 = true, add_point_9 = true,
  add_point_10 = true, add_point_11 = true, add_point_12 = true,
  add_point_13 = true, add_point_14 = true, add_point_15 = true,
  add_point_16 = true, add_point_17 = true, add_point_18 = true,
  add_point_19 = true, add_point_20 = true, add_point_21 = true,
  add_point_22 = true, add_point_23 = true, add_point_24 = true,
  add_point_25 = true, add_point_26 = true, add_point_27 = true,
  add_point_28 = true, add_point_29 = true, add_point_30 = true,
  add_point_31 = true, add_point_32 = true
}

-- set all values to something
function set_all_values(tbl, value)
  for k, _ in pairs(tbl) do
    tbl[k] = value
  end
end

function set_active_point(point)
  if (point == 0) then
    set_all_values(points, true)
  elseif (point == 32) then
    set_all_values(points, false)
  else
    set_all_values(points, true)
    points["add_point_" .. point] = false
  end 
   
end

set_active_point(1)
PS.

Alternate approach: In a well structured code, you migh want to make a "point" class - as a nice "package" of all values, behaviors and data (like gui) of a point. All these point objects could then have a value that is "connected" to a single renoise.Document.ObservableBoolean object. In practice, you will then be able to automatically set all point values by setting a single global value.

This is a bit complex to explain in a short post, but I just want to mention it to give a vague idea of how things "should" be done ideally.

Edited by joule, 06 November 2017 - 10:01.


#3 Raul (ulneiz)

Raul (ulneiz)

    Guruh Motha Fakka is Levitating and Knows Everything About Renoise Member

  • Normal Members
  • PipPipPipPipPipPipPipPipPipPipPipPipPip
  • 1028 posts
  • Gender:Male
  • Location:Spain

Posted 06 November 2017 - 18:35

-- creating a table for the example.
local points = {
  add_point_1 = true, add_point_2 = true, add_point_3 = true,
  add_point_4 = true, add_point_5 = true, add_point_6 = true,
  add_point_7 = true, add_point_8 = true, add_point_9 = true,
  add_point_10 = true, add_point_11 = true, add_point_12 = true,
  add_point_13 = true, add_point_14 = true, add_point_15 = true,
  add_point_16 = true, add_point_17 = true, add_point_18 = true,
  add_point_19 = true, add_point_20 = true, add_point_21 = true,
  add_point_22 = true, add_point_23 = true, add_point_24 = true,
  add_point_25 = true, add_point_26 = true, add_point_27 = true,
  add_point_28 = true, add_point_29 = true, add_point_30 = true,
  add_point_31 = true, add_point_32 = true
}

-- set all values to something
function set_all_values(tbl, value)
  for k, _ in pairs(tbl) do
    tbl[k] = value
  end
end

function set_active_point(point)
  if (point == 0) then
    set_all_values(points, true)
  elseif (point == 32) then
    set_all_values(points, false)
  else
    set_all_values(points, true)
    points["add_point_" .. point] = false
  end 
   
end

set_active_point(1)
PS.

Alternate approach: In a well structured code, you migh want to make a "point" class - as a nice "package" of all values, behaviors and data (like gui) of a point. All these point objects could then have a value that is "connected" to a single renoise.Document.ObservableBoolean object. In practice, you will then be able to automatically set all point values by setting a single global value.

This is a bit complex to explain in a short post, but I just want to mention it to give a vague idea of how things "should" be done ideally.

 

 

Thanks Joule!

 

Solved!

 

Finally I have chosen to include better structured tables to have access to several true or false at the same time. In this way I can, for example, change to true the even numbers, or the odd numbers, or all at once, or in a chain. I also use other tables to access several multi-button properties. So I can call all of them using a single number.

 

For example:

--show points variable
show_point = { false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false,
               false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false }
---
function vpd_aut_show_point(value)
  if ( show_or_chain == false ) then
    if ( show_point[value] == false ) then
      show_point[value] = true
      vb.views["VPD_AUT_CONTROLS_"..value].visible = true
      vb.views["VPD_AUT_BT_POINT_"..value].color = VPD_AUT_PNT_CLR_ON[value]
      vb.views["VPD_AUT_POINT_"..value].visible = true
    else
      show_point[value] = false
      vb.views["VPD_AUT_CONTROLS_"..value].visible = false
      vb.views["VPD_AUT_BT_POINT_"..value].color = VPD_AUT_PNT_CLR_OFF[value]
      vb.views["VPD_AUT_POINT_"..value].visible = false
    end
  else
    for i = 1, 32 do
      if ( i > value ) then
        show_point[i] = false
        vb.views["VPD_AUT_CONTROLS_"..i].visible = false
        vb.views["VPD_AUT_BT_POINT_"..i].color = VPD_AUT_PNT_CLR_OFF[i]
        vb.views["VPD_AUT_POINT_"..i].visible = false
      else
        show_point[i] = true
        vb.views["VPD_AUT_CONTROLS_"..i].visible = true
        vb.views["VPD_AUT_BT_POINT_"..i].color = VPD_AUT_PNT_CLR_ON[i]
        vb.views["VPD_AUT_POINT_"..i].visible = true
      end 
    end  
  end
end


--and a button for bang!
vb:button {
  id = "VPD_AUT_BT_POINT_1",
  height = 20, width = 28,
  color = VPD_AUT_PNT_CLR_OFF[1],
  text = "1",
  notifier = function() vpd_aut_show_point(1) end
},

:excl: Development of my tool: GT16-Colors

 

:excl: My API wishlist R3.1 (updated 24 July 2017):

Spoiler

 

:excl: My Renoise 3.1 wishlist (updated 26 September 2017):

Spoiler