View Single Post
Old 01-31-2019, 09:18 AM   #2
cfillion
Human being with feelings
 
cfillion's Avatar
 
Join Date: May 2015
Location: Québec, Canada
Posts: 2,775
Default

Use reaper.defer to schedule a function to be run later (~33ms). Unlike regular loops which freeze REAPER until they stop, reaper.defer allows the GUI to run in between.

Code:
function onNextMeasure(callback)
  local beatsSinceLastMeasure = reaper.TimeMap2_timeToBeats(0, reaper.GetPlayPosition())
  
  if beatsSinceLastMeasure < 0.1 then
    -- if we're at (or very near) the start of a measure, call the given function
    callback()
  else
    -- or, if we're not at the start of a measure, check again ~33ms later
    reaper.defer(function() onNextMeasure(callback) end)
  end
end

onNextMeasure(function()
  -- code to run at the next measure here
  reaper.ShowConsoleMsg("Hello Next Measure!\n")
end)

Last edited by cfillion; 01-31-2019 at 09:32 AM.
cfillion is offline   Reply With Quote