View Single Post
Old 05-10-2019, 09:07 AM   #7
Human being with feelings
Lokasenna's Avatar
Join Date: Sep 2008
Location: Calgary, AB, Canada
Posts: 6,076

Originally Posted by tXShooter View Post
Maybe something like:
	elseif string.starts(nameOut, "Special " .. i) then -- found 'Start' marker
		Song{i}SMarked = true
		Song{i}StartMarkerNum = retval
		Song{i}Title = MkrSegments[2]
		Song{i}Singer = MkrSegments[3]
		GUI.Val("Song" .. i .. "Tracks", MkrSegments[4])
		GUI.Val("Song" .. i .. "Scene", MkrSegments[5])
		Song{i}StartMarkerButtonText = reaper.format_timestr(posOut, "")
		GUI.elms.Song{i}StartButton.func = EditSong{i}Start
I've tried a few different variations, but either it's not possible, or I'm doing it wrong.
You've got the right idea, but you can only interpolate variable names like that when you're accessing a table. Lua also doesn't have real interpolation like most languages - you have to use .. to concatenate String .. Variable .. String. In this case, try:
_G["Song"..i.."Title] = MkrSegments[2]

1. It's generally not a good idea to use global variables for everything. There's a performance cost, and you can leave yourself open to all sorts of complicated bugs because your variables are accessible from EVERYWHERE in the script.

2. Any time you're repeating the same structure, or you find yourself wanting to have a bunch of the same variables with an incrementing digit, a table is going to make your life so much simpler.

local songs = {}

... do stuff ...

elseif string.starts(nameOut, "Special " .. i) then -- found 'Start' marker
  songs[i] = {
    marked = true,
    startMarkerNum = retval,
    title = MkrSegments[2],
    singer = MkrSegments[3],
    startMarkerButtonText = reaper.format_timestr(posOut, ""),
  GUI.Val("Song" .. i .. "Tracks", MkrSegments[4])
  GUI.Val("Song" .. i .. "Scene", MkrSegments[5])
  GUI.elms["Song"..i.."StartButton"].func = EditSongStart
  GUI.elms["Song"..i.."StartButton"].params = {i}
Also note that I've changed the name of EditSongStart so that all of the buttons can use the same function, and then they can pass their i to it as a parameter. Always try to make things reusable.
Lokasenna is offline   Reply With Quote