Question: Is there a way in AE scripting to test whether a script/object variable has already been set?
Short answer: No. However, it is possible to read the contents of a variable whether or not it has been set. To do this, first simply set the variable to its own current value. E.g.: :SET &VAR1# = &VAR1#
(Thanks to FrankMuffke for this solution.)
Long answer: See below.
Original post:The UC4 scripting language does not appear to provide a direct method for determining whether a variable is set or not. In one job or workflow, a variable may be set, but in another, that variable may not be set. By finding a way to check whether a variable is set or not, we can avoid syntax/runtime errors, and make modular and reusable units of UC4 scripting that may be incorporated as include files (JOBI objects) in a broader range of objects.
I found a way to work around this limitation; some of you may have noticed that I employed this work-around in my SET_FILE_MODE solution.
:SET &RUNID# = SYS_ACT_ME_NR()
:SET &MY_VAR_NAME# = "MY_VAR#"
:SET &MY_VAR_TMP# = GET_PUBLISHED_VALUE(&RUNID#,&MY_VAR_NAME#)
:SET &VAR_LENGTH# = STR_LENGTH(&MY_VAR_TMP#)
:SET &VAR_LENGTH# = FORMAT(&VAR_LENGTH#)
:PRINT "Contents of &&MY_VAR_TMP# : '&MY_VAR_TMP#'"
:PRINT "Length of &&MY_VAR_TMP# : &VAR_LENGTH#"
:IF &VAR_LENGTH# = 1
: IF &MY_VAR_TMP# = ''
: PRINT "Variable &&MY_VAR# is not set"
: ENDIF
:ELSE
: PRINT "Variable &&MY_VAR# is set"
:ENDIF
In the above example, &MY_VAR# is the name of the variable being tested. (The GET_PUBLISHED_VALUE statement expects the name of this variable without the leading ampersand.) I stored the name of the variable in another variable just to demonstrate that indirect referencing is feasible. This means that this solution is more generalizable. You could conceivably even put it inside a :WHILE loop, and generate variable names dynamically or pull them from an outside source. The main problem with this approach is that you must avoid all direct references to the variables in the rest of the script, and instead refer to the variables by their stand-in _TMP equivalents. This means that it is necessary to set one _TMP variable for each variable you want to test.
Has anyone found a better solution to this?