Sending integers as custom datablocks
Posted: 09 Jul 2018, 15:26
I'm using custom datablocks in quite a lot of ways to transfer information between models to both carry communications and say things like "you are in range of my sensor". But in some cases I only need to send really basic information across, in some cases all I need is for one object's child script to send a message to a custom datablock on another of a single value. So that the receiving object's child script can consult that object's custom datablocks and know things like whether the sending object was telling it to change mode or other such things. A really simple example is one object writing a 1 to the custom datablock of another to tell the other to turn a behavioud on, and a 0 is written to the custom datablock when the receiving object is to turn off a behaviour. But custom datablocks seem to only work for packed tables, even though i just want to send an i9nteger number across I'm having to pack it to a table first and then unpack it at the other end. See my code below:
These functions can be called during a specific step of a simulation on a sending object to turn an "LED" behaviour on another object on or off:
The object with the LED behaviour runs the following every step to see if the custom datablock's value is currently 1 or 0
All this packing and unpacking is bothersome and must surely slow stuff down quite a bit, but thingsw don't seem to work if I just try to send numerical values like this without first packing and afterwards unpacking. Is there any way to use custom datablocks with integers rather than packed tables, like the way one can do with int. float or string signals?
Thank you
These functions can be called during a specific step of a simulation on a sending object to turn an "LED" behaviour on another object on or off:
Code: Select all
function EnableLED(LEDHandle)
OnTable={1}
sim.writeCustomDataBlock(LEDHandle, 'LEDstatus', sim.packTable(OnTable))
end
function DisableLED(LEDHandle)
OffTable={0}
sim.writeCustomDataBlock(LEDHandle, 'LEDstatus', sim.packTable(OffTable))
end
Code: Select all
LEDstatusPacked=sim.readCustomDataBlock(MyHandle,'LEDstatus') --checks whether this LED should be off or on, as set by the script on the other object
LEDstatusTable=sim.unpackTable(LEDstatusPacked)--various conversion is done
LEDstatus=LEDstatusTable[1]--and finally we get the status into a simple number form
if (LEDstatus==1) then
--stuff is done here
end
Thank you