Global Scripts for Tag Usage?

Hello,

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…

Any ideas?

-Lenny

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.

Shared scripts are visible to tag events. Even better is to move your tag events to a dedicated “background” project’s tag change events.

Can they be protected from other ppl screwing them up?

1 Like

That’s the type of question that can keep you up at night. :scream:

If you right-click a project resource you can ‘protect’ it, but anyone with the privileges can come in after you and unprotect it.

Do the shared scripts show up in every project? Or only the projects I create them from? I’m assuming they’ll show up in all of them.

Yes. As the names imply: shared scripts are shared across all projects. Project scripts are local to that project.

Yeah, just confirming, thanks.

You’d think you could at least modify the roles allowed to edit them.

Just one of the reasons why tag change events are superior to tag events.

I’m using the Tag Events -> Value Changed. This is an OPC tag… Is there somewhere better?

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.

1 Like

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…

1 Like

Yes, yes, yes, and yes. And yes, and yes.

And if the processing is sufficiently similar among the ten tags, you only need one event that lists all ten to monitor.

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

Thanks for everything!

-Lenny

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.

Thanks again…

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.

1 Like