|
|
|
03-18-2017, 06:05 PM
|
#1
|
Human being with feelings
Join Date: Feb 2009
Location: Reaper HAS send control via midi !!!
Posts: 4,031
|
Script which runs a function from another script. Possible?
Is it possible to write a script as action, which just runs an existing function from another .lua?
function x
run function y from test.lua
|
|
|
03-18-2017, 07:33 PM
|
#2
|
Human being with feelings
Join Date: May 2015
Location: Québec, Canada
Posts: 4,937
|
Code:
dofile('path_to_the_file_containing_function_y')
y()
If you also need to know the absolute path of the directory containing of the action currently running:
Code:
local dir = ({reaper.get_action_context()})[2]:match("^(.*[/\\])")
|
|
|
03-18-2017, 08:26 PM
|
#3
|
Human being with feelings
Join Date: Sep 2008
Location: Calgary, AB, Canada
Posts: 6,551
|
dofile will run the entire second script though; you can't just access a single a function on its own unless the script is specifically written that way.
|
|
|
03-18-2017, 09:39 PM
|
#4
|
Human being with feelings
Join Date: Jun 2012
Posts: 2,173
|
I've used eval() in eel, but nothing similar in Lua. (not for TonE's specific request) Might that be a possible way?
I see there's something called Load or Loadstring here:
https://en.wikipedia.org/wiki/Eval
|
|
|
03-19-2017, 03:56 AM
|
#5
|
Human being with feelings
Join Date: Apr 2016
Location: Beijing, China
Posts: 215
|
Few ways to do it.
My favorite is to just return a table of functions at the end of a file, then require that file in your action script. Call by table value.
file 1.lua:
Code:
function fn_print(value)
print(value)
end
return {print = fn_print}
file 2.lua:
Code:
--you need to get the path of the first script by whatever means, then:
local fn = require ("file 1")
fn.print("Test")
To see this in action, check out my Vimper Solo script.
|
|
|
03-19-2017, 07:27 AM
|
#6
|
Human being with feelings
Join Date: Sep 2008
Location: Calgary, AB, Canada
Posts: 6,551
|
Quote:
Originally Posted by FnA
|
- There's also loadfile for a situation like this.
- loadstring is deprecated because load works on strings too.
They're pretty much the same as dofile, though - the only difference is that dofile loads AND runs the target file, whereas loadfile just, well, loads it into a variable. i.e.
Code:
retval = dofile(stuff.lua)
--> spits out stuff.lua's return value
vs
local stuff = load(stuff.lua)
retval = stuff()
--> spits out stuff.lua's return value
I use something similar in Radial Menu to load the user settings - it saves them as a legal Lua table, so reloading settings is just a matter of opening the file and running it.
|
|
|
03-19-2017, 04:14 PM
|
#7
|
Human being with feelings
Join Date: Jun 2012
Posts: 2,173
|
Thanks RCJacH and Lokasenna. (thanks also for those tutorial-type posts recently on string matching in lua)
|
|
|
05-20-2023, 06:26 AM
|
#8
|
Human being with feelings
Join Date: Dec 2021
Location: Jupiter Island
Posts: 924
|
Quote:
Originally Posted by RCJacH
Few ways to do it.
My favorite is to just return a table of functions at the end of a file, then require that file in your action script. Call by table value.
file 1.lua:
Code:
function fn_print(value)
print(value)
end
return {print = fn_print}
file 2.lua:
Code:
--you need to get the path of the first script by whatever means, then:
local fn = require ("file 1")
fn.print("Test")
To see this in action, check out my Vimper Solo script.
|
Old thread I know, but just to say -- here's what I found when doing this:
the path of the script seems to be expected at (on Windows at least) c:\Program Files\ REAPER (x64)\lua
Also, there is no need to add extension, it is expected to be .lua
- There was no lua folder in the expected location, so either you can create one and copy the relevant scripts there -- or more convenient is to create a symlink from REAPER\scripts folder.
- once this folder is created, it is simply necessary to put the filename, without extension exactly like the example above.
|
|
|
05-20-2023, 11:29 AM
|
#9
|
Human being with feelings
Join Date: Sep 2008
Location: Sweden
Posts: 7,417
|
Quote:
Originally Posted by AudioBabble
Old thread I know, but just to say -- here's what I found when doing this:
the path of the script seems to be expected at (on Windows at least) c:\Program Files\ REAPER (x64)\lua
Also, there is no need to add extension, it is expected to be .lua
- There was no lua folder in the expected location, so either you can create one and copy the relevant scripts there -- or more convenient is to create a symlink from REAPER\scripts folder.
- once this folder is created, it is simply necessary to put the filename, without extension exactly like the example above.
|
This might hold for the default installation of 64-bit Reaper, but not so for 32-bit Reaper, and not for a portable install, so this cannot be relied on. To be safe, you have to get the path to the scripts folder by some other means, see cfillion's post above.
Also, I think that dofile requires the extension, whereas loadfile does not.
__________________
// MVHMF
I never always did the right thing, but all I did wasn't wrong...
|
|
|
05-21-2023, 09:43 AM
|
#10
|
Human being with feelings
Join Date: May 2017
Location: Leipzig
Posts: 6,621
|
require also uses different default-paths on different platforms. So it might work on Windows but not necessarily on Mac/Linux.
|
|
|
06-17-2023, 05:07 AM
|
#11
|
Human being with feelings
Join Date: Dec 2021
Location: Jupiter Island
Posts: 924
|
Just stumbled back upon this post.
Seems that 'require' should work like 'import' in Python, but there are many path-related problems introduced within a Reaper environment (I tried it with a portable install and it really doesn't seem to want to play ball!)
So the best answer seems to be 'dofile' or 'loadfile', but the proviso is that at some point the whole script will be run, not just the function you want.
So it's a case of either manually copying the function into your code, or stripping out the executed parts of the script to just return the functions -- which could get tricky if it's a complicated script.
Are there any other ways to get 'library import'-like functionality in Reascript?
|
|
|
06-17-2023, 05:17 AM
|
#12
|
Human being with feelings
Join Date: May 2017
Location: Leipzig
Posts: 6,621
|
Quote:
Originally Posted by AudioBabble
Just stumbled back upon this post.
Seems that 'require' should work like 'import' in Python, but there are many path-related problems introduced within a Reaper environment (I tried it with a portable install and it really doesn't seem to want to play ball!)
So the best answer seems to be 'dofile' or 'loadfile', but the proviso is that at some point the whole script will be run, not just the function you want.
So it's a case of either manually copying the function into your code, or stripping out the executed parts of the script to just return the functions -- which could get tricky if it's a complicated script.
Are there any other ways to get 'library import'-like functionality in Reascript?
|
You mean, that only imports one function from a library?
What is your usecase? Maybe you think of the problem from a too complicated angle...
|
|
|
06-17-2023, 04:57 PM
|
#13
|
Human being with feelings
Join Date: Dec 2021
Location: Jupiter Island
Posts: 924
|
Quote:
Originally Posted by Meo-Ada Mespotine
You mean, that only imports one function from a library?
What is your usecase? Maybe you think of the problem from a too complicated angle...
|
No specific use-case, just trying to understand things a bit better. I'm making my way through the lua.org tutorial, where it says 'require' is the preferred function for loading libraries, but in Reascript, it seems dofile is more appropriate.
...and I was thinking more of api type scripts -- lots of functions made available, like I've noticed in ultrashall scripts, pretty much the first line of code is dofile calling the ultraschall_api script
|
|
|
06-18-2023, 01:50 AM
|
#14
|
Human being with feelings
Join Date: May 2017
Location: Leipzig
Posts: 6,621
|
Quote:
Originally Posted by AudioBabble
No specific use-case, just trying to understand things a bit better. I'm making my way through the lua.org tutorial, where it says 'require' is the preferred function for loading libraries, but in Reascript, it seems dofile is more appropriate.
...and I was thinking more of api type scripts -- lots of functions made available, like I've noticed in ultrashall scripts, pretty much the first line of code is dofile calling the ultraschall_api script
|
I wrote once something about it:
https://forum.cockos.com/showthread.php?t=250584
|
|
|
06-18-2023, 04:52 AM
|
#15
|
Human being with feelings
Join Date: Dec 2021
Location: Jupiter Island
Posts: 924
|
Quote:
Originally Posted by Meo-Ada Mespotine
|
That's brilliant I've bookmarked that thread -- really good info, thank you.
|
|
|
Thread Tools |
|
Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -7. The time now is 02:25 AM.
|