Your examples where it doesn't work are because you didn't give it a function to defer. Plain and simple.
When you include ()s, you're telling Lua to call the function right then and there. That is:
Code:
reaper.defer(msg("test"))
is the same thing as
Code:
retval = msg("test")
reaper.defer(retval)
msg doesn't return anything, so
retval is
nil. You're telling
defer to call
nil as a function, which is what the error message says.
When you include quotes, you're telling Lua that everything in between is a string. That is:
Code:
reaper.defer('msg("test")')
is the same thing as
Code:
str = 'msg("test")'
reaper.defer(str)
str is a string. You're telling
defer to call it as a function, which is what the error message says.
The error messages are correct; they're telling you exactly what the problem is.
For comparison, let's look at a working version:
Code:
reaper.defer( function() msg("hello!") end )
is the same thing as
Code:
function wrap()
msg("hello!")
end
reaper.defer(wrap)
Note that there are no ()s attached to
wrap in the
defer call, because you don't WANT to call
wrap. You want to give
defer a function that IT can call later.
wrap() is not a function, just like
msg("hello!") is not a function - they're whatever the function returns. Consider:
Code:
y = math.floor( math.sin(x) )
We use this sort of structure all the time, because it avoids having to create a temporary variable for the value returned by
math.sin(x).
defer is behaving the same way.