# Global Scripts for Tag Usage?

**URL:** <https://forum.inductiveautomation.com/t/global-scripts-for-tag-usage/17115>\
**Category:** Ignition\
**Created:** [February 23, 2018, 6:27pm UTC](https://forum.inductiveautomation.com/t/global-scripts-for-tag-usage/17115 "2018-02-23T18:27:34Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![lshirley](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lshirley/32/6627_2.png) [@lshirley](https://forum.inductiveautomation.com/u/lshirley)\
**Post date:** [February 23, 2018, 6:27pm UTC](https://forum.inductiveautomation.com/t/global-scripts-for-tag-usage/17115/1 "2018-02-23T18:27:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bfuson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bfuson/32/4597_2.png) [@bfuson](https://forum.inductiveautomation.com/u/bfuson)\
**Post date:** [February 23, 2018, 6:33pm UTC](https://forum.inductiveautomation.com/t/global-scripts-for-tag-usage/17115/2 "2018-02-23T18:33:05Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [February 23, 2018, 6:33pm UTC](https://forum.inductiveautomation.com/t/global-scripts-for-tag-usage/17115/3 "2018-02-23T18:33:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![lshirley](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lshirley/32/6627_2.png) [@lshirley](https://forum.inductiveautomation.com/u/lshirley)\
**Post date:** [February 23, 2018, 6:41pm UTC](https://forum.inductiveautomation.com/t/global-scripts-for-tag-usage/17115/4 "2018-02-23T18:41:25Z")

</div>

Can they be protected from other ppl screwing them up?

---

<div class="post-metadata">

**Author:** ![bfuson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bfuson/32/4597_2.png) [@bfuson](https://forum.inductiveautomation.com/u/bfuson)\
**Post date:** [February 23, 2018, 6:43pm UTC](https://forum.inductiveautomation.com/t/global-scripts-for-tag-usage/17115/5 "2018-02-23T18:43:49Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![lshirley](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lshirley/32/6627_2.png) [@lshirley](https://forum.inductiveautomation.com/u/lshirley)\
**Post date:** [February 23, 2018, 6:45pm UTC](https://forum.inductiveautomation.com/t/global-scripts-for-tag-usage/17115/6 "2018-02-23T18:45:43Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![bfuson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bfuson/32/4597_2.png) [@bfuson](https://forum.inductiveautomation.com/u/bfuson)\
**Post date:** [February 23, 2018, 6:46pm UTC](https://forum.inductiveautomation.com/t/global-scripts-for-tag-usage/17115/7 "2018-02-23T18:46:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![lshirley](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lshirley/32/6627_2.png) [@lshirley](https://forum.inductiveautomation.com/u/lshirley)\
**Post date:** [February 23, 2018, 6:46pm UTC](https://forum.inductiveautomation.com/t/global-scripts-for-tag-usage/17115/8 "2018-02-23T18:46:33Z")

</div>

Yeah, just confirming, thanks.

---

<div class="post-metadata">

**Author:** ![lshirley](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lshirley/32/6627_2.png) [@lshirley](https://forum.inductiveautomation.com/u/lshirley)\
**Post date:** [February 23, 2018, 6:47pm UTC](https://forum.inductiveautomation.com/t/global-scripts-for-tag-usage/17115/9 "2018-02-23T18:47:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [February 23, 2018, 6:47pm UTC](https://forum.inductiveautomation.com/t/global-scripts-for-tag-usage/17115/10 "2018-02-23T18:47:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![lshirley](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lshirley/32/6627_2.png) [@lshirley](https://forum.inductiveautomation.com/u/lshirley)\
**Post date:** [February 23, 2018, 6:48pm UTC](https://forum.inductiveautomation.com/t/global-scripts-for-tag-usage/17115/11 "2018-02-23T18:48:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [February 23, 2018, 6:54pm UTC](https://forum.inductiveautomation.com/t/global-scripts-for-tag-usage/17115/12 "2018-02-23T18:54:38Z")

</div>

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](http://forum.inductiveautomation.com/t/some-project-resources-not-publishing/16888/3?u=pturmel).

---

<div class="post-metadata">

**Author:** ![lshirley](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lshirley/32/6627_2.png) [@lshirley](https://forum.inductiveautomation.com/u/lshirley)\
**Post date:** [February 23, 2018, 7:27pm UTC](https://forum.inductiveautomation.com/t/global-scripts-for-tag-usage/17115/13 "2018-02-23T19:27:02Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [February 23, 2018, 7:44pm UTC](https://forum.inductiveautomation.com/t/global-scripts-for-tag-usage/17115/14 "2018-02-23T19:44:35Z")

</div>

> [@lshirley](#):
>
> Cause I could then do role permissions on that project as well...

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

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [February 23, 2018, 7:45pm UTC](https://forum.inductiveautomation.com/t/global-scripts-for-tag-usage/17115/15 "2018-02-23T19:45:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![lshirley](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lshirley/32/6627_2.png) [@lshirley](https://forum.inductiveautomation.com/u/lshirley)\
**Post date:** [February 23, 2018, 7:46pm UTC](https://forum.inductiveautomation.com/t/global-scripts-for-tag-usage/17115/16 "2018-02-23T19:46:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![lshirley](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lshirley/32/6627_2.png) [@lshirley](https://forum.inductiveautomation.com/u/lshirley)\
**Post date:** [February 23, 2018, 7:47pm UTC](https://forum.inductiveautomation.com/t/global-scripts-for-tag-usage/17115/17 "2018-02-23T19:47:56Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [February 23, 2018, 7:51pm UTC](https://forum.inductiveautomation.com/t/global-scripts-for-tag-usage/17115/18 "2018-02-23T19:51:05Z")

</div>

> [@lshirley](#):
>
> I just need to work on a case/switch/if statement that can change that one variable based on the firing tag

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.
