How do I pass an array as function argument

Hi there,

want to know how I can pass an array as an argument for a max script function.
I currently try it like this. but I get an error 'No "append" function for Global:myarray':

fn myfunc arr =
(
--do some stuff with array
append arr 3
)

myarray=#(a,b,c,d,e)
myfunc &arr

When I leave the reference symbol away the variable arr in myfunc becomes "undefined".
So how do you do that kind of stuff done right? Or is that not possible in max script?

Comments

Comment viewing options

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

search for "by reference" in the help

I think you need function with "by reference" variable. By default all function parameters in MaxScript are ByVal (i.e. pass a copy of the value), not ByRef (we marked explicit with '&' sign). And for example:

myArr = #(1,2,3) --> our initial array
num = myArr.count + 1 --> 4
append myArr num --> will modify our myArr to:
#(1,2,3,4)

But... using function:

myArr = #(1,2,3)
fn myFunc arr = (append arr (arr.count+1))
myFunc myArr --> will return NEW array = #(1,2,3,4)
-- but will NOT modify your input myArr

So, you still can assign the result of your function to your initial array like this:
myArr = myFunc myArr
Or if you want start using ByRef, then here is an example:

myArr = #(1,2,3)
-- now take a focus on the param definision (&refArr):
fn myFunc &refArr = (append refArr (refArr.count+1))
-- and take a focus on how we call that function:
myFunc &myArr -- i.e. again using '&' followed by your array name
-- now your myArr is directly modified.

Ah, and as all functions always return result, using ByRef you can combine to make your function return multi-results. Here is an illustration --

-- lets say we want to filter FreeCamera and do something with them...
camArray = for c in Cameras where classOf c == FreeCamera collect c
if camArray.count > 0 do (
	for c in camArray do ...
)
-- but we intend to wrap this pre-setup in function...
fn filtFreeCams &outArr = (
	-- we modify/assign result to variable out of the function:
	outArr = for c in Cameras where classOf c == FreeCamera collect c
	-- and supply boolean out result for our function:
	outArr.count > 0 -- i.e. False if its empty, otherwise True
)
 
-- so, now we can write our code like:
camArray = #()
if (filtFreeCams &camArray) do (for c in camArray... )

Hope this helps, Cheers!

my recent MAXScripts RSS (archive here)

Syphorlate's picture

thx miauu, yeah I tried that

thx miauu, yeah I tried that too but didn't work somehow. anyway I did it another way now, but thanks anyways

miauu's picture

( fn myfunc arr = ( --do

(
fn myfunc arr =
(
--do some stuff with array
append arr 3
)
arr = #()
-- myarray=#(a,b,c,d,e) -- #(undefined, undefined, undefined, undefined, 2.71828)
myfunc arr
)

Comment viewing options

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