# Exiting a Gateway Tag Change script prematurely?

**URL:** <https://forum.inductiveautomation.com/t/exiting-a-gateway-tag-change-script-prematurely/74330>\
**Category:** Ignition\
**Created:** [May 31, 2023, 5:19am UTC](https://forum.inductiveautomation.com/t/exiting-a-gateway-tag-change-script-prematurely/74330 "2023-05-31T05:19:23Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [May 31, 2023, 5:19am UTC](https://forum.inductiveautomation.com/t/exiting-a-gateway-tag-change-script-prematurely/74330/1 "2023-05-31T05:19:23Z")

</div>

I'm sure I should know the answer to this, but how would I exit a Gateway Tag Change script early? You can't use `return` and I know it's not great to use `sys.exit()`. So what other options are there, apart from a potentially very long `if/else` block and moving to a project library?  
e.g.

```auto
...
if exit_condition:
   LOGGER.info('log something')
else:
   ....
   potentially_lots_of_lines_of_more_code...._with_ugly_useless_indenting

```

I'd prefer to do something like:

```auto
...
if exit_condition:
   LOGGER...
   exit

...
lots_of_lines...

```

Edit: perhaps I should move this all into a project library... I didn't really want to since the scripts are VERY specific to the tag change events 😕

---

<div class="post-metadata">

**Author:** ![victordcq](https://avatars.discourse-cdn.com/v4/letter/v/f6c823/32.png) [@victordcq](https://forum.inductiveautomation.com/u/victordcq)\
**Post date:** [May 31, 2023, 9:14am UTC](https://forum.inductiveautomation.com/t/exiting-a-gateway-tag-change-script-prematurely/74330/2 "2023-05-31T09:14:21Z")

</div>

whats wrong with sys.exit?

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [May 31, 2023, 9:29am UTC](https://forum.inductiveautomation.com/t/exiting-a-gateway-tag-change-script-prematurely/74330/3 "2023-05-31T09:29:50Z")

</div>

Never even thought of using it in python, but I can tell you why I have mixed feelings about `exit` in C:  
It breaks the normal flow of your program.  
Imagine you're looking at the `main` function, see it's doing things before its final return... but it never happens and you don't know why. So you have to go and dig through the source, following the flow of the program, until you find where it took a path you didn't expect and exited early.  
I've used it, because sometimes the alternative is messy and painful, but always made a point in those cases to make it as clear and explicit as possible, usually through a function placed near the `main` that handled the exit in a single place.

Now I know python is not C, but I'd expect similar reasons to apply.

---

<div class="post-metadata">

**Author:** ![zxcslo](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zxcslo/32/61610_2.png) [@zxcslo](https://forum.inductiveautomation.com/u/zxcslo)\
**Post date:** [May 31, 2023, 9:37am UTC](https://forum.inductiveautomation.com/t/exiting-a-gateway-tag-change-script-prematurely/74330/4 "2023-05-31T09:37:54Z")

</div>

Make a def() in Tag change script, use return where needed and at the end (bottom of the Tag change script) call that def()...

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [May 31, 2023, 11:19am UTC](https://forum.inductiveautomation.com/t/exiting-a-gateway-tag-change-script-prematurely/74330/5 "2023-05-31T11:19:00Z")

</div>

Haha, that's genius, that would work. I did end up moving everything into a script library though, and I don't regret it

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [May 31, 2023, 11:30am UTC](https://forum.inductiveautomation.com/t/exiting-a-gateway-tag-change-script-prematurely/74330/6 "2023-05-31T11:30:41Z")

</div>

Even if it's specific to certain tags I always find that it's better off in a script library anyways. It makes it much easier to look at your script and how it relates to other gateway scripts that you would also have in your script library.

I especially make sure to do it for message handling - I can see how I send the message, how the message handler gets it, and what it does all in one script makes trouble shooting much easier.

---

<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:** [May 31, 2023, 12:06pm UTC](https://forum.inductiveautomation.com/t/exiting-a-gateway-tag-change-script-prematurely/74330/7 "2023-05-31T12:06:59Z")

</div>

> [@zxcslo](#):
>
> Make a def() in Tag change script

Don't do it. This invokes a whole bunch of import grief due to backwards-compatible legacy scoping.

Gateway events scripts should always be one-liners (approximately) delegating everything to a project library script. Never--yes, _ **Never** _--use any `def` in a gateway event.

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [May 31, 2023, 12:20pm UTC](https://forum.inductiveautomation.com/t/exiting-a-gateway-tag-change-script-prematurely/74330/8 "2023-05-31T12:20:26Z")

</div>

This is something to store away in my tips and tricks 🙂

---

<div class="post-metadata">

**Author:** ![zxcslo](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zxcslo/32/61610_2.png) [@zxcslo](https://forum.inductiveautomation.com/u/zxcslo)\
**Post date:** [June 1, 2023, 4:54am UTC](https://forum.inductiveautomation.com/t/exiting-a-gateway-tag-change-script-prematurely/74330/9 "2023-06-01T04:54:36Z")

</div>

> [@pturmel](#):
>
> This invokes a whole bunch of import grief due to backwards-compatible legacy scoping

Care to elaborate a little more?

---

<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:** [June 1, 2023, 12:06pm UTC](https://forum.inductiveautomation.com/t/exiting-a-gateway-tag-change-script-prematurely/74330/10 "2023-06-01T12:06:18Z")

</div>

Look for the numerous posts here where people have to include `import system` inside their functions because of this particular phenomenon. Or any other top-level variable that is normally auto-included and available throughout out all class and function definitions. Or encountering cross-event contamination of variable assignments.

---

<div class="post-metadata">

**Author:** ![drewdin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/drewdin/32/3363_2.png) [@drewdin](https://forum.inductiveautomation.com/u/drewdin)\
**Post date:** [May 13, 2026, 12:55am UTC](https://forum.inductiveautomation.com/t/exiting-a-gateway-tag-change-script-prematurely/74330/11 "2026-05-13T00:55:11Z")

</div>

old post but i am doing this now, everything is in the gateway script and I am debating moving it over to project scripts. Glad I found you post!

---

<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:** [May 13, 2026, 11:11am UTC](https://forum.inductiveautomation.com/t/exiting-a-gateway-tag-change-script-prematurely/74330/12 "2026-05-13T11:11:28Z")

</div>

Since comment #7 above, I've expanded my "one-liner" advice to basically all event scripts and component scripts.

FWIW, Ignition v8.3 abandons the legacy support that is responsible for this particular import quirk, but my one-liner advice still stands.
