Problems assigning default values to variables in a function

This is my first post, so I honestly apologize if I'm doing something wrong...

I'm new at MAXscript and I've got a little problem trying to assign default values to variables in a user function. Let's just take the example MAXscript help provides:

   function sign val:0 =
   (
   if val == 0
   then messagebox ("Equal to 0")
   else if val > 0
   then messagebox ("Greater than 0")
   else messagebox ("Less than 0")
   )

Simple enough... opening a New Script window, copying/pasting it there, evaluating it and checking for the results at the listener is says "sign()... OK"
But if I try to use this function typing, for instance

   sign(5)

the answer is:

   -- Argument count error: sign wanted 0, got 1

if I remove the syntax for the suposedly default value on the first line, like that:

   function sign val =
   (
   if val == 0
   then messagebox ("Equal to 0")
   else if val > 0
   then messagebox ("Greater than 0")
   else messagebox ("Less than 0")
   )

it now works fine if I say "sign(-5)", but inputing a value is now mandatory and if I type only the function without providing values for the parameters, it gives an error message like

   sign()
   -- Argument count error: sign wanted 1, got 0

It seems that if I do provide a "default" value to the function variable doesn't get it anymore. I suspect that my mistake is evident but it scapes me.
And, by the way, I also strongly suspect that this question is faaar too "newb" to be here, is there a more appropriate place for people who's just starting on MAXscript?
Thank you very much!

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
bugmenot's picture

I thinks it's bad written,because I've problems with this Script

function mycube side position:[0,0,0] =
(
box length:side width:side height:side pos:position
)
mycube side:12 position:[10,10,10]

On the Max2012 Reference tutorial this is the method written

And gets this result:

-- Error occurred in anonymous codeblock; filename: ; position: 123; line: 5
-- Argument count error: mycube wanted 1, got 0

You can see that there is an error because position parameter is color highlited in purple while side is not...it's not recognizing side as a parameter.

To solve you can add the parameter even with an empty value like this:

function mycube side: position:[0,0,0] =
(
box length:side width:side height:side pos:position
)
mycube side:12 position:[10,10,10]

"side:" did the trick, and in the editor you can see it turned to purple like the other parameter.
In the end you have a not supplied parameter (side) and an initialized one (position).

Anyway the reference is full of errors, that should be corrected, in the previous chapters:

localrad was one of the errors

It should be written like this:
local rad

In My Opinion Maxscript is a bad clone of Python language, some things could have been made a little less messy.

pfbelesa's picture

permutation

hi guys
im new to 3ds max
and i need a script that permutates 3 diferent variables, all 3 goes from 1 to 20.
so its a 20x20x20 combination. i could not see anything directly related to permutations on max help.

any one have ideas ?

colinsenner's picture

No problem! Have a great

No problem! Have a great day.

Nelson Baietti's picture

I see... Both commands work,

I see...

Both commands work, but the way you say makes more sense!

Yah it helps! Thanks, Colin!

colinsenner's picture

function mycube side

function mycube side position:[0, 0, 0] =
(
box length:side width:side height:side pos:position
)
mycube()
mycube 10
$Box:Box01 @ [0.000000,0.000000,0.000000]
mycube 10 position:[10,10,10]
$Box:Box02 @ [10.000000,10.000000,10.000000]

I know it's confusing syntax especially after coming from a language that needs arguments in parenthesis, but this isn't the case in max. The ONLY time you NEED parenthesis is if there are no required arguments.

if there is an argument with a colon it is not required, without a colon it is required.

Hope this helps.
-Colin

Nelson Baietti's picture

Weirdly, though... if you

Weirdly, though... if you use the next example Max provides on the help "Defining Custom Functions"

function mycube side position:[0, 0, 0] =
(
box length:side width:side height:side pos:position
)

the input syntax is again different, for it to work out you must place the value INSIDE the parentheses to get the default:

mycube(30)

And after trying a dozen different syntax permutations, I figured it out that to insert a position different from the default, you must write it like this:

mycube(20) position:[10,10,10]

I don't really get what happened... why for some variables you must put them inside the parentheses and for others you must call them by [variable_name][colon][value]? weird... I trully think that the 3DMAX help is great, but I believe that a few explained input samples in this chapter would help

[EDIT]:

One more thing: if you do not assign a default value in the function, say, like this:

function mycube side position =
(
box length:side width:side height:side pos:position
)

How confusing is that when you realize that the proper syntax becomes:

mycube 20 [10,10,10]

Nelson Baietti's picture

Yeah, that's it! Its

Yeah, that's it!

Its working now! Testing here, for the code in the first post, the proper way to input the value is indeed typing the [function] space [variable][colon][value_you_want_to_use] like so:

sign val:1

sign val:-5

and if you still want to use de defaults, the syntax is different, you open and close the parentheses just after the name of the function, wich were not part of the syntax when you are inputing a value of your own, like so:

sign()

Now I get it… Every time you learn a new language you still know the basic logic and way it works, but quite often the little syntaxes bug us a lot in the beginning…!

Thank you very much for the help and also for the warm welcome guys!!

- Nelson Baeitti

Christopher Grant's picture

Aha! I misread you previous

Aha! I misread you previous post and still thought a:5 was used when defining not when calling the function.

Thanks for the info Colin!

colinsenner's picture

square a:5 is the correct

square a:5

is the correct syntax because if it's an optional argument you need to give it the optional variable and the value like I stated above in the first example I gave. :)

Christopher Grant's picture

Ok, but for instance if I

Ok, but for instance if I have a simple function that returns the square of a number but defaults to the square of 10 if nothing specified I'd define it as follows:

fn square a:10 = a*a

If I now type square() I'll get 100. But if I type square 5 it'll say:
-- Argument count error: square wanted 0, got 1

It seems like by declaring it as a default in the function, it then doesn't accept that variable.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.