## Call Lua functions in an external file

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

### Call Lua functions in an external file

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

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:

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

Thank you, that works perfectly :)

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

### Re: Call Lua functions in an external file

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
Posts: 8206
Joined: 14 Dec 2012, 00:25

### Re: Call Lua functions in an external file

Hello Federico,

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

Cheers

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

### Re: Call Lua functions in an external file

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

Code: Select all


sim_stringparam_application_path