[Closed] Forward declarations not working in MS?
Hi all,
I’m trying to pass a selection from multiple functions to a single function and have that function manip the forward vars. But it’s not working…
here is pseudocode:
--local vars
local bodyMesh
local cloakMesh
--main func
function grabObjs objs =
(
objs = pickobject count:#multiple;
)
grabObjs bodyMesh
grabObjs cloakMesh
print bodymesh -- This returns undefined! ..Why?
So this doesn’t make sense to me. isn’t the bodyMesh passed to the function as an argument? shouldn’t the objs object point back to the object passed into the function? Why is it undefined? I must be missing something, a scoping issue, or a deep/shallow reference issue, etc…
Any light that can be shed on the subject would be greatly appreciated!
-Thanks!
(
--local vars
local bodyMesh
local cloakMesh
--main func
function GrabObjs =
(
objs = pickObject count:#multiple
)
bodyMesh = GrabObjs()
print bodymesh
cloakMesh = GrabObjs()
)
[edit]
Thanks miauu… looks like I’m calling the functions incorrectly…
ok, I was able to solve my issue by using a return function instead, but I still couldn’t get the forward reference to work.
Even though I don’t need it anymore, I’d still like to know what I was doing wrong. Miauu, the example you wrote didn’t seem to solve it. I’m just curious…what aren’t I understanding about this?
You calling the main function incorectly.
local bodyMesh
bodyMesh is undefined
The main fn:
grabObjs objs = ( objs = pickobject count:#multiple; )
Then you have:
grabObjs bodyMesh
This means:
1- you call the function and pass bodyMesh, so
objs == bodyMesh
. But when the fn is executed the
objs gets new values – all the picked objects.
2- the fn returns the objs, because it is the last parameter in it, but… you did not collect this value. If you have:
bodyMesh = grabObjs()
Then the bodyMesh will holds the picked objects, that fn returns(the objs variable).
Having only
grabObjs bodyMesh
will execute the function, but since all variables in the fn is local, they are not visible outside of it, so you will not have the selected objects outside of the fn.
One way to get the objs value outside of the fn is to use:
bodyMesh = grabObjs()
There is no need to pass bodyMesh to the fn.
Other way is to use this:
local bodyMesh
fn grabObjs = ( bodyMesh = pickobject count:#multiple; )
Now, bodyMesh is defined outside of the fn as local variable and when the fn is executed the bodyMesh will change its values.
But if you have:
local bodyMesh
fn grabObjs = ( local bodyMesh = pickobject count:#multiple; )
After the execution of the fn, the bodyMesh still will be undefined, because in the fn bodyes you have declared bodyMesh as local, to the function, variable.
You can check maxscript help file for “by reference”.
I am sure that someone can explain this better than me.
One way to get the objs value outside of the fn is to use:
Code:
bodyMesh = grabObjs()
Yes, This is exactly what I ended up doing. It worked.
I guess I am just used to other languages where the scope seems to behave differently. I have freequently forward declared variables, and paseed them into funtions for manipulation or assignment… If that’s just not the way maxscript works, that’s fine.
…Also, I didn’t know that maxscript functions automatically return the last parameter! Werid! …but cool. – I’ll get used to it
thanks again Miauu!
By default maxscript passes parameters to functions by value, not by reference. So the original variable is not changed if you do not assign the function result to it. You can however pass by reference using “&”. You have to put it in front of any paramater that you want to pass by reference in the function definition and whenever calling the function, so the actual variable is changed. Like so:
function grabObjs &objs =
(
objs = pickobject()
)
grabObjs &bodyMesh
grabObjs &cloakMesh