"this" in lua

Post links to your CoppeliaSim videos, scenes and models
Post Reply
FieteBotschen

"this" in lua

Post 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

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

Re: "this" in lua

Post 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

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

Re: "this" in lua

Post 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

FieteBotschen

Re: "this" in lua

Post 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

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

Re: "this" in lua

Post 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

FieteBotschen

Re: "this" in lua

Post 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

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

Re: "this" in lua

Post 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

FieteBotschen

Re: "this" in lua

Post by FieteBotschen »

Thanks again for your fast and reliable support!

Post Reply