I know I'm approaching this incorrectly, but I'm not quite sure how to go about this in Ignition and I'm struggling to come up with the right search criteria to find it, somehow...
What I'd like to do is create a class object on one button press and then access that stored object in another button press.
I currently have a test project script (UniversalScripts.TestScripts) setup as follows:
test = UniversalScripts.TestScripts.TestClass()
test.setReply("This is a reply")
print test.getReply()
Which obviously prints the stated reply to the console. In button 2 I have just:
print test.getReply()
which obviously returns an error because it's not been initialized under button 2, only inside of button 1.
Where would I store the "test" variable from button 1 so that I can access the same instance of it from within button 2? If I try to store it in a script, I still get a different instance of the variable in button 2 vs button 1 (which I expect). Doing this in Java, I could just store the variable in the parent class that creates the buttons and access it that way, but I don't know what would be considered the parent class, in this case (the parent container?) nor how to store/access a variable there from within a button.
I feel like this should be obvious to me, but it's been a long day (week really) and my brain feels like mush, so... Help!
You can create collection objects as top-level variables in a script module, and any keys you assign to them will be available throughout the JVM for that project. True in all scopes (but separate by JVM, and separate by project in the gateway).
The global keyword has no meaning at the top level. It is intended to be used in functions and methods to make assignment to a top level variable possible from within those functions or methods. You don't need it in this case.
Instead, assign to tClass to initialize it at the top level of the script (outside any function). Then you do not need the getTestClass() method, as it is equivalent to simply referencing path.to.package.tClass.
Using a class object instead of a native collection will complicate your life greatly if you need cross-project sharing in gateway scope.
Ahh, I think I was trying to assign to tClass from within a function without using global is why it wasn't working for me the way I initially tried it (getters/setters habit from Java and not knowing about global definition). I probably also forgot that you have to assign the new class to the variable after the class definition or it doesn't recognize that the class exists. IIRC, you could assign a class to a variable on a line before the class is even defined and it will still work, in JavaScript (been a while), whereas Python seems more strict in that regard, requiring that the class be defined before it can be called for assignment.
For this use-case, I just need it within the client scope, but this is good information for the future, as I'll likely try something along those lines as I get more into Ignition.