Struggle with callScriptFunction

Typically: "How do I... ", "How can I... " questions
Post Reply
RL_Niryo
Posts: 14
Joined: 03 Oct 2019, 17:05

Struggle with callScriptFunction

Post by RL_Niryo » 10 Oct 2019, 10:47

Hi, it's me (again ^^),

Thanks to your help, we succeed in reproducing our factory case in V-REP !
It pick Cans from a Conveyor Belt (the cans are automatically generated) and store them on automatically generated cardboard.
Each object (Producer, Conveyor, ...) has its own script and we now want to centralized everything on a controller which aims to be the master which call functions from each script.

I'm currently struggling with the use of sim.callScriptFunction =>

My controller's Threaded child script need to call a function from my producer's non-threaded child

The function to call is the following (and is written directly in the script associated to the object "Producer_plate"

Code: Select all

function gen_dummies(h,grid_shape,plate_shape,can_shape)
   print("I'm Here")
   # SOME STUFF WHICH IS WORKING WHEN DIRECTLY CALLED FROM THE SCRIPT
   return list_handle_dummies
end

and I'm calling it this way from my controller Threaded Child

Code: Select all

prod_p = sim.getObjectHandle("Producer_plate")
prod_p_script = sim.getScriptAssociatedWithObject(prod_p)
handle_ready_plate = sim.getScriptSimulationParameter(cvfill_script, "objectHandle")
print(prod_p,handle_ready_plate,grid_shape,plate_shape,can_shape)
list_handle_dummies=sim.callScriptFunction("gen_dummies@Producer_plate",prod_p_script,handle_ready_plate,grid_shape,plate_shape,can_shape)
(I'm printing parameters before callScriptFunction to be sure parameters are valids)

It result to the error
[string "CHILD SCRIPT Controller"]:75: Failed calling script function. (sim.callScriptFunction)

Can you explain me where I miss-use the function ? Is this a way to "allow" the access to the function that I forgot ?

Merci d'avance / Thank you in advance

Cheers

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

Re: Struggle with callScriptFunction

Post by coppelia » 15 Oct 2019, 10:17

Hello,

from the documentation:
Calls a script function (from a plugin, the main client application, or from another script). This represents a callback inside of a script. Call this only:
a) from the main thread, or:
b) from a thread that originated from a threaded child script. In that case, you cannot call non-threaded child scripts.
So unfortunately you cannot call function in a non-threaded script, if you are running in a script.
Only use threaded scripts where it really makes sense (they are less efficient if not programmed correctly).

Also, by design, V-REP scripts are each running in their individual states, and are isolated from the others. This makes it easier to write distributed code, and more difficult to write non-distributed code.

Practically, scripts in V-REP run sequentially (with threads behaving more like co-routines). Execution order is important and can be precisely controlled. This also means that often you only want to inform another script about some even, some data, etc. For that, it is much more convenient to use sim.writeCustomDataBlock / sim.readCustomDataBlock, like in following example:

Code: Select all

-- script1:
local data={}
data.enabled=true
data.name="hello"
data.array={1,2,3}
sim.writeCustomDataBlock(object2,'myData',sim.packTable(data))

-- script2, associated with object2:
local tmp=sim.readCustomDataBlock(object2,'myData')
if tmp then
    local data=sim.unpackTable(tmp)
    sim.writeCustomDataBlock(object2,'myData','') -- clear the data again
end
Cheers

RL_Niryo
Posts: 14
Joined: 03 Oct 2019, 17:05

Re: Struggle with callScriptFunction

Post by RL_Niryo » 15 Oct 2019, 11:17

coppelia wrote:
15 Oct 2019, 10:17
Cheers
Thanks, I will know that for future works !

Have a good day

Post Reply