2 State Toggle Event Script

When using a 2 state toggle what is the best way to set up the a script to write the value to a tag? I found two ways of doing this: one through the Event configuration using onActionPreformed script and the other using a change script on props.selected.

Events onActionPreformed script:
 def runAction(self, event):
	if self.props.selected == 1:
		system.tag.write("Test/TestOos",1)
	else:
		system.tag.write("Test/TestOos",0) 
props.selected change script:
def valueChanged(self, previousValue, currentValue, origin, missedEvents):
	system.tag.write("Test/TestOos",currentValue) 

Is one way better than the other?

The first could be re-written as,

def runAction(self, event):
	system.tag.write("Test/TestOos", self.props.selected)

I don't know which of your two solutions would be better but a much better way would be to create a tag binding on the toggle. Point it directly to "Test/TestOos" and make the binding bidirectional. No script needed so it's faster and more efficient.

2 Likes

The two scripts are effectively equivalent (I'll point out that you can rewrite the action as just system.tag.write("Test/TestOos", self.props.selected)), but the better question is - why use a script at all? A bidirectional binding on selected will mean that your toggle always reflects the value of the tag, and writes back to the tag whenever the button is pressed.

2 Likes

Thank you guys! this is exactly what is was looking for!! The whole time I was thinking..."there has got to be a more efficient/effective route".