Wednesday, April 20, 2011

Maxscript Function Pre-declaration

Anyone who has spent more than 10 minutes writing MAXScript has probably run into this problem before. You're coding along, and you do one of these:

fn funcOne = (
funcTwo()
)

fn funcTwo = (
print "Hello, World!"
)



Upon getting to the definition, Maxscript will promptly lose its shit over your function ordering. "The hell is a FuncTwo()?" it will cry. It will then halt execution and schedule an appointment with its shrink because it's halucinating about functions that don't exist.

At this point you may think, "Aha, but Maxscript has structures! Structures will solve all my problems!" This is honestly not a bad way to think. Using Maxscript structures make a lot of Maxscript headaches go away! I read about how cool they are on TA.O and everything!*

So, armed with your newfound love of structures, you write the following:

struct orderingStruct
(
fn funcOne = (
funcTwo()
),

fn funcTwo = (
print "Hello, World!"
)
)

You hit ctrl+e, thinking you've solved this problem. But no, Maxscript freaks out at you at the same point. It still has NO idea what a funcTwo() is, and promptly halts execution and cries at you. You're not really sure what's wrong here. Other languages with structures think this is just fine, but Maxscript can't deal. So, what do you do? Well, What would a C++ programmer do?

Predeclare.

The issue is that Maxscript just doesn't know what sort of thing a funcTwo is. For all it knows, it could be some crazy built-in dotNET thing. Really all you need to do is to let Maxscript know ahead of time that there is a function called funcTwo, and it will be happy. So, knowing this, you write this:



struct orderingStruct
(
/* --- Interface --- */
fn funcOne = (), fn funcTwo(),

/*--- Implementation --- */
fn funcOne =(
funcTwo()
),

fn funcTwo =(
print "Hello, World!"
)
)
And, like magic, the problem goes away!


*= Sarcasm aside, I am a HUGE advocate of structs in any and all Maxscript code. You can ALWAYS use moar structures in your Maxscripting. They are as rad as Maxscript gets!

No comments:

Post a Comment