Notifications
Clear all

[Closed] Position Controller Problems

Hei guys, I’m sorry for posing so many questions about Max Script, but it’s really important this time (as usual :rolleyes: ).

Is it possible to to assign an outer variable from the Max-Script code to a position controller?

For example:

d = Point();
d.name = “p”;

i = 100;
d.position.controller = position_script() –max-script-code

scrpt = [i]”print(i + 1) – the position script

[0,0,0]”;[/i]

d.position.controller.script = scrpt –max-script-code again

Normally, I would expect that script printing “101” but it doesn’t, instead of that it says that the inner variable (within the position script) “i” is undefined. I don’t understand how I could declare “i” as a local positionscript variable with the value of the outer variable “i”.

Does anybody got an idea?

Cheers “ko’gen”.

4 Replies

It is printing 101 on my machine as expected.
Did you run the SAME code you posted?
If your example is a snippet from another, larger script, make sure “i” is declared explicitly as GLOBAL to make it visible throughout the scene. You might have to make it even PERSISTENT GLOBAL to be saved with the Max file, otherwise your controllers would fail on loading as “i” would be gone after restarting Max.

It did print “101”?:eek:

I posted just a “light” version of my actual problem, but on my machine, it was always saying “i” is undefined. That’s strange. Thanks for the tip with the “persistent global” variable. But I guess, there is another tiny problem: If the value is global, that means there can only be one value in “i” for an other point… I mean:

e = Point()
e.name = “pp”;

i = 20; --new global value i
d.position.controller = position_script() --max-script-code

scrpt = [i]"print(i + 1) -- the position script

[0,0,0]";[/i]

d.position.controller.script = scrpt –max-script-code again

It will set the variable “i” to 20 and it will print “21” on your marvellous machine Bobo

BUT the variable “i” in the Position script of the former point “d”, will be 21 as well, I guess? That’s not good, it would be fine if it stays “101” in d and “21” in e.

Your answer about printing 101 confused me, I have to test it immediately.

Cheers “ko’gen”.

You could localize i the following way.
If you use this code block again by changing the value of i to say 20,
listener will print out 2 values for you, 101 from the first point and
21 from the second. This is because both the i’s exist in local space of their own
script cntrollers

 
 
pnt = Point() 
pnt.name = "pt1";
 
pnt.position.controller = position_script() --------- max-script-code
pnt.pos.controller.IScriptCtrl.AddConstant "i" 100 -- i will be in local scope (only for this particular script controller)
 
scrpt = "print(i + 1) -- the position script
[0,0,0]";
pnt.position.controller.script = scrpt -------------- max-script-code again
 

Thank you very much to both of you.

At first I tried the thing with the little script I’ve posted, again and as Bobo said, it worked fine (Dunno why it didn’t work this morning.)
After that I tried out the trick shibu told me and now everything is fine. I guess I’m one step closer to the end, in a positive way

Cheers “ko’gen”.