Old 07-13-2018, 05:44 PM   #1
Thonex
Human being with feelings
 
Join Date: May 2018
Location: Los Angeles
Posts: 1,721
Default Lua Reascript Is there a "wait ()" statement equivalent?

I'm coding some mixer toggle states and would like to minimize clicking by ducking volume and then waiting and then resuming functions.

Is there a Reascript or Lua command that would be equivalent to a simple wait(ms/Ms) command?

Thanks.

Cheers,

Andrew K
Thonex is offline   Reply With Quote
Old 07-13-2018, 06:32 PM   #2
Lokasenna
Human being with feelings
 
Lokasenna's Avatar
 
Join Date: Sep 2008
Location: Calgary, AB, Canada
Posts: 6,551
Default

os.sleep, but that will cause Reaper to wait as well.

You can use defer to let a function keep checking the time and then do something if time_now - time_started > X.

Code:
local function Msg(str)
   reaper.ShowConsoleMsg(tostring(str) .. "\n")
end

time_start = reaper.time_precise()
Msg("Starting a timer for 2 seconds...")

local function Main()

    local elapsed = reaper.time_precise() - time_start

    if elapsed >= 3 then
        Msg("2 seconds have elapsed! Ending the loop.")
        return
    else
        reaper.defer(Main)
    end
    
end

Main()
__________________
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 07-13-2018, 07:47 PM   #3
Thonex
Human being with feelings
 
Join Date: May 2018
Location: Los Angeles
Posts: 1,721
Default

Thanks Loka!! Again!!

Will try it out this weekend.

Here's to hoping it will give milli or micro second precision.

Cheers,

Andrew K
Thonex is offline   Reply With Quote
Old 07-13-2018, 07:49 PM   #4
Xenakios
Human being with feelings
 
Xenakios's Avatar
 
Join Date: Feb 2007
Location: Oulu, Finland
Posts: 8,062
Default

Quote:
Originally Posted by Thonex View Post
Here's to hoping it will give milli or micro second precision.
Sorry to say, but it won't. The deferred functions are called about in the 30-50 millisecond range without any guarantees. (If anything in the GUI thread stalls due to Reaper's operations or 3rd party plugins, the ReaScript deferred functions will also stall.)
__________________
I am no longer part of the REAPER community. Please don't contact me with any REAPER-related issues.
Xenakios is offline   Reply With Quote
Old 07-13-2018, 09:24 PM   #5
Thonex
Human being with feelings
 
Join Date: May 2018
Location: Los Angeles
Posts: 1,721
Default

Quote:
Originally Posted by Xenakios View Post
Sorry to say, but it won't. The deferred functions are called about in the 30-50 millisecond range without any guarantees. (If anything in the GUI thread stalls due to Reaper's operations or 3rd party plugins, the ReaScript deferred functions will also stall.)
DARN!!

whaaa!



Cheers,

Andrew K
Thonex is offline   Reply With Quote
Old 04-30-2021, 04:21 AM   #6
daniellumertz
Human being with feelings
 
daniellumertz's Avatar
 
Join Date: Dec 2017
Location: Brazil
Posts: 2,008
Default

Ok, I am trying to make a Wait() function to work inside other defered function. Mostly for debugging . And controlling the rate of prints.

Code:
function Msg(str)
    reaper.ShowConsoleMsg(tostring(str) .. "\n")
end
 

function Wait(time_start, delay)
    Msg("func timestart    "..time_start)
    elapsed = reaper.time_precise() - time_start

    if elapsed >= delay then
        Msg("2 seconds have elapsed! Ending the loop.")
        return
    else
        reaper.defer(Wait)
    end
end
 
function def()
    local mainHandle = reaper.GetMainHwnd()
    local focusedWindow = reaper.JS_Window_GetFocus()
    if reaper.JS_Window_GetParent(focusedWindow) == mainHandle then
      reaper.ShowConsoleMsg("aloha".."\n")
    end
    
    Msg('mainHandle       '..tostring(mainHandle))
    Msg('focusedWindow       '..tostring(focusedWindow))
    Msg('reaper.time_precise()       '..tostring(reaper.time_precise()))
    Wait(reaper.time_precise(), 2 )
    reaper.defer(def)
end

def()
I tried to adapt lokasenna script but getting a really weird error where it prints time_start on the line
Code:
Msg("func timestart    "..time_start)
And at the same time gives a error saying time_start is nil, although printing right!
daniellumertz is offline   Reply With Quote
Old 04-30-2021, 06:45 AM   #7
Meo-Ada Mespotine
Human being with feelings
 
Meo-Ada Mespotine's Avatar
 
Join Date: May 2017
Location: Leipzig
Posts: 6,628
Default

Use Defer from Ultraschall-Api. It's doing what you try to do and allows controlling the defer loop from another script as well, to pause execution of the deferred function or to even stop it.

And you can set it to run the deferred function every x seconds or defer-cycles.


https://mespotin.uber.space/Ultrasch...1_Introduction
__________________
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 04-30-2021, 07:12 AM   #8
nofish
Human being with feelings
 
nofish's Avatar
 
Join Date: Oct 2007
Location: home is where the heart is
Posts: 12,107
Default

Schwa once posted a wait/sleep Lua example:
https://forum.cockos.com/showthread....89#post1591089
nofish is offline   Reply With Quote
Old 05-01-2021, 08:25 PM   #9
daniellumertz
Human being with feelings
 
daniellumertz's Avatar
 
Join Date: Dec 2017
Location: Brazil
Posts: 2,008
Default

Quote:
Originally Posted by Meo-Ada Mespotine View Post
Use Defer from Ultraschall-Api. It's doing what you try to do and allows controlling the defer loop from another script as well, to pause execution of the deferred function or to even stop it.

And you can set it to run the deferred function every x seconds or defer-cycles.


https://mespotin.uber.space/Ultrasch...1_Introduction
Thanks mespotine!!! This a really usefull function instead of trying to do on my own a wait. Nice work!!! Work smoothly here!
daniellumertz is offline   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 11:04 PM.


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