To write a union just set the field you want to and write:
sendUnion.lua
local myWriter = CONTAINER.WRITER['MyPublisher::MyWriter']
-- The union has 3 cases:
-- * aLong
-- * aShort
-- * aLongOpt
-- If you want to set aShort just set that field by name, the
-- discriminator will be set automatically:
myWriter.instance['aShort'] = 3
myWriter:write();
To read it, first get the name of the selected field, then access it:
getUnion.lua:
local myReader = CONTAINER.READER['MySubscriber::MyReader']
myReader:take()
for i, sample in ipairs(myReader.samples) do
if (not myReader.infos[i].valid_data) then
print("\t invalid data!")
else
-- If you are trying to retrieve a Union,
-- first you have to find out wich field has been sent:
local sentFieldName = sample['#']
-- once you have the name you can just get the value like this:
local value = sample[sentFieldName];
print('I received ' .. value .. ' for ' .. sentFieldName)
end
end
I attached a zip file here with the example. To run it open to terminal and type:
Hello Sneha,
Let's assume your type is a union like this:
Union.xml
<types> <union name="AUnionType"> <discriminator type="long"/> <case> <caseDiscriminator value="0"/> <member type="long" name="aLong"/> </case> <case> <caseDiscriminator value="1"/> <member type="short" name="aShort"/> </case> <case> <caseDiscriminator value="2"/> <member optional="true" type="long" name="aLongOpt"/> </case> </union> </types>To write a union just set the field you want to and write:
sendUnion.lua
To read it, first get the name of the selected field, then access it:
getUnion.lua:
local myReader = CONTAINER.READER['MySubscriber::MyReader'] myReader:take() for i, sample in ipairs(myReader.samples) do if (not myReader.infos[i].valid_data) then print("\t invalid data!") else -- If you are trying to retrieve a Union, -- first you have to find out wich field has been sent: local sentFieldName = sample['#'] -- once you have the name you can just get the value like this: local value = sample[sentFieldName]; print('I received ' .. value .. ' for ' .. sentFieldName) end endI attached a zip file here with the example. To run it open to terminal and type:
and:
You can find more information in the Prototyper Getting Started Guide here.
Please let me know if this answers your questions!
Gianpiero
v
Thank you so much :)