[Closed] Can functions be declared in Structs?
I like to declare all my functions at the beginning of my scripts so that I don’t have to worry about in what order the functions are defined later. But I can’t seem to do this inside a Struct, and I hate having to carefully order my function definitions to avoid “undefined function” errors.
Is it possible to declare functions in Structs somehow?
Thanks in advance!
yes they can,
struct mystruct_c
(
fn test_func a b = (
(a.pos[2] – b.pos[2])
)
)
mystruct = mystruct_c()
mystruct.test_func $point01 $point02
This is a simple version, im too getting into strut stuff, variables etc etc btw you could put this in a start up file so they dont have to be in the primary script.
eek
Charles,
Thanks for your reply, but I think maybe I wasn’t explicit enough about my question.
In your example, you defined a function, but you didn’t declare it. Declarations are like placeholders at the beginning of your script; they tell the program that a function of a given name is going to be used in the script, but the actual definition will happen later. This allows a function to call another function even if the called function is placed below the first function in the script.
So my problem is that I can’t seem to figure out how to declare functions in Structs the same way you would declare functions in the main body of the script.
structs are the greatest thing in maxscript … looking at paul hormis’ script pack i learned how he uses them, and started to use them myself, now we have a library of functions inside structs that are used throughout our macroscripts, makes things very easy to manage and update
*edit: didn’t see your second post when i wrote this
this is an interesting discussion. I will check out paul’s scripts like you suggest aearon.
In your example, you defined a function, but you didn’t declare it. Declarations are like placeholders at the beginning of your script; they tell the program that a function of a given name is going to be used in the script, but the actual definition will happen later. This allows a function to call another function even if the called function is placed below the first function in the script.
does this mean you have to declare your functions in globals within the struct, or is there no need to declare them?. Sorry if i’m not following.
Does this mean you have to declare your functions in globals within the struct, or is there no need to declare them?
Peter,
Declaring struct functions globally (even if it is possible, I haven’t tried it) defeats the purpose of having a neat and tidy localized struct package. And as far as I can tell, it seems like you can’t declare functions in structs, only variables. If this is the case, it means that you have to be careful about defining your struct functions in a specific order: if you call a struct function from another struct function, the called function must appear in the struct before the calling function. This can become almost a puzzle at times.
Surely there’s a way around this?
A large portion of the Max UI is implemented that way, also MAXScript itself provides many functions packaged in structs.
Here is the common way of doing it:
*You declare ONE global variable to hold the struct.
*You define a struct containing only functions separated by commas.
*You DON’T create an instance of this struct, you can access all the functions inside by prefixing their names with the global struct variable name.
*You save this to an .MS file in the StdPlugs/StdScripts or a subfolder of it.
*Once you restart Max, a single global variable is “wasted”, but it gives you global access to an arbitrary number of functions…
Nice and tidy!
Bobo you say DON’T create an instance? Why not? If I don’t I can’t access variables without it but I can access FN’s. So why not do it?
I don’t know why Bobo wouldn’t, but when I only have functions in it and no variables (which is true for about 95% of the structs I use) I don’t create an instance either.
I’d say that not creating another instance saves memory, but I’ve never really checked if that really makes a significant difference.
-Kees
When I am using a struct for just packaging functions, I never define variables inside, so I really don’t have to create an instance of the struct… YMMV
have you considered posting this on the autodesk maxscript forum?
if you discover anything on this topic i’d appreciate it if you’d drop me a line
Structs are very useful for keeping your code local but still being able to call what is inside globaly.
Here is an example.
struct testSt
(
myVariable=10,
fn myFn aString aFloat aPoint3:[0,1,2]=
(
format "A String Value: %
A Float Value: %
A Point3 Value: %
" aString aFloat aPoint3
),
fn callMyFn =
(
myFn "test String" 20 aPoint3:[10,100,200]
)
)
testInst=testSt()
testInst.myVariable
testInst.myFn "test String" 20
testInst.callMyFn()
You don’t want the functions to be global, but you can still call them from out side the struct.
Bobo,
until now i’ve put all my structs in scripts\startup, can you tell me where the difference/advantage with StdPlugs/StdScripts is?
As Paul said, it get’s loaded earlier. In fact, it is the place Autodesk developers put their functions The reason is that you want your functions to be defined before the .MCRs from Ui/MacroScripts get loaded. Scripts\Startup is one of the last places to load, so it is ok only if nothing depends on what is declared there.
Check out the “Startup Scripts” topic in MAXScript Reference. It lists the order and explains what happens under to hood…
Thanks to all contributing to this topic, I think some valuable things are being covered.
But I’m affraid my original question is being overlooked, so let me give an example:
struct myStruct (
fn myFunction01 = (
myFunction02()
)
fn myFunction02 = (
– do something
)
)
a=myStruct()
a.myFunction01()
This creates an error because the first function is calling a second function which isn’t declared/defined yet. I would like to be able to not worry about what order my functions are definded within the struct because it can become quite a puzzle sometimes.