## Exchange Object Handle / Script decision

Typically: "How do I... ", "How can I... " questions
Tobias
Posts: 21
Joined: 08 Jan 2016, 14:35

### Exchange Object Handle / Script decision

Hello,

I again have a question. I tried in several ways to exchange an object handel form non-threaded to threaded script. I use simSet/GetIntegerSignal and the right handle / number is printed in the threaded script. But the function simGetObjectPosition does return an error (function's argument type is not correct).

What am I doing wrong here?

Another question only partly related with this is: Where would you recommend to specify joystick input, force feedback and audio output? Rather in a threaded or non-threaded script? I put all in threaded scripts but I am not sure if it is best when it comes to performance and synchronization.

Thanks so much for all your help,

Tobi

P.S.: I should mention that I use LuaJIT with ffi for joystick, force feedback and audio

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

### Re: Exchange Object Handle / Script decision

Hello Tobi,

if the object handle is correct, then it should be transmitted correctly. There are 2 things that could go wrong:
• You didn't get the correct object handle in the first place: the handle might depend on the location where the simGetObjectHandle function is called. Make sure you understood this page, the section Access from associated code.
• When you set a signal in one script, then you might have to wait next simulation step for the other script to resume and be able to read it. Child script are executed in a specific order.
About your second question: I would rather go with a non-threaded child script. With threaded child scripts you need to be more careful not to waste time, and call simSwitchThread yourself depending on the situation.

Cheers

Tobias
Posts: 21
Joined: 08 Jan 2016, 14:35

### Re: Exchange Object Handle / Script decision

Hi,
I can not find my mistake. I get get the handle with:

Code: Select all

if (sim_call_type==sim_childscriptcall_initialization) then
closest_handle = simGetObjectHandle('Target')
end

if (sim_call_type==sim_childscriptcall_actuation) then
simSetIntegerSignal("closest_handle", closest_handle)
end

and try to receive it with

Code: Select all

if (sim_call_type==sim_childscriptcall_initialization) then
handle = 99
end
if (sim_call_type==sim_childscriptcall_actuation) then
handle = simGetIntegerSignal("closest_handle")
targetPos = simGetObjectPosition(handle,-1)
end

If I print the handle it is correctly printed. If I do not send the signal I receive the obvious error that my object does not exist.

Cheers, Tobi

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

### Re: Exchange Object Handle / Script decision

The problem is very probably that your second script is initializing before the first script. At that moment the signal is not yet set.

You can set the execution priority or order for child script in the script dialog. This will however have an effect only if the two scripts are either both threaded or both non-threaded. On top of that, if your first script is built on the hierarchy of the second script, then will will not work either, because scripts are executed in a cascaded fashion (i.e. cascaded order: first the main script will execute. The main script will then first launch the threaded child scripts in a cascaded fashion (with simLaunchThreadedChildScripts), then the non-threaded child scripts (also in a cascaded fashion, with simHandleChildScripts).

You can of course also retrieve the handle of the same object from 2 different locations. To avoid having the automatic name adjustment to happen you can explicitely specify the full object name, e.g.:
• if your object name is myObject, specify myObject#
• if your object name is myObject#0, specify myObject#0
• ...
• if your object name is myObject#n, specify myObject#n
The automatic object name adjustment mechanism is mean to automatically have your copy-pasted working models operational, without having to adjust the scripts for different object names.

Cheers

Tobias
Posts: 21
Joined: 08 Jan 2016, 14:35

### Re: Exchange Object Handle / Script decision

Hi,

thanks for the answer. It works and you were right that the execution order was the problem.

Regarding the decision for the right kind of script:

I want to achieve a pulsation similar to the one Vrep integrates for collision detection (Emissive Color Component Pulsation). I tought the only possibility is to use a threaded child script as the pulsation lasts longer than one simulation step (e.g. frequency 2.000 Hz). Am I right with that? I am also struggling to get that pulsation effect. Is there a possibility to see how your integrated function achieves that?

Cheers and sorry for all my questions,

Tobi

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

### Re: Exchange Object Handle / Script decision

You can do this from a threaded or non-threaded child script:

from a threaded child script:

Code: Select all

threadFunction=function()
while simGetSimulationState()~=sim_simulation_advancing_abouttostop do
local t=simGetSimulationTime()
simSetShapeColor(shape,nil,sim_colorcomponent_emission,{0.5+math.sin(t)*0.5,0,0})
simSwitchThread()
end
end

shape=simGetObjectHandle('Cuboid')
res,initColor=simGetShapeColor(shape,nil,sim_colorcomponent_emission)

res,err=xpcall(threadFunction,function(err) return debug.traceback(err) end)
if not res then
simAddStatusbarMessage('Lua runtime error: '..err)
end

simSetShapeColor(shape,nil,sim_colorcomponent_emission,initColor)
From a non-threaded child script:

Code: Select all

if (sim_call_type==sim_childscriptcall_initialization) then
shape=simGetObjectHandle('Cuboid')
res,initColor=simGetShapeColor(shape,nil,sim_colorcomponent_emission)
end

if (sim_call_type==sim_childscriptcall_actuation) then
local t=simGetSimulationTime()
simSetShapeColor(shape,nil,sim_colorcomponent_emission,{0.5+math.sin(t)*0.5,0,0})
end

if (sim_call_type==sim_childscriptcall_cleanup) then
simSetShapeColor(shape,nil,sim_colorcomponent_emission,initColor)
end
Cheers