Call Lua functions in an external file

Typically: "How do I... ", "How can I... " questions
Post Reply
Yann
Posts: 4
Joined: 11 Feb 2013, 16:41

Call Lua functions in an external file

Post by Yann » 12 Sep 2013, 15:31

Can I call a function that I have in a file?
I mean, from several child script, I want to call a collection of utility functions that I have in a file "myFunctions.lua".
How to?

Eric
Posts: 186
Joined: 11 Feb 2013, 16:39

Re: Call Lua functions in an external file

Post by Eric » 12 Sep 2013, 18:37

Hi!

Yes you can call an external lua file with your functions in it from a V-REP script. It will be equivalent to have the functions copied in your V-REP scripts. To do so:

In your external myfunctions.lua located under D:path/To/File/, you write your functions

Code: Select all

function foo(bar)
    return bar
end

function bar(foo)
   return(foo)
end
In your V-REP script you should execute once:

Code: Select all

package.path = package.path .. "D:path/To/File/?.lua;"
require("myfunctions")

and later in this V-REP script you call the functions as if they were inside the V-REP script itself

Code: Select all

data=foo(something)
data2=bar(somethingelse)

Yann
Posts: 4
Joined: 11 Feb 2013, 16:41

Re: Call Lua functions in an external file

Post by Yann » 13 Sep 2013, 10:01

Thank you, that works perfectly :)

fferri
Posts: 92
Joined: 09 Sep 2013, 19:28

Re: Call Lua functions in an external file

Post by fferri » 13 Oct 2015, 15:17

is it possible to have that path relative to the scene?

i.e. is there a function to retrieve the path of current scene, so that it can be added to package.path?

coppelia
Site Admin
Posts: 6185
Joined: 14 Dec 2012, 00:25

Re: Call Lua functions in an external file

Post by coppelia » 13 Oct 2015, 15:27

Hello Federico,

yes, you have following two functions available:
by the way, thanks for dropping by at the IROS 2015 ;)

Cheers

ahundt
Posts: 111
Joined: 29 Jan 2015, 04:21

Re: Call Lua functions in an external file

Post by ahundt » 12 Sep 2017, 03:02

It seems there are some new ones for those that come across this in the future:

Code: Select all


sim_stringparam_application_path
can only be read
sim_stringparam_scene_path_and_name
can only be read
sim_stringparam_scene_path
can only be read
sim_stringparam_scene_name
can only be read
sim_stringparam_video_filename
path + filename without extension

Post Reply