Forward declarations not working in MS?

Become a member of the CGSociety

Connect, Share, and Learn with our Large Growing CG Art Community. It's Free!

Thread Tools Search this Thread Display Modes
Old 11 November 2013   #1
Post 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!

Old 11 November 2013   #2

	--local vars
	local bodyMesh
	local cloakMesh

	  --main func
	function GrabObjs = 
		objs = pickObject count:#multiple

	bodyMesh = GrabObjs()
	print bodymesh
	cloakMesh = GrabObjs()

Old 11 November 2013   #3

Thanks miauu.. looks like I'm calling the functions incorrectly..

Last edited by Yorgo12345 : 11 November 2013 at 08:38 PM.
Old 11 November 2013   #4
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?
Old 11 November 2013   #5
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.

Last edited by miauu : 11 November 2013 at 09:39 PM.
Old 11 November 2013   #6
Quote: One way to get the objs value outside of the fn is to use:
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!
Old 11 November 2013   #7
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
Old 11 November 2013   #8
Thread automatically closed

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.
CGTalk Policy/Legalities
Note that as CGTalk Members, you agree to the terms and conditions of using this website.
Thread Closed share thread

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Society of Digital Artists

Powered by vBulletin
Copyright ©2000 - 2006,
Jelsoft Enterprises Ltd.
Minimize Ads
Forum Jump

All times are GMT. The time now is 07:03 PM.

Powered by vBulletin
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.