PDA

View Full Version : Passing a function into another function?


Jordo
05-08-2008, 09:30 AM
Hey guys,
Can you define a function that take another function as a variable?
I have a "searching" function that I want to send a "doing" function to but I dont think its going to work :(
The "searching" function recursively loops through subanims and propnames to find something like a TextureMap class then the "Doing" function will change/list etc things. I would like to keep the searching function separate and be able to send it different "Doing" functions.
Whats the best way to impliment this?

Cheers,
Jordan Walsh
VFX Lead
Zero One Animation (http://www.zerooneanimation.com)

davestewart
05-08-2008, 10:38 AM
I didn't think this would work either, but it seems to be quite happy:

(
-- caller function
fn test arg =
(
local scopedVariable = (random 1 10) as string
format "Calling: % [%]\n" arg (classof arg)
local result = arg()
format "Result: %\n\n" result
)

-- callee functions
fn doit =
(
format "Called: doit\n"
format "scopedVariable: %\n" scopedVariable
"doit"
)

fn testit =
(
format "Called: testit\n"
format "scopedVariable: %\n" scopedVariable
"testit"
)

-- call the functions
test doit
test testit
test (fn sendit = (format "Called: sendit\n")) -- inline function
true

/*
Calling: doit() [MAXScriptFunction]
Called: doit
scopedVariable: undefined
Result: doit

Calling: testit() [MAXScriptFunction]
Called: testit
scopedVariable: undefined
Result: testit

Calling: sendit() [MAXScriptFunction]
Called: sendit
Result: OK

true
OK
*/

)

Looks like you need to scope any variables that are needed in both functions outside both functions, but apart from that it looks good.

Doesn't qSort pass a function as one of it's arguments as well?

IkerCLoN
05-08-2008, 01:27 PM
And couldn't be the 'return' command used as a result of the function? Do you mean this, davestewart?

(
local result

fn addNumbers a b =
(
result = a + b
return result
)

print result as string
)

davestewart
05-08-2008, 03:03 PM
Actually, omitting return is best-practice in MaxScript, as behind the scenes C++ is performing a try/catch. So...


fn addNumbers a b = (local result = a + b; return result)
fn addNumbers a b = (return a + b)
fn addNumbers a b = (a + b)

...are all functionally equivilent, however they (theoretically) increase in speed as you remove





the intermediate variable declaration (fn 1)
the return call (fn 2)
So in any function, the result of the last line is what is returned (fn 3).


Look up "How To Make It Faster" in the MAXScript FAQ.



Cheers,
Dave

IkerCLoN
05-08-2008, 03:16 PM
So in any function, the result of the last line is what is returned (fn 3).

Thanks, Dave. I'll take this in consideration ;)

Jordo
05-09-2008, 02:53 AM
Cheers for that guys,
I got it working, I just had to keep passing the function into itself so it was always defined when iterating!

so its something like this:

SubFN var=
(
do stuff
)


MainFN var mySubFN
(
find stuff
mySubFN var
MainFN var mySubFN
)

MainFN obj SubFN

thatoneguy
05-09-2008, 05:25 PM
[...] Doesn't qSort pass a function as one of it's arguments as well?

Yes. [longer message to meet CGTalk posting requirements]

CGTalk Moderation
05-09-2008, 05:25 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.