Old 11-21-2019, 03:08 PM   #1
MusoBob
Human being with feelings
 
MusoBob's Avatar
 
Join Date: Sep 2014
Posts: 2,643
Default while loop

I just need the script to wait until the play cursor == time start
but this don't work
Code:
   time_start = 42.0

   while (current_pos ~= time_start) do  
      current_pos = reaper.GetPlayPosition()
      play_state = reaper.GetPlayState()
      if play_state ~= 1 then break end
   end
__________________
ReaTrakStudio Chord Track for Reaper forum
www.reatrak.com
STASH Downloads https://stash.reaper.fm/u/ReaTrak
MusoBob is offline   Reply With Quote
Old 11-21-2019, 03:35 PM   #2
Meo-Ada Mespotine
Human being with feelings
 
Meo-Ada Mespotine's Avatar
 
Join Date: May 2017
Location: Leipzig
Posts: 6,630
Default

You need to work with defer-loops for that, as Reaper reads all the attributes of the playcursor only once, when the script is started and doesn't update it again.
Exception: with defer-loops, Reaper will restart the script after finishing it, from a function of your choice.
And that restart updates all the "internal states" like reading the playcursor-position.

Here's a small introduction I wrote about defer-cycles. This should help you do it:
https://forum.cockos.com/showpost.ph...2&postcount=21
__________________
Use you/she/her.Ultraschall-Api Lua Api4Reaper - Donate, if you wish

On vacation for the time being...
Meo-Ada Mespotine is offline   Reply With Quote
Old 11-21-2019, 03:40 PM   #3
nait
Human being with feelings
 
nait's Avatar
 
Join Date: Jun 2018
Location: Edmonton, AB, Canada
Posts: 1,391
Default

I don't work with LUA, but I do a lot of other programming, and will also say that if you're doing a while loop like this that is time based, you shouldn't do a check like where time != something because in a lot of cases, with whatever processing that happens in your loop, the time may ALWAYS never equal the time you're looking for. In other words, it may be that your time is earlier than the exit time for most of the loops, and then because of how long it takes to process, the next time it's through the loop, it jumps past the exit time, and therefore it doesn't meet that exit condition.

Do something like, while time < some time.


This is all pseudo code, of course, but you get the gist.. and this is just a general coding thing. Again, I don't know LUA, and what was posted by mespotine might be the real answer you need. I'm just saying you'll run into problems doing this how you did (in any language). It's not a good idea to check if a time is reached using equals / not equals. There are of course exceptions to this, this is a general rule, but this could create an infinite loop.
__________________
My Rig (also serves as my gaming PC): MSI Mag X570 Tomahawk Mobo, Ryzen R9 3900X, 32GB RAM, Samsung 960 Evo 500gb NVMe, Crucial 1TB NVMe, NVidia RTX 2080 Super, Arturia Minifuse 2, Nektar Impact LX25+ MIDI Controller Keyboard.

Last edited by nait; 11-21-2019 at 04:07 PM.
nait is offline   Reply With Quote
Old 11-21-2019, 04:04 PM   #4
Meo-Ada Mespotine
Human being with feelings
 
Meo-Ada Mespotine's Avatar
 
Join Date: May 2017
Location: Leipzig
Posts: 6,630
Default

@nait
I think you're right with your point, this should be added even in a defer-loop.


@MusoBob
So this might work:
Code:
-- will stop the defer-loop, when playposition is bigger/equal 42 seconds
time_start = 42.0

   function main()
      current_pos = reaper.GetPlayPosition()
      play_state = reaper.GetPlayState()
      if current_pos>=time_start then 
        -- put in here the code, you want to execute if the condition is met
        return 
      end

      reaper.defer(main)
   end

main()
You also checked for play_state, not for current_pos in your example-code. I corrected it in my example,
__________________
Use you/she/her.Ultraschall-Api Lua Api4Reaper - Donate, if you wish

On vacation for the time being...
Meo-Ada Mespotine is offline   Reply With Quote
Old 11-21-2019, 05:47 PM   #5
nait
Human being with feelings
 
nait's Avatar
 
Join Date: Jun 2018
Location: Edmonton, AB, Canada
Posts: 1,391
Default

I think he was intentionally checking play_state to also exit the loop if the song is not playing. So I presume he's would want his if changed to this:

Code:
if current_pos>=time_start or play_state = 1 then
Again, I don't ever do LUA (And too lazy to look it up lol), so possibly update the above for however you do an "or", and however you do a check for equals and assuming the code fires (ex. the exit of the loop) the same when either of those conditions is met.

And sorry if it sounds like I'm nitpicking code, you did a great job of helping.. just think it would make sense that the play_state part was intended (I could be wrong, however).
__________________
My Rig (also serves as my gaming PC): MSI Mag X570 Tomahawk Mobo, Ryzen R9 3900X, 32GB RAM, Samsung 960 Evo 500gb NVMe, Crucial 1TB NVMe, NVidia RTX 2080 Super, Arturia Minifuse 2, Nektar Impact LX25+ MIDI Controller Keyboard.
nait is offline   Reply With Quote
Old 11-21-2019, 06:06 PM   #6
Meo-Ada Mespotine
Human being with feelings
 
Meo-Ada Mespotine's Avatar
 
Join Date: May 2017
Location: Leipzig
Posts: 6,630
Default

I think too, but I guess, it was a leftover from his previous tests and experiments, so I removed it for my examplecode.
Though nothing prevents him re-adding it again, as I'm pretty sure, that my code is only the basis but far away from the actual code needed by him.

BTW: learning Lua is very helpful when working with Reaper. There's so much you can quickly code in a few lines of Lua to make Reaper's workflow fitting even better your needs.
So it's definately worth learning. Especially if you are already familiar with coding.
The biggest problem though, it's tempting to code more rather than actually using Reaper for making music
__________________
Use you/she/her.Ultraschall-Api Lua Api4Reaper - Donate, if you wish

On vacation for the time being...
Meo-Ada Mespotine is offline   Reply With Quote
Old 11-21-2019, 08:38 PM   #7
nait
Human being with feelings
 
nait's Avatar
 
Join Date: Jun 2018
Location: Edmonton, AB, Canada
Posts: 1,391
Default

Yeah I might actually pick up Lua one day as it would be nice to be able to write some scripts for myself should I ever need something. And yes, you can really get down a rabbit hole with coding!

I've lost a lot of time coding stuff completely unrelated to my work (which is programming). Did some video game coding in Unity that sucked a lot of my time away, some scripting and web development for a hockey pool site.. that was a real time vampire. This stuff can really eat up free time.. that's kind of why I'm not sure I want to get sucked back into more of that! haha I completely sympathize with anyone that develops these Reaper scripts for other people for free or little money.
__________________
My Rig (also serves as my gaming PC): MSI Mag X570 Tomahawk Mobo, Ryzen R9 3900X, 32GB RAM, Samsung 960 Evo 500gb NVMe, Crucial 1TB NVMe, NVidia RTX 2080 Super, Arturia Minifuse 2, Nektar Impact LX25+ MIDI Controller Keyboard.
nait is offline   Reply With Quote
Old 11-21-2019, 08:57 PM   #8
MusoBob
Human being with feelings
 
MusoBob's Avatar
 
Join Date: Sep 2014
Posts: 2,643
Default

Thanks I will get back to it now.
I was looking at spk77 defer this morning
https://forum.cockos.com/showthread.php?t=213212#4
and repeat until

I was trying something like this, but wasn't working so I read up on the posts and try again.

Code:
function wait_for_cursor(time_start)
  current_pos = reaper.GetPlayPosition()
  defer_count = defer_count+1
  if current_pos ~= time_start then
    current_pos = reaper.GetPlayPosition()
    reaper.defer(wait_for_cursor)
  else
    Msg("At Start Time...\n")
    return
  end
  Msg(tostring(defer_count) .. " ")
end
__________________
ReaTrakStudio Chord Track for Reaper forum
www.reatrak.com
STASH Downloads https://stash.reaper.fm/u/ReaTrak
MusoBob is offline   Reply With Quote
Old 11-21-2019, 09:03 PM   #9
Lokasenna
Human being with feelings
 
Lokasenna's Avatar
 
Join Date: Sep 2008
Location: Calgary, AB, Canada
Posts: 6,551
Default

Because scripts aren't run as often as Reaper actually updates, the cursor position will almost never equal the time you're looking for.

The most accurate you can get is to check if cursor_pos >= time_start then do_things() end
__________________
I'm no longer using Reaper or working on scripts for it. Sorry. :(
Default 5.0 Nitpicky Edition / GUI library for Lua scripts / Theory Helper / Radial Menu / Donate
Lokasenna is offline   Reply With Quote
Old 11-21-2019, 10:49 PM   #10
mschnell
Human being with feelings
 
mschnell's Avatar
 
Join Date: Jun 2013
Location: Krefeld, Germany
Posts: 14,793
Default

BTW.: Actually in close to any situations in programming, it's forbidden to compare REAL values for being equal.

-Michael
mschnell is online now   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 09:53 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.