expression_controller basic help

expression_controller basic help

bhnh
Advocate Advocate
749 Views
8 Replies
Message 1 of 9

expression_controller basic help

bhnh
Advocate
Advocate
I'm going in circles on this one. Would someone demonstration how to put an expression controller together, please?

I've been trying to get a variable to change according to the relative positions of two objects. Currently playing with the idea of using a float_expression or float_script... don't know if this is even a viable approach. But it turns out that's begging the question; I can't even figure out how to properly construct an expression controller, despite poring over all the Reference stuff I've got. When I try (don't laugh)...

)
b1 = box()
2 = box pos:
local a = 1

fx = b1.position.controller = float_expression
fx.SetExpression = "if (b1.pos.y > b2.pos.y) then v = 2 else v = 1"
)

I get Unknown property: "SetExpression" in Controller:Float_Expression. Any guidance would be greatly appreciated. Thanks.
0 Likes
750 Views
8 Replies
Replies (8)
Message 2 of 9

Steve_Curley
Mentor
Mentor
3 Possible problems.

1) <boolean> setExpression <&TSTR>Expression
setExpression is a function, not a property, so the "=" is probably what is causing the error.

2) Because of that <&TSTR> (pointer/reference to a string), it may be advisable (unless it "just works" the other way) to assign the string to a variable and use that in the function call.
local a
--other stuff here
a = "if (b1.pos.y > b2.pos.y) then v = 2 else v = 1"
fx.setExpression &a

NB. Not tested - it's game time here 😛

3) I'm not sure on this one, but as I see it an expression is just that - a calculation, a formula to be applied. The help for "if - then" only mentions scripts, not expressions. So whether the expression string is even valid I'm not certain.

Actually (and this relates to your previous thread as well) I think you're getting closer to what you want, but it's a script controller that you need.
Check the help for "Using script controllers". The first couple of paragraphs imply (to me) that while expressions are scripts, they do not allow the full range of script commands to be used within them. I'm guessing that "if-then" is one of those you can't use.

Max 2016 (SP1/EXT1)
Win7Pro x64 (SP1). i5-3570K @ 4.4GHz, 8Gb Ram, DX11.
nVidia GTX760 (2GB) (Driver 430.86).

0 Likes
Message 3 of 9

bhnh
Advocate
Advocate
Thanks for the response, Steve. At this point I've overthought it so much I can't even picture how to construct the script controller.

Another approach I've been playing with is the following:
b1 = box()
b2 = box pos:

when transform b1 changes do
(
case of
(
(b1.pos.z > b2.pos.z): b2.pos.x = 200
(b1.pos.z <= b2.pos.z): b2.pos.x = 50
)
)


The catch here is "changes" rather than "changed". If you run this and move the first box up the z-axis, nothing happens; b1.pos.z is being read as the value before the change, not after. If you move box2 up the z-axis a second time, then box2 will change its x position. Sigh.
0 Likes
Message 4 of 9

Steve_Curley
Mentor
Mentor
This works, but only if you move Box01. Moving Box02 seems to cause some rather odd motion and I'm not sure why, or how to "fix" it.
The reason for the addNode lines is the for the same reason as in this thread namely that unless the variables are Nodes they won't update properly in real time in the viewports.
(
box pos: name: "Box01" wirecolor: Red
box pos: name: "Box02" wirecolor: Blue

sc = float_script()
sc.addNode "b1" $Box01
sc.addNode "b2" $Box02
$Box02.pos.Y_Position.controller = sc
sc.script = "if (b2.pos.z == b1.pos.z) then b1.pos.y else b2.pos.y"
)

Max 2016 (SP1/EXT1)
Win7Pro x64 (SP1). i5-3570K @ 4.4GHz, 8Gb Ram, DX11.
nVidia GTX760 (2GB) (Driver 430.86).

0 Likes
Message 5 of 9

bhnh
Advocate
Advocate
Steve, I'm putting your name in for a GBE. Can't tell you how much I appreciate this. I'm still new enough at MXS that the "addNode" business would never have occurred to me. Thanks!
0 Likes
Message 6 of 9

Steve_Curley
Mentor
Mentor
Question is - which GBE am I up for? http://www.acronymfinder.com/af-query.asp?Acronym=gbe&Find=find&string=exact 😄

To be quite honest the only reason I knew about that was because of the other thread. I got the script (above) working but it wasn't updating until I nudged the TimeSlider - then I remembered the other thread. I had to cross-check between the script, the Listener window, the script editor dialog and the viewports to work out what was happening and what was being set when using the other ways to add scripts - plus checking the entries in the Help ofc. The lack of examples for that kind of function does make life a bit difficult - you just have to worry at it until something "clicks".

Anyway - I'm glad it helped, and I learned some stuff along the way - still an MSX noob myself. A win-win situation 🙂

Max 2016 (SP1/EXT1)
Win7Pro x64 (SP1). i5-3570K @ 4.4GHz, 8Gb Ram, DX11.
nVidia GTX760 (2GB) (Driver 430.86).

0 Likes
Message 7 of 9

bhnh
Advocate
Advocate
Gargantuanly Beneficent Expert. :coolsmile:

A follow-up on the Box02 oddness. This seems to be glitch-free...
(
box pos: name: "Box01" wirecolor: Red
box pos: name: "Box02" wirecolor: Blue
point()

local a = 1
print a

sc = float_script()
sc.addNode "b1" $Box01
sc.addNode "b2" $Box02
$Point01.position.Y_position.controller = sc
sc.script = "if (b1.pos.z == b2.pos.z) then a = 1 else a = 2"
)


Tying the controller to Box02 makes it an "active participant ", where the elements being affected by the script need to be "referenced participants". By making a point (or anything else) the "script holder", both boxes are able to act as they should.

I admire your perseverance. I realize that the Reference can't cover everything, but it's daunting when one doesn't have a clue where to begin looking.
0 Likes
Message 8 of 9

Anonymous
Not applicable
Not that I'm trying to further confuse you Bruce, but you might consider assigning a track instead
of a node. The ideal situation would have you only testing what you need to make the script
work. Assigning the entire node means that the script controller is looking for any changes in
the nodes in question.

Having waded through script controllers in the past I understand the desire to leave well enough
alone, take your "winnings" and move on. 😄

If you don't have a heavy scene, then node assignment is okay, but if you plan to have thousands of
nodes, it might be worth figuring out speedier methods.
0 Likes
Message 9 of 9

bhnh
Advocate
Advocate
I'm not sure it's possible to confuse me more, SC. :roll: Thanks for the tip. As soon as my stomach settles on this I'll take a look at tracks.
0 Likes