View Full Version : Maxscript - function prototypes like C /C++

04 April 2009, 04:01 AM
Hi ,

I am from C / C++ background and worinkg on maxscript for our inhouse tools.

I like to know how I can put function prototype in the begining of the maxscript program? Like in C / C++ if we have some function that is used before its defination then we just put function prototype in the begining with semicolon at the end so the compiler knows that fuction exist and it dosnt throw any errors.

func(); //prototype with semicolon at the end

a = func() //using function before its defination

func() //function defination
{ some work


I have searched Maxscript documentation but not found anything useful...

Please help if anybody knows how it can be achived..

thanks in advance


04 April 2009, 04:25 AM
It is pretty similar, but you pre-declare the variable that holds the function. It works for ANY variable (and since functions are accessed via a pointer in a variable, it works for them too), so you can pre-declare rollouts you will be defining later, functions you will be defining later, structs, class names etc.

Obviously, if the function will be used globally, you would have to declare it in the global scope, or if you are working on a complex script where the declaration, definition and the call happen in the same code but out-of-order, in a top-most local scope.
If you intend to have a whole library of functions, it is better to declare a variable which would later be assigned a struct of functions so one global variable can give you access to an arbitrary number of functions. Also try to use a long descriptive name to avoid collisions with other scripts that might use globals.

Keep in mind that the content of \Stdplugs\StdScripts is evaluated first of all possible Script locations, so it is best to place your global definitions there (or in sub-folders of that folder which are scanned for .MS files, too). This way, any scripts in the MacroScripts folders or the Startup folder will be loaded after them and would not need to do and pre-declaration.

So if a script will be using some function that has not beed defined yet, you can say

global myCoolAndImportantGlobalFunction --will contain undefined initially

fn myLocalFunction =
myCoolAndImportantGlobalFunction() --calling the global undefined one


and then later in another or the same script define the function

fn myCoolAndImportantGlobalFunction = ( print "Hello World!") --now it is defined

Since the variable myCoolAndImportantGlobalFunction will be set to undefined (VOID) but have a valid memory address, any later references to that variable that are not prefixed with a LOCAL qualifier will be looked for in higher scopes and the global scope and will "see" the global variable there. So when you define the actual function, the already existing variable will be reused and changed to point at that function and all previous references to that variable will work as if it had always been there.

Hope this helps.

04 April 2009, 01:48 PM
Thanks Bobo for your prompt reply it relly helps me, now I understand how the functions are handled in maxscript and will be able to effectively use them in future.

your knowledge of maxscript always helps us everytime, whether it is DVD or forum we know you are always there to help. I must say you are the master of Maxscript!

Thank you again for your time

CGTalk Moderation
04 April 2009, 01:48 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.