Page 1 of 1

Call Lua functions in an external file

Posted: 12 Sep 2013, 15:31
by Yann
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?

Re: Call Lua functions in an external file

Posted: 12 Sep 2013, 18:37
by Eric
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)

Re: Call Lua functions in an external file

Posted: 13 Sep 2013, 10:01
by Yann
Thank you, that works perfectly :)

Re: Call Lua functions in an external file

Posted: 13 Oct 2015, 15:17
by fferri
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?

Re: Call Lua functions in an external file

Posted: 13 Oct 2015, 15:27
by coppelia
Hello Federico,

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

Cheers

Re: Call Lua functions in an external file

Posted: 12 Sep 2017, 03:02
by ahundt
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