Variable global in event script doesnt work

Hello,

I have a simple script in an event script that when the button is pressed, causes the following code to write to the Label text component:

def runAction(self, event):
	tempCounter = 0
	
	def funkcja():
		global tempCounter
		tempCounter += 1
	    
	funkcja()
	self.getSibling('Label').props.text = tempCounter

I get this error:

Error running action 'dom.onClick' on Cameras/Cameras_test_2@D/root/FlexContainer_2/Button_1: Traceback (most recent call last): File "function:runAction", line 8, in runAction File "function:runAction", line 6, in funkcja NameError: global name 'tempCounter' is not defined

Script console work fine:

The global keyword in funkcja marks that name as being outside of both runAction and funkcja, but runAction itself is the scope for your tempCounter. You might be able to include a matching global statement in runAction. Seems absurdly fragile.

Why do you need this code? What are you really trying to accomplish? (Also, the interactive interpreter is never a suitable place to test code intended to be used in Perspective. The scopes are not compatible.)

3 Likes

You're probably better off using a gateway tag (if the value should be shared across all sessions) or a custom session property (if it's different for each login).

1 Like

I understand the problem I will try something else thank you for the tip