Page 1 of 1

"this" in lua

Posted: 04 Mar 2013, 09:16
by FieteBotschen
Hi all,

is there a possibilty to address in a script the object of the scrip (like this in c++)?
I would like to create a script for an object and then copy the object. Therefore, it would be nice to be able to address the script's object.

Best, Fiete

Re: "this" in lua

Posted: 04 Mar 2013, 12:53
by Eric
Hello

I am not sure if it is what you are willing to do but if you want to reuse/share some code from "outside of vrep" (e.g. a library of commonly used functions or a configuration files), you can do as follow:

Code: Select all

LuaLibPath="C:/Myfirst/path/to/thelibrary/?.lua ; D:\AnOther\PathToThisLibrary\?.lua;" 
-- to point to the lua file (lua will first look for the file in the first path, if not in ther in the second etc etc)

package.path = package.path .. LuaLibPath
require("myLibrary") -- myLibrary.lua
require("myOtherLib") --other lib in on of the folders in LuaLibPath 

foo,bar= getFooBarFromExternalFunc()
I hope it helps
cheers
Eric

Re: "this" in lua

Posted: 04 Mar 2013, 12:57
by coppelia
Hello Fiete,

What are you trying to do exactly? Do you want to exchange information between several child scripts?
Or what is the purpose of wanting to point to a specific script?

Cheers

Re: "this" in lua

Posted: 04 Mar 2013, 12:58
by FieteBotschen
Hi,

thanks Eric, but i meant something else.

E.g. when I get an object handle by simGetObjectHandle in my script and later copy the corresponding object the script of the copied object would still get the object handle of the original object and not the copied one! In this case, something like "this" would be awesome!

Best, Fiete

Re: "this" in lua

Posted: 04 Mar 2013, 13:15
by coppelia
Ah, I see!

When retrieving object handles from within a child script, there is actually an automatic mechanism in V-REP that goes like that:

If your script retrieves some handles, then there will be an automatic object name adjustment.
Each script/object can have a name suffix (e.g. "#0", "#42", etc.)
The name suffix will be used to determine what objects are accessed from within that script.

You can retrieve object handles implicitely with:

Code: Select all

simGetObjectHandle("myObject")
which could retrieve handles of objects "myObject", "myObject#0", "myObject#1", etc. (which of those is retrieved depends on your script's name suffix)

Or you can retireve object handles explicitely with:

Code: Select all

simGetObjectHandle("myObject#")
simGetObjectHandle("myObject#0")
simGetObjectHandle("myObject#42")
which will retrieve respectively object handles "myObject", "myObject#0" and "myObject#42" (and this is independent of the script's name suffix)

This automatic mechanism is very powerful and allows to copy-paste objects including attached child scripts without the need to adjust an code.

Cheers

Re: "this" in lua

Posted: 04 Mar 2013, 16:34
by FieteBotschen
Hey,

thanks, that is really an awesome feature. Is there a possibilty to remove the '#' from the name suffix?

Why do I ask? Well, I want to be able to copy Quadrotors with ROS publisher and subscribers. Those quadrotors should send all their pose etc. data on seperate topics. I am doing this right now by building the name out of the object name and a string (e.g.
simExtROS_enablePublisher(copterName..'copterVelocityZ',...))

The problem is, that V-REP crashes as soon as I copy another quadrotor because ROS doesnt allows topics to be have an '#' in their name!

Best, Fiete

Re: "this" in lua

Posted: 04 Mar 2013, 16:45
by coppelia
If you remove the "#" from the name, then the automatic mechanism won't work anymore.
What I would do is simply replace the "#" in the topic name with a char that ROS supports, and when the data comes back replace that char with a "#" again.
Object names in V-REP follow a naming convention. Allowed are:
  • Chars "A"-"Z"
  • Chars "a"-"z"
  • Chars "0"-"9"
  • underscore ("_")
  • one single "#", followed by a number
Cheers

Re: "this" in lua

Posted: 05 Mar 2013, 09:59
by FieteBotschen
Thanks again for your fast and reliable support!