Problems assigning default values to variables in a function
Welcome to ScriptSpot!
Interesting catch. I haven't noticed this behavior before but I'm getting similar results here. I was perusing the MAXScript help under "Function Parameters" and unless I'm missing something - its saying you should be using var:value as the syntax for default function values which is exactly what you're doing...
Anyone else have any ideas?
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.
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
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]
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




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:
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
the answer is:
-- Argument count error: sign wanted 0, got 1if I remove the syntax for the suposedly default value on the first line, like that:
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
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!