Scripting a property change

Hello, I just started working with ignition probably one week ago so I’m extremely new to the software. In fact, I have never programmed anything before in my entire life.

That being said, my apologies if the question I’m about to ask is silly.

I have two custom properties defined on a table and I wanted to write a property change script that runs when both properties change at the same time. The table has a column that is being treated as a Boolean and allows a user to edit the underlying value by checking the box. One custom property calculates the sum of the Boolean column, this should increase or decrease depending on if something is selected/deselected. The other custom property is looking up a value in another column of the table and changes sometimes (I’ll leave it at that, without going into too much detail on how it changes). Both custom properties are integers.

I want to create an script that runs the instant both those properties change.

I right clicked the table and selected “Scripting…” On the left I went to Event handlers and selected “propertyChange”

I know if I write something like:

if event.propertyName == "custom1": <rest of code here>

The script will run when the first custom property is change and everything works fine. However, will this work too:?

if event.propertyName == "custom1" and event.propertyName == "custom2": <rest of code here>

I tried the second code out but it didn’t seem to work… unless the syntax above is wrong (it could be, I’m super new to this)

Also, if I am able to have the script run when both properties change then how would I reference the oldValue and newValue of the respective properties?

If this isn’t possible can someone kindly suggest an alternative?

Thanks!

put

print event.propertyName

and see what prints out, I’m not sure if custom properties are included in the change script.

you could create an expression tag that is a bit encode of the two tags and then use a tag change script or something similiar

1 Like

The event script will indeed fire for custom properties.

Your code

if event.propertyName == "custom1" and event.propertyName == "custom2": <rest of code here>

will not work as the statement will never be true, because propertyName can only be one thing at once.

If both properties do change at the same time then the script will be called twice, one for each property.

So you’ll need to be a bit creative to achieve what you want. Are the two things that would change linked in any way? You could (as drewdin mentioned above) have a third property that is a combination of the first two, but bear in mind that that will then change whenever either one of the first two changes. And therefore if they both change then it will change twice.

You could store the second value somewhere else, then when the first one changes compare the “current” second value with the “stored” second value and see if they also differ - but I suspect this is likely to run into issues around timing. Hence my question above about whether the two are related etc.

If you are looking for both values to change, then as has already been alluded to, you need to get creative.

Here’s one example:

This is a window with a single label component in it. I added five custom properties to it:

  • custom1 (string)
  • custom2 (string)
  • oldCustom1 (string)
  • oldCustom2 (string)
  • trigger (string)

Also there is one called now, which holds the current time. It’s not necessary to the operation, it just gets used later.

‘trigger’ is set to an expression:

({Root Container.Label.custom1} != {Root Container.Label.oldCustom1}) && ({Root Container.Label.custom2} != {Root Container.Label.oldCustom2})

All this does is check to see if botch custom vales are different than the ‘old’ values.

In the property change script:

if event.propertyName == 'trigger': # check if 'trigger' has changed if event.newValue == 1: # check if trigger equals 1 now=event.source.now # get time event.source.text='<html><center>Last change was at<br>' + now #display change time in label text event.source.oldCustom1 = event.source.custom1 # set oldCustom values event.source.oldCustom2 = event.source.custom2

Here’s the window to illustrate. Hope this helps!
Monitor Multiple Prop Changes_2015-01-16_0817.proj (6.49 KB)