Explicit and non-explicit calls
Some objects, such as proximity sensors, normally require handling or calculations to be performed once per simulation step, to reflect a possible change in the scene. This handling can be done implicitely or explicitely, and those objects have a special setting related to that: an explicit handling flag. By default that flag is unchecked, which means that the object will be implicitely handled via the main script. Indeed the following main script code will be executed and handle said objects once per simulation step:
sim.handleProximitySensor(sim.handle_all_except_explicit)
Above code will perform proximity sensor computations, and any other script can then read the up-to-date calculation results with:
local calculationResult=sim.readProximitySensor(sensorHandle)
Above code, unlike the previous, will not compute anything, but just read the calculation result. This is very convenient when calculation need to happen exactly once per simulation step and/or the calculation result is required by several scripts.
If however, an object requires less or more frequent calculations (e.g. several calculations within a same simulation step), then above mechanism does not work. In that case, the object should be flagged as explicit handling: from that moment on, the main script will not handle that object anymore, and it is the responsibility of another script to do this. For instance if the proximity sensor should only be handled once every other simulation step, a child script could include following code:
function sysCall_init()
sensorHandle=sim.getObject('/myProximitySensor')
end
function sysCall_sensing()
handleSensor=not handleSensor
if handleSensor then
sim.handleProximitySensor(sensorHandle)
end
end
If new calculations need to be performed several times within a same simulation step (e.g. because an object or the sensor itself is moved more frequently), one could write:
function sysCall_init()
sensorHandle=sim.getObject('/myProximitySensor')
end
function sysCall_sensing()
for i=1,10,1 do
local pos=sim.getObjectPosition(sensorHandle,-1)
pos[3]=i/10
sim.setObjectPosition(sensorHandle,-1,pos) -- e.g. move the sensor
local calculationResults=sim.handleProximitySensor(sensorHandle)
end
end
|