Beginner question regarding objects

(joule) #1

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

For exampel, if I do

local voices = { }

then voices[1] will just be a reference to the object in the class. Can I somehow make it unique (copy it to a slot, so to speak), and perhaps even copy it back to 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.

(dblue) #2

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:

(joule) #3

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"] =,
                    ["track"] =,
                    ["line"] =,
                    ["column"] = passed_column_index -- sorry for this, but that's life
  return song_pos
--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]
  return note_data
-- helper to restore note column data
function restore_note_column(note_data, song_pos)
  local note_column =
  for parameter, value in pairs(note_data.parameters) do
    note_column[parameter] = value
-- 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(, song_pos)
song_pos.line = song_pos.line + 1
restore_note_column(note_column_temp, song_pos)