Notifications
Clear all

[Closed] error using function the second time in for loop

Hi,

I have a function which searches for the first key and last key of selected objects. I’m then returning array of time range start, end, first key time, last key time and slidertime.
I’m calling the function inside for loop. Problem is that everything works correctly in the first cycle, but will crash with an error on the second loop (Call needs function or class, got:).

Here is how my script looks like:

fn FindTimeRange =
 (
    ...
    --some simple stuff finding time ranges, first and last keyframes
    ... 
   -- here I'm "storing" output values into the function
   FindTimeRange = #(OrigTimeStart, OrigTimeEnd, CT, MaxTimeStart, MaxTimeEnd)
 )
 
 for I=1 to 3 do
 (
 ...
 -- running the function and loading it's values into other variable... I think I'm doing something wrong here
 TimeRanges = FindTimeRange()
 ...
 )

First loop runs ok, but the second loop will crash with this error:
– Type error: Call needs function or class, got: #(0f, 14f, 14f, 0f, 26f)
In maxscript editor cursor stops on TimeRanges = FindTimeRange()
I’m getting that error even if I manually execute single line which tries to run the function and load it into variable.

I think that I’m doing something wrong with trying to load function into variable part of the script, but can’t figure out what it is or what should I do different.

Any help would be appreciated.

2 Replies

I’m guessing you’ve used Visual Basic before, where this is how you have a function return a value. In maxscript it works a little different though.

fn FindTimeRange =
(
    FindTimeRange = #(OrigTimeStart, OrigTimeEnd, CT, MaxTimeStart, MaxTimeEnd)
)

When you create an mxs function like above, max basically creates a variable (in the above case FindTimeRange) which contains a pointer to the actual function. So FindTimeRange() calls the function.
The second line in the above script overwrites that function definition with the array, so that’s why it fails when you try calling it again. The second time the variable no longer points to the function, but to the array.

A maxscript function always returns the last value that was used in the function, so you can simply change it to:

fn FindTimeRange =
(
    #(OrigTimeStart, OrigTimeEnd, CT, MaxTimeStart, MaxTimeEnd)
)

Hope that helps,
Martijn

Eh, shame on me, should have figured that out.
Thanks a lot.