Is there like a global script location that tags CAN access? I have like 10 tags that all do the same thing, just from different TCP drivers. It’s several pages of code in each tag that’s duplicate (using variables to let the code know the differences), and would really love to be able to have the code in only one section, but executable from any tag. Every change I make to the code in cumbersome on having to update every tag. Just venting…
Sure, you can define functions in a shared script library and access it in your tag event scripts using shared.myScriptFile.myFunction(currentValue.value) or something similar. I do this for data munging of raw sensor data, as well as writing change logs to the DB. When you have multiple tags/udts that all need similar functionality, you can simply update the one script to change them all.
Project => Scripts => Gateway Event Scripts => Tag Change.
I recommend that such critical code be in a project that has no windows, or is otherwise hidden from the home page. So updates to it don’t disturb any users.
You might find this post helpful.
So, you’re saying create a new project, use the project scrips library for all of the code, and create “Tag Change” events in that project for the 10 or so tags that need to run said code, and leave it all in there? That way nothing is global? If I have that right, that’s interesting indeed. Cause I could then do role permissions on that project as well…
I already had a GUI project for use of viewing those tags data, and tables, etc. I created a project library in there, and copied all of the shared code to it. Then created gateway tag change events for the tags in question, and moved the code from the tag change events to there. Everything is working like a charm! I feel so much better about the code staying safe from the multiple other admins/developers (whom have once already deleted some of my stuff).
Yeah, for now, I added them all individually. All that changes between them is one hard coded variable that’s passed to the code. I know there’s a way to get the actual tag that changed, I just need to work on a case/switch/if statement that can change that one variable based on the firing tag, and go from there.
The simplest way is to place a dictionary at the top level of your project script module that has tag paths as keys, and the desired variable content as the values. Then at the top of the event script look up the tag path in that dictionary.