# Referencing a Data Type structue in a script

**URL:** <https://forum.inductiveautomation.com/t/referencing-a-data-type-structue-in-a-script/10212>\
**Category:** Ignition\
**Created:** [July 10, 2015, 5:54pm UTC](https://forum.inductiveautomation.com/t/referencing-a-data-type-structue-in-a-script/10212 "2015-07-10T17:54:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![eric129](https://avatars.discourse-cdn.com/v4/letter/e/7ea924/32.png) [@eric129](https://forum.inductiveautomation.com/u/eric129)\
**Post date:** [July 10, 2015, 5:54pm UTC](https://forum.inductiveautomation.com/t/referencing-a-data-type-structue-in-a-script/10212/1 "2015-07-10T17:54:19Z")

</div>

Say I have a Data Type defined, “MyData”, which has three tags in it, Name, Date, Size.

I have an instance of that type called “MyComplexTag”.

Is it possible in a script to read the base tag and then reference the subtags within it?

e.g.:

myInfo = system.tag.read(“MyComplexTag”)  
name = myInfo.Name  
date = myInfo.Date  
size = myInfo.Size

What I’ve tried doesn’t work and I don’t see this documented anywhere.

Thanks.

---

<div class="post-metadata">

**Author:** ![diat150](https://avatars.discourse-cdn.com/v4/letter/d/a587f6/32.png) [@diat150](https://forum.inductiveautomation.com/u/diat150)\
**Post date:** [July 10, 2015, 6:18pm UTC](https://forum.inductiveautomation.com/t/referencing-a-data-type-structue-in-a-script/10212/2 "2015-07-10T18:18:53Z")

</div>

```auto

name = system.tag.read("MyComplexTag/name").value
date = system.tag.read("MyComplexTag/date").value
size = system.tag.read("MyComplexTag/size").value
```

I havent fooled with UDT’s but I think this would work. or you could use system.tag.readAll and grab all 3 values in one call.

---

<div class="post-metadata">

**Author:** ![eric129](https://avatars.discourse-cdn.com/v4/letter/e/7ea924/32.png) [@eric129](https://forum.inductiveautomation.com/u/eric129)\
**Post date:** [July 10, 2015, 7:42pm UTC](https://forum.inductiveautomation.com/t/referencing-a-data-type-structue-in-a-script/10212/3 "2015-07-10T19:42:21Z")

</div>

While I’m certain that would work, I’m interested if they can be referenced as I described.

Thanks for the response.

---

<div class="post-metadata">

**Author:** ![Pat](https://avatars.discourse-cdn.com/v4/letter/p/8491ac/32.png) [@Pat](https://forum.inductiveautomation.com/u/Pat)\
**Post date:** [July 10, 2015, 7:58pm UTC](https://forum.inductiveautomation.com/t/referencing-a-data-type-structue-in-a-script/10212/4 "2015-07-10T19:58:14Z")

</div>

If you have more than a few tags to read then system.tag.readAll() is much more efficient.

[code]udtPath = “[default]MyComplexTag”  
udtTags = ["/Name", “/Date”, “/Size”]

#create tags to read based on the udtPath and udtTags  
tagsToRead = []  
for tag in udtTags:  
tagsToRead.append(udtPath + tag)

#read tag values  
tagValues = system.tag.readAll(tagsToRead)

#print values  
for tagValue in tagValues:  
print tagValue.value

#or you could do  
print tagValues[0].value  
print tagValues[1].value  
print tagValues[2].value[/code]

---

<div class="post-metadata">

**Author:** ![diat150](https://avatars.discourse-cdn.com/v4/letter/d/a587f6/32.png) [@diat150](https://forum.inductiveautomation.com/u/diat150)\
**Post date:** [July 10, 2015, 10:11pm UTC](https://forum.inductiveautomation.com/t/referencing-a-data-type-structue-in-a-script/10212/5 "2015-07-10T22:11:27Z")

</div>

[quote=“eric129”]While I’m certain that would work, I’m interested if they can be referenced as I described.

Thanks for the response.[/quote]

I dont think so, but you may be able to use the browseTags function to grab all of the available tagnames in your structure first and then use readAll to grab the values for the tags that browseTags finds.

---

<div class="post-metadata">

**Author:** ![eric129](https://avatars.discourse-cdn.com/v4/letter/e/7ea924/32.png) [@eric129](https://forum.inductiveautomation.com/u/eric129)\
**Post date:** [July 10, 2015, 10:48pm UTC](https://forum.inductiveautomation.com/t/referencing-a-data-type-structue-in-a-script/10212/6 "2015-07-10T22:48:32Z")

</div>

I’m thinking about using a readall and converting the results to a dictionary so it would be flexible, but if the read (and write) of the structure did this internally it would be a lot cleaner.

---

<div class="post-metadata">

**Author:** ![Pat](https://avatars.discourse-cdn.com/v4/letter/p/8491ac/32.png) [@Pat](https://forum.inductiveautomation.com/u/Pat)\
**Post date:** [July 11, 2015, 7:51pm UTC](https://forum.inductiveautomation.com/t/referencing-a-data-type-structue-in-a-script/10212/7 "2015-07-11T19:51:07Z")

</div>

You could create a function like this -

[code]#udtPath determines dictionary keys, pay attention to the final backslash  
udtPath = “[myTagProvider]MyUdtFolder/”

#get tag paths for the ‘udt’  
udtTagPaths = system.tag.browseTags(parentPath=udtPath, recursive=True, sort=“ASC”)

udtTags = []  
for tag in udtTagPaths:  
if not tag.isFolder() and not tag.isUDT():   
udtTags.append(tag.fullPath)

tagValues = system.tag.readAll(udtTags)

udt = {}  
for x in range(len(udtTags)):  
#strip udtPath from fullPath and add key and value to dictionary  
udt[udtTags[x].replace(udtPath, “”)] = tagValues[x]

print udt[‘Level’].value  
print udt[‘Level’].quality  
print udt[‘Level’].timestamp  
#or  
print udt[‘Level’][/code]
