Notifications
Clear all

[Closed] Passing a function into another function?

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

6 Replies

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: % [%]
" arg (classof arg)
 		local result = arg()
 		format "Result: %

" result
 		)
 		
 -- callee functions
 	fn doit =
 		(
 		format "Called: doit
"
 		format "scopedVariable: %
" scopedVariable
 		"doit"
 		)
 		
 	fn testit =
 		(
 		format "Called: testit
"
 		format "scopedVariable: %
" scopedVariable
 		"testit"
 		)
 	
 -- call the functions
 	test doit
 	test testit
 	test (fn sendit = (format "Called: sendit
")) -- 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?

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
 )

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

[ol]
[li]the intermediate variable declaration (fn 1)[/li][li]the return call (fn 2)[/li][/ol]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

So in any function, the result of the last line is what is returned (fn 3).

Thanks, Dave. I’ll take this in consideration

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

Yes. [longer message to meet CGTalk posting requirements]