"Global" Instance of a Class

Hi all,

In Ignition, is there any way to create a “global” instance of a class in Ignition when a client/project is started up?

Suppose we have (GUI) Button A and Button B and both have scripts for “actionPerformed”. In both scripts, suppose there is an instance inst_foo of a class foo used. Is there a way to just create the instance inst_foo of foo once at the beginning (start-up) of the project/client and then we just use inst_foo instance throughout the scripts in Ignition project without a new to re-create it for every script?

For instance, in Python script we can declare a class and initialize one instance of it:

class foo:
def init(self):
self.val = 0
self.list = []

inst_foo = foo()

then the inst_foo (instance of class foo) can be used by whatever code is written below.

Likewise, can we declare instance of a class once in the initial script which can be called across all other Python scripts (say, in the component, tag, etc)? Using the examples, can we use inst_foo in “actionPerformed” scripts of Button A and Button B immediately (such as calling inst_foo.val = some_val without a need to declare inst_foo = foo() in the “actionPerformed” scripts)?

Many thanks!

Yes, define and instantiate the class in a project script module. Optionally define a function in that module that will retrieve that instance for you, so you won’t accidentally instantiate another copy.

Note that such a “global” instance will exist independently in each client and in the gateway. It isn’t a shared object across the entire system.

Hi pturmel,

I created a script library called shared.Common.Debug (Common is the package and Debug is the script file name) and in the file I created a classed named Debug.

Then, in the Gateway event script I created an instance called dummyDebug = shared.Common.Debug.Debug(). Also, I created a button component with a script in “actionPerformed” event to print dummyDebug

When I run the project and press the button, I got an error message because ‘dummyDebug’ is not defined.

When I repeated the process in the Client event script instead of Gateway event script but the result is the same. :frowning: any idea?

Hi ian,

I have several ideas.

  1. It looks like you stored a dummyDebug instance in a Gateway event script. That is the wrong place to store an instance if you want to use it in other places. You need to store the instance in the shared.Common.Debug Python module. Once you do that you can share it in other places. There are multiple ways to do it but here is a clear way. In your Gateway event script: #Storing data in a Python dictionary that is created in the shared.Common.Debug module: shared.Common.Debug.MyDictionaryVariable["dummyDebug"] = shared.Common.Debug.Debug() 2. Python data/instances created in the Gateway scope cannot be accessed in the Client scope unless serverFunctions are used.

  2. Phil Turmel is absolutely right that global Python data can be stored in project (and shared) Python library modules. However I created GStore (short for global storage) to answer the exact question, “How can I store and use Python data globally in an Ignition project?” GStore is an explicit way to store global data.

Edit: Getting rid of GStore: nickmudge.info/post/getting-rid-of-gstore

Best,

Hi nmudge,

Thanks for the reply. That clarifies my understanding. I checked the link that you posted about GStore function to store global variables or instances, which can also be replicated by the current Python project module. Now, I can define the global variables or instances by declaring them in the Python project module, as you suggested. :slight_smile: