Beginner question regarding objects

Is there a way to copy an object in LUA? Not only referencing it.

For exampel, if I do

local voices = { }
table.insert(voices, renoise.song():pattern(1):track(1):line(1):note_column(1))

then voices[1] will just be a reference to the object in the renoise.song() class. Can I somehow make it unique (copy it to a slot, so to speak), and perhaps even copy it back to renoise.song() without an error?

This would make things a lot easier to me. Otherwise I’d have to construct my own custom data structure to store/restore values from.

At the moment you’ll have to maintain your own custom tables and manually get/set the particular values that you’re interested in working with.

If you write a few useful helper functions to take care of the boring work for you, then it should hopefully not be too painful :slight_smile:

Thanks for answering!

The one thing that makes that a bit extra tedious is that you can’t (?) see within an object where it is situated (for example which track a note_column belongs to). So programming wise, I’ll have to pass a lot of values from function to function in order to store such information in my custom object. It’s not the prettiest…

I’ll do it like this for now. Seems to work, but feels a bit awkward… It will eventually be used for ordering chord voices by pitch in xStream.

Click to view contents
function get_song_pos(passed_column_index) -- will be needed in this implementation
  local song_pos = {
                    ["pattern"] = renoise.song().selected_pattern_index,
                    ["track"] = renoise.song().selected_track_index,
                    ["line"] = renoise.song().selected_line_index,
                    ["column"] = passed_column_index -- sorry for this, but that's life
                   }
  return song_pos
end
--helper to store note column data
function store_note_column(note_column, song_pos)
  local note_data = { ["parameters"] = { }, ["pos"] = song_pos }
  local parameters = { 
                       ["delay_value"] = true,
                       ["effect_amount_value"] = true,
                       ["effect_number_value"] = true,
                       ["instrument_value"] = true,
                       ["note_value"] = true,
                       ["panning_value"] = true,
                       ["volume_value"] = true
                     }
                          
  for parameter, value in pairs(parameters) do
      note_data.parameters[parameter] = note_column[parameter]
  end
  return note_data
end
-- helper to restore note column data
function restore_note_column(note_data, song_pos)
  local note_column = renoise.song():pattern(song_pos.pattern):track(song_pos.track):line(song_pos.line):note_column(song_pos.column)
  for parameter, value in pairs(note_data.parameters) do
    note_column[parameter] = value
  end
  
end
-- testing below, copying from stored note_data to a note_column
local song_pos = get_song_pos(1)
local note_column_temp = store_note_column(renoise.song().selected_note_column, song_pos)
song_pos.line = song_pos.line + 1
restore_note_column(note_column_temp, song_pos)