MAXScript FAQ > MAXScript Values > Why does Execute return 'undefined' when accessing a loop variable? |
The following code tries to use Execute() function to convert the variable var containing the string "x" to the loop variable 'x' and print its value. It returns 'undefined' 10 times instead of printing the numbers from 1 to 10.
The answer to this question is based on the fact that Execute always uses the global scope. On the other hand, a for loop always creates a new scope and the variable 'x' is local to this scope. This is the key to understanding the original problem and its solution - the Execute just does not "see" the local variable 'x' in the global scope.
Here is a possible solution which correctly prints the numbers from 1 to 10.