Announcements

Between mid-October and November, the content on AREA will be relocated to the Autodesk Community M&E Hub and the Autodesk Community Gallery. Learn more HERE.

How to declare a function in MaxScript

How to declare a function in MaxScript

YASUSHI_EMOTO
Enthusiast Enthusiast
864 Views
1 Reply
Message 1 of 2

How to declare a function in MaxScript

YASUSHI_EMOTO
Enthusiast
Enthusiast

I have created the following simple code
When I run this, I get an error.

 

Since the testB function is defined before the testA function, the testA function cannot be called inside the testB function. Is there a solution to these problems?

fn testB =
(
      testA()
)
fn testA =
(
      messagebox "test!" title:"OK"
)

testB()

 

The error message is as follows

 

-- Error occurred in anonymous codeblock; filename: ; position: 25; line: 3
-- Type error: Call needs function or class, got: undefined
-- MAXScript callstack:
--	thread data: threadID:35944
--	------------------------------------------------------
--	[stack level: 0]
--	In testB(); filename: ; position: 26; line: 3
--		Locals:
--			testA: undefined
--		Externals:
--			owner: undefined
--	------------------------------------------------------
--	[stack level: 1]
--	called from top-level

 

0 Likes
Accepted solutions (1)
865 Views
1 Reply
Reply (1)
Message 2 of 2

denisT.MaxDoctor
Advisor
Advisor
Accepted solution

there are several ways to do it in MXS...

 

the most used are:

 

 

 

--1:
(
	local functionB
	
	fn functionA = (functionB())
	fn functionB = (print #A)
	
	functionA()
)		

--2:
(
	struct structFN
	(
		fn functionA = (this.functionB()),
		fn functionB = (print #AA)	
	)

	str = structFN()
	str.functionA()
)

 

 

 

of course the same technique can be used in #global scope too:
  

 

global print_val

fn print_a = 
(
	print_val #a
)
fn print_val val = (print val)

print_a() ​

 

 

but you should be aware that defining custom functions or methods in the global scope is not good practice and should be avoided whenever possible.

 

0 Likes