Go Back   Cockos Incorporated Forums > REAPER Forums > ReaScript, JSFX, REAPER Plug-in Extensions, Developer Forum

Reply
 
Thread Tools Display Modes
Old 03-18-2017, 06:05 PM   #1
TonE
Human being with feelings
 
Join Date: Feb 2009
Location: Reaper HAS send control via midi !!!
Posts: 4,031
Default 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
TonE is offline   Reply With Quote
Old 03-18-2017, 07:33 PM   #2
cfillion
Human being with feelings
 
cfillion's Avatar
 
Join Date: May 2015
Location: Québec, Canada
Posts: 4,937
Default

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("^(.*[/\\])")
cfillion is offline   Reply With Quote
Old 03-18-2017, 08:26 PM   #3
Lokasenna
Human being with feelings
 
Lokasenna's Avatar
 
Join Date: Sep 2008
Location: Calgary, AB, Canada
Posts: 6,551
Default

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.
__________________
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-18-2017, 09:39 PM   #4
FnA
Human being with feelings
 
FnA's Avatar
 
Join Date: Jun 2012
Posts: 2,173
Default

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
FnA is offline   Reply With Quote
Old 03-19-2017, 03:56 AM   #5
RCJacH
Human being with feelings
 
Join Date: Apr 2016
Location: Beijing, China
Posts: 215
Default

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.
RCJacH is offline   Reply With Quote
Old 03-19-2017, 07:27 AM   #6
Lokasenna
Human being with feelings
 
Lokasenna's Avatar
 
Join Date: Sep 2008
Location: Calgary, AB, Canada
Posts: 6,551
Default

Quote:
Originally Posted by FnA View Post
I see there's something called Load or Loadstring here:
https://en.wikipedia.org/wiki/Eval
- 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.
__________________
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-19-2017, 04:14 PM   #7
FnA
Human being with feelings
 
FnA's Avatar
 
Join Date: Jun 2012
Posts: 2,173
Default

Thanks RCJacH and Lokasenna. (thanks also for those tutorial-type posts recently on string matching in lua)
FnA is offline   Reply With Quote
Old 05-20-2023, 06:26 AM   #8
AudioBabble
Human being with feelings
 
AudioBabble's Avatar
 
Join Date: Dec 2021
Location: Jupiter Island
Posts: 924
Default

Quote:
Originally Posted by RCJacH View Post
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.
AudioBabble is offline   Reply With Quote
Old 05-20-2023, 11:29 AM   #9
Fabian
Human being with feelings
 
Fabian's Avatar
 
Join Date: Sep 2008
Location: Sweden
Posts: 7,417
Default

Quote:
Originally Posted by AudioBabble View Post
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...
Fabian is offline   Reply With Quote
Old 05-21-2023, 09:43 AM   #10
Meo-Ada Mespotine
Human being with feelings
 
Meo-Ada Mespotine's Avatar
 
Join Date: May 2017
Location: Leipzig
Posts: 6,621
Default

require also uses different default-paths on different platforms. So it might work on Windows but not necessarily on Mac/Linux.
__________________
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 06-17-2023, 05:07 AM   #11
AudioBabble
Human being with feelings
 
AudioBabble's Avatar
 
Join Date: Dec 2021
Location: Jupiter Island
Posts: 924
Default

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?
AudioBabble is offline   Reply With Quote
Old 06-17-2023, 05:17 AM   #12
Meo-Ada Mespotine
Human being with feelings
 
Meo-Ada Mespotine's Avatar
 
Join Date: May 2017
Location: Leipzig
Posts: 6,621
Default

Quote:
Originally Posted by AudioBabble View Post
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...
__________________
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 06-17-2023, 04:57 PM   #13
AudioBabble
Human being with feelings
 
AudioBabble's Avatar
 
Join Date: Dec 2021
Location: Jupiter Island
Posts: 924
Default

Quote:
Originally Posted by Meo-Ada Mespotine View Post
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
AudioBabble is offline   Reply With Quote
Old 06-18-2023, 01:50 AM   #14
Meo-Ada Mespotine
Human being with feelings
 
Meo-Ada Mespotine's Avatar
 
Join Date: May 2017
Location: Leipzig
Posts: 6,621
Default

Quote:
Originally Posted by AudioBabble View Post
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
__________________
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 06-18-2023, 04:52 AM   #15
AudioBabble
Human being with feelings
 
AudioBabble's Avatar
 
Join Date: Dec 2021
Location: Jupiter Island
Posts: 924
Default

Quote:
Originally Posted by Meo-Ada Mespotine View Post
I wrote once something about it:
https://forum.cockos.com/showthread.php?t=250584
That's brilliant I've bookmarked that thread -- really good info, thank you.
AudioBabble 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 02:25 AM.


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