Constant or Enum Value for expression

I would like to be able to declare some Constant Value or Enum value
(with a Gateway scope)
and to reference it in expression or binding or script.

You could create a variable in a script module in app and reference it (even change it! it is unique per client) in any window at the project level. I haven’t used Ignition 7.7 yet but it seems like you could do the same in the “shared” (?) script for your events/scripting/etc.

app.const

BIGNUM = 999999

Window.Button.actionPerformed

system.gui.messageBox(str(app.const.BIGNUM))
app.const.BIGNUM = 9999999
system.gui.messageBox(str(app.const.BIGNUM))

You have this ability with 7.7.

In the global (shared) script library, I made a script called constants. That script is from collections import namedtuple Constants = namedtuple('Constants', ['pi', 'e']) math= Constants(3.14, 2.718)
Then in a window, I made a label , and bound the text to this expression: runScript("shared.constants.math.pi") My label shows 3.14.

Obviously you aren’t limited to numbers, and you can make a number of namedtuples in your script – you might have shared.constants.math and shared.constants.motorNames and shared.constants.somethingElse.

You can also define variables without using the named tuples, but the values of those variables could get changed accidentally, causing much heartache. :frowning:

You can also define functions in your shared script, such as def pi(): return 3.14159 which you can call with shared.constants.pi() . This will give you an immutable value back.

Many thanks Kathy for those examples.
namedtuple to manage enumeration look like to be a very good solution.

But I wonder for a wide use if “runScript” function in HMI bindings can be resource consumer if constant value are not in cache of the client :scratch:

I just talked to Colby about this. If you don’t specify a polling rate in the run script (and my example didn’t), the expression is only evaluated when the window is opened.

good to know :smiley:
thanks a lot