View Single Post
Old 11-16-2019, 02:25 PM   #613
Human being with feelings
Lokasenna's Avatar
Join Date: Sep 2008
Location: Calgary, AB, Canada
Posts: 6,550

Originally Posted by reapero View Post
i wonder if i am just making things more complicated than they should.
Putting a bunch of values into one string and splitting them back out again is actually the most common way to do this.

If your presets are flat - that is, they don't have any nested tables - it's really easy:

-- Make sure the characters you use aren't in the data, or you'll have to add
-- some logic to hande them
local keyValueSeparator = "="
local fieldSeparator = "&"

local function hashToExtString(hash)
  local out = {}
  for k, v in pairs(hash) do
    out[#out + 1] = k .. keyValueSeparator .. v
  return out.concat(fieldSeparator)

local function hashFromExtString(ext)
  local out = {}
  for pair in ext:gmatch("[^" .. fieldSeparator .. "]+") do
    local k, v = pair:match("(.+)=(.+)")

    if tonumber(v) then
      v = tonumber(v)
    elseif v == "true" then
      v = true
    elseif v == "false" then
      v = false

    out[k] = v

  return out

-- To save:

local presets = {
    name = "apple",
    min = 5,
    max = 10,
    name = "banana",
    min = 0,
    max = 3,
    name = "cherry",
    min = 2,
    max = 9,

for i, preset in pairs(presets) do
  local extString = hashToExtString(preset)
  reaper.SetExtState("MY_PLUGIN", "PRESET_NUMBER_" .. i, extString, true)

-- To load:

local presets = {}

for i = 1, NUMBER_OF_PRESETS do
  local extString = reaper.GetExtState("MY_PLUGIN", "PRESET_NUMBER_"..i)

  presets[i] = hashFromExtString(extString)
(Note: Don't have a chance to test this)

If you do have nested tables, it's a bit more work. Radial Menu stores its settings in a text file, and I found a function that writes all of the table contents as Lua code: . Loading them again is just a matter of reading in the file and telling Lua to execute it.
Lokasenna is online now   Reply With Quote