Go Back   Cockos Incorporated Forums > REAPER Forums > REAPER Feature Requests

Reply
 
Thread Tools Display Modes
Old 03-22-2019, 06:44 AM   #1
Meo-Ada Mespotine
Human being with feelings
 
Meo-Ada Mespotine's Avatar
 
Join Date: May 2017
Location: Leipzig
Posts: 6,621
Default reaper.defer shall allow passing of parameters

I often run into one issue, that deferred functions forget the values of their own local variables.

The following code produces a nil-error, when the deferred function is run the second time:

Code:
function main(counter)
   counter=counter+1
   reaper.defer(main)
end

main(1)
So I would love to suggest, that the defer-function allows passing parameters as well, with which I can "memorise" the local variables.
If I don't pass a parameter, the deferred-function behaves as it already does right now, that the local parameter-variables are set as nil. So already existing scripts shouldn't break with that.

Code:
function main(counter)
   counter=counter+1
   reaper.defer(main, counter) -- like this way
end

main(1)

Otherwise I can use only global variables to keep their values, but this could be potentially dangerous in some situations. I love to keep variables local for the functions who are the only ones needing them.

Making them global means potential accidental overwriting of such a global variable an I'd love to avoid that.
__________________
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 03-22-2019, 10:57 AM   #2
cfillion
Human being with feelings
 
cfillion's Avatar
 
Join Date: May 2015
Location: Québec, Canada
Posts: 4,937
Default

Code:
reaper.defer(function() main(counter) end)
cfillion is offline   Reply With Quote
Old 03-22-2019, 01:50 PM   #3
Lokasenna
Human being with feelings
 
Lokasenna's Avatar
 
Join Date: Sep 2008
Location: Calgary, AB, Canada
Posts: 6,551
Default

Hooray for closures.

Code:
function deferWithArgs (func, ...)
 return function ()
   func(...)
 end
end

local function main(counter)
  counter = counter + 1
  reaper.defer(deferWithArgs(main, counter))
end

main(1)
__________________
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 03-23-2019, 02:15 AM   #4
Meo-Ada Mespotine
Human being with feelings
 
Meo-Ada Mespotine's Avatar
 
Join Date: May 2017
Location: Leipzig
Posts: 6,621
Default

Holy Cow! Big big thanks!
__________________
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 05-03-2023, 06:08 AM   #5
Buy One
Human being with feelings
 
Buy One's Avatar
 
Join Date: Sep 2019
Posts: 1,132
Default

Quote:
Originally Posted by Lokasenna View Post
Hooray for closures.

Code:
function deferWithArgs (func, ...)
 return function ()
   func(...)
 end
end

This may have worked before, but not any longer

This version works currently

PHP Code:
function deferWithArgs(func,...)
local t = {...}
return function() 
func(table.unpack(t)) end
end 
Without the table the arguments remain invisible to the returned function and the API throws an error
.
.
.
.
__________________
https://github.com/Buy-One/REAPER-scripts (175)
REAPER is a DAW whose user guide file is larger than its installation file
Buy One is online now   Reply With Quote
Old 11-26-2023, 10:40 AM   #6
nscotto
Human being with feelings
 
Join Date: Jul 2020
Posts: 84
Default

Quote:
Originally Posted by Buy One View Post
This may have worked before, but not any longer

This version works currently

PHP Code:
function deferWithArgs(func,...)
local t = {...}
return function() 
func(table.unpack(t)) end
end 
Without the table the arguments remain invisible to the returned function and the API throws an error
.
.
.
.
Hi, facing some weirdness with this too.

I have a global table of parameters with nested tables. Some of these parameters need to be passed to the deferred function.

I don't quite understand your answer, could you elaborate?
I am not hugely familiar with lua too, just getting my grips on it.

I would really appreciate if have the time to check my project's repo issue directly.


EDIT: Solved it

It was due to the way I initialise things with ReaImgui.
Putting inital nil values was unecessary in the end.

Last edited by nscotto; 11-26-2023 at 12:05 PM.
nscotto 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 04:14 AM.


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