Sending integers as custom datablocks

Typically: "How do I... ", "How can I... " questions
Post Reply
RobAtLab
Posts: 46
Joined: 10 Jan 2018, 17:49

Sending integers as custom datablocks

Post by RobAtLab » 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:

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

The object with the LED behaviour runs the following every step to see if the custom datablock's value is currently 1 or 0

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
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

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

Re: Sending integers as custom datablocks

Post by coppelia » 10 Jul 2018, 07:53

Hello,

custom data blocks basically store a string inside of a scene, or a specific object. So you could write:

Code: Select all

sim.writeCustomDataBlock(objectHandle,'dataTag','1')
In above example, 1 will be stored as a string. When reading that data, you might have to do:

Code: Select all

local value=sim.readCustomDataBlock(objectHandle,'dataTag')
value=tonumber(value)
But I would always use a packed table:
  • it allows you to easily store almost any type of data
  • it is very fast (unless you have pictures, etc.)
  • it is elegant and easily extensible. e.g. you can do following:

Code: Select all

local data={}
data.version=1
data.author='Coppelia'
data.dependencies={}
data.dependencies.robotA={...}
data.dependencies.robotZ={...}
sim.writeCustomDataBlock(objectHandle,'dataTag',sim.packTable(data))
if at a later time you decide you don't need field dependencies anymore, but rather a list of robots, you can do following:

Code: Select all

local data=sim.readCustomDataBlock(objectHandle,'dataTag')
data=sim.unpackTable(data)
data.dependencies=nil -- remove dependencies
data.robotList={'robotA',robotZ'}
data=sim.packTable(data)
sim.writeCustomDataBlock(objectHandle,'dataTag',data)
Cheers

RobAtLab
Posts: 46
Joined: 10 Jan 2018, 17:49

Re: Sending integers as custom datablocks

Post by RobAtLab » 18 Jul 2018, 14:19

Thanks for explaining that

Post Reply