[Closed] Local declarations not working in script controller
Hey,
I’m having a strange problem. In the maxscript reference, it says that if you declare a local variable in a scripted controller, the variable will remain in the scripted controller after each evaluation of the expression.
But, whenever I try using local variables in scripted controllers…it resets them to “undefined” after each evaluation of the controller’s expression. This is really frustrating, since using global variables in this case is not an option.
A practical example of the problem I’m having is this:
Make a sphere. Set it’s positional controller to “Position List” and set its available controller to “Position Script”
Enter this code into the newly created script controller:
if var1 == undefined then
(
local var1 = 0
)
var1 += 1
print var1
[0,0,0]
Now, if you open up the listener and move the sphere around, you’ll notice that the only thing printed in the listener is a series of “1”s, whereas you’d think that it would print out an increasingly larger number (“1,2,3,4,5,6,7,8…”), because the variable is only supposed to be reset to 0 if it doesn’t exist…and due to our local declaration, it should exist…so what’s the deal??? What am I doing wrong?
Unfortunately (according to the script controller help at any rate), local variables are only available for one evaluation of the script. If you want to get it to work you’re going to have to use a global declaration.
I guess local variables in the script controllers are set up this way for specific per-evaluation block expressions. They explicitly point out you can use global expressions to share amongst scripts, but say the local variables are temporary for that evaluation.
A bit of a bummer, but understandable I guess.
Oh really? That sucks…I guess I misinterpreted the help files…
Here’s a quote from the maxscript docs I’m using:
Script controller local variables are heap-based locals, which are slightly different from normal (stack-based) locals. Normal locals are visible in the current scope and have a lifetime of one execution of that scope. Heap-based locals are also visible only in the current scope, but have a lifetime equal to the lifetime of the top-level expression where they are defined. A script controllers locals are created the first time the script is executed and are kept permanently in the heap (unless and until you redefine the script). This means you can store values in local variables during one execution of the script controller and these values will still be present at the next evaluation.
Maybe Bobo could clarify?
Okay, you’re not the only one confused then! Here’s the quote from the Max help (ie. the non-maxscript help):
Because the text is inside a block expression, you can declare local variables that are visible only within the script and are temporary for one evaluation. You can also declare or access global variables that are shared with all other scripts in MAXScript and hold their values from one evaluation to the next.
Inconsistency! Argh!
Nooo!!! haha…foiled again
Thanks for your help erilaz ;)…I guess we’ll just have to see what Boris says!
Is the script time based? If so, you could always use the F constant (frames) set up in the script controller. That would increment the same way, just not based on your manual movement of the object.
Well…I need the script to be able to increment values without the time changing…so no
As an aside, MAKE’s new demo reel is awesome! Well done man.
Love the music btw.
Thanks bro! There’s a couple of new things on my site that aren’t on the reel as well
Hi,
I think you need to declare the variable first :
local vart1
if var1 == undefined then
(
var1 = 0
)
var1 += 1
print var1
[0,0,0]