Notifications
Clear all

[Closed] spinner values and functions

I want to send 2 spinner values to a function to be compared to each other and if ValueB is bigger than ValueA, I want to make ValueB equal to ValueA. This is cause I have 2 spinners and the first one has to be equal to or smaller than the second one and vice-versa. I have code working but not using functions. I’d like to know how to do this kind of thing by sending the values to a function and have it figured out that way and sent back to the relevant spinner. Mostly for learning purposes. I want to execute the function every time one of the spinners is changed.

I’ve attached the script as it’s workin at the moment without using a function to test and change spinner values. The main things I want to change are the 6 chunks that start with.

on spn_### changed do…

I’ve tried writing the function

fn checkValLow lowVal highVal =
( if lowVal > highVal do
( highVal = lowVal
)
)

And calling the function like this:

on spn_redlow changed redlowval do
( checkValLow spn_redlow.value spn_redhigh.value
)

But it doesn’t seem to pass the values around properly as nothing actually happens to the spinner that I think is supposed to change.

I need to know how to send the spinner values to the function and have it send back relevant values to the spinners if needed.

I know I’ll only really save a few lines of code in this script by using a function for this, but I really want to know how to do this kind of thing a bit more elegantly.

Thanks,

Cg.

11 Replies

You just have to set the .value of the spinner at the end:

fn checkValLow lowVal highVal =
(
if lowVal > highVal do highVal = lowVal
return highVal
)

and

hiVal = checkValLow spn_redlow.value spn_redhigh.value
spn_redhigh.value = hiVal

However the function is so small, you could probably not even bother with it and do it in the event:

on spn_redlow changed val do
(
	if  spn_redlow.value >  spn_redhigh.value do spn_redlow.value = spn_redhigh.value
)

Yeah I realise that the function is pretty much superfluous, it’s more about just learning how to do it.

Thanks for taking time to reply. Much appreciated.

Someone else helped me out too, as usual there’s alway more than one way to skin a cat. The other suggestion was to just add a & in front of the names in the function, that way it references the actual spinner values rather than just copying the values to the variable in the function…

1 Reply
(@erilaz)
Joined: 11 months ago

Posts: 0

Can you elaborate on that method Chris? I’m intrigued!

It’s ByReference parameter passing (as opposed to by value). A simple example:


  fn square &n = (n = n * n)
 
 n = 3
     --3
 
 square &n
     n
     --9

As you can see, a value isn’t directly assigned to n in the usual way: “n = square n”, or “assign the result of ‘square n’ to the variable n”.

The “&” symbol basically tells max to operate on the variable directly.

I’m no expert on these matters, so I never thought to use it with such things as spinner values though. Nice touch!

Ah sweet. Nice use. It’s hard to find a reference to in the documentation.

See Creating Functions -> By Reference Parameter Passing in the documentation.

By-reference parameter passing is very handy when you want to modify a value passed as argument in a function as mentionned by Dave.

1 Reply
(@erilaz)
Joined: 11 months ago

Posts: 0

Thanks Yannick.

Yeah, what they ^^^ said. Hehe. Sorry I only just noticed you’d replied. ypuech and davestewart answered better than I could have. But here’s the resulting section of my script anyway to show how I got it working.

    fn checkLow &lowVal &highVal =
    (    if lowVal > highVal do
        (    highval = lowval
        )
    )    
        
        fn checkHigh &lowVal &highVal =
    (    if lowVal > highVal do
        (    lowval = highval
        )
    )
        
    on spn_redlow changed redlowval do
    (    checkLow &spn_redlow.value &spn_redhigh.value
    )
        
    on spn_redhigh changed redhighval do
    (    checkHigh &spn_redlow.value &spn_redhigh.value
    )
         on spn_redlow changed redlowval do
         (    checkLow &spn_redlow.value &spn_redhigh.value
         )

You are never even using the value passed by the spinner which in this example you are declaring as redlowval.

by putting redlowval after “changed” you are passing the value of the spinner that the user changes it to to that variable.


rollout my_spinners "My spinners" (
	spinner spn_redLow "Red Low:  "
	spinner spn_redHigh "Red High: "

	on spn_redLow changed val do (
		if val > spn_redHigh.value then
			spn_redHigh.value = val
	)
	
	on spn_redHigh changed val do (
		if val < spn_redLow.value then
			spn_redLow.value = val
	)
)
createDialog my_spinners 150 50

Try that code. You’ll notice in my example that val holds the value of the spinner so instead of writing some arbitrary function that you can only use for these spinners, this would be a better approach.

If you really want to figure out functions then you could write a function that could work for each spinner and use it like this


fn ifSmallerSetEqualTo low high thespinner = (
	if low > high then (
		if (isProperty thespinner #value) then
			thespinner.value = low
	)
	if high > low then (
		if (isProperty thespinner #value) then
			thespinner.value = high
	)
)

rollout my_spinners "My spinners" (
	spinner spn_redLow "Red Low:  "
	spinner spn_redHigh "Red High: "

	on spn_redLow changed val do (
		ifSmallerSetEqualTo val spn_redHigh.value spn_redHigh
	)
	
	on spn_redHigh changed val do (
		ifSmallerSetEqualTo spn_redLow.value val spn_redHigh
	)
)
createDialog my_spinners 150 50

Hope this helps,
Colin

Hi Colin, thanks for your time. I apprecate everyone sharing their experience with me. The first script you write there is pretty much the same as what I had in the first script that I had zipped up and attached to the first post. The second one that you listed doesn’t really work the same way though… it lets you turn the low spinner up and the high will adjust accordingly, but when you try to turn the high spinner down, it won’t go any lower than what the low spinner is set to.

As far as the variable that does nothing, I tried it without and I got errors… I do realise that I’m not using that value though. I just can’t figure out how to just leave it off…

Cheers to everyone for all your help, I’ve gained so much just from this little exercise.

Cg.

you can’t omit the val on “on spner changed val do” because it will always pass the value of the spinner to you so you don’t have to reference this.value. (note: the this is not valid in maxscript). The second code I wrote is if you were determined to write a function for use with the code, the first is a much better option. Best of luck! keep scripting!

-Colin