CGTalk > Software > Autodesk 3ds max > 3dsMax SDK and MaxScript
Login register
Thread Closed share thread « Previous Thread | Next Thread »  
 
Thread Tools Search this Thread Display Modes
Old 11-21-2013, 12:04 AM   #1
Yorgo12345
www.luminous-arts.com
 
Yorgo12345's Avatar
portfolio
George Lambrakis
3D lead
Luminous Arts
New York City, USA
 
Join Date: Mar 2003
Posts: 321
Send a message via AIM to Yorgo12345
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:
Code:
--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!
__________________
http://www.luminous-arts.com
 
Old 11-21-2013, 12:46 AM   #2
miauu
Expert
 
miauu's Avatar
portfolio
Kostadin Kotev
Blagoevgrad, Bulgaria
 
Join Date: Sep 2009
Posts: 774
Code:
( --local vars local bodyMesh local cloakMesh --main func function GrabObjs = ( objs = pickObject count:#multiple ) bodyMesh = GrabObjs() print bodymesh cloakMesh = GrabObjs() )
 
Old 11-21-2013, 08:22 PM   #3
Yorgo12345
www.luminous-arts.com
 
Yorgo12345's Avatar
portfolio
George Lambrakis
3D lead
Luminous Arts
New York City, USA
 
Join Date: Mar 2003
Posts: 321
Send a message via AIM to Yorgo12345
[edit]

Thanks miauu.. looks like I'm calling the functions incorrectly..
__________________
http://www.luminous-arts.com

Last edited by Yorgo12345 : 11-21-2013 at 08:38 PM.
 
Old 11-21-2013, 09:06 PM   #4
Yorgo12345
www.luminous-arts.com
 
Yorgo12345's Avatar
portfolio
George Lambrakis
3D lead
Luminous Arts
New York City, USA
 
Join Date: Mar 2003
Posts: 321
Send a message via AIM to Yorgo12345
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?
__________________
http://www.luminous-arts.com
 
Old 11-21-2013, 09:24 PM   #5
miauu
Expert
 
miauu's Avatar
portfolio
Kostadin Kotev
Blagoevgrad, Bulgaria
 
Join Date: Sep 2009
Posts: 774
You calling the main function incorectly.

Code:
local bodyMesh

bodyMesh is undefined

The main fn:
Code:
grabObjs objs = ( objs = pickobject count:#multiple; )


Then you have:
Code:
grabObjs bodyMesh

This means:
1- you call the function and pass bodyMesh, so
Code:
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:
Code:
bodyMesh = grabObjs()

Then the bodyMesh will holds the picked objects, that fn returns(the objs variable).

Having only
Code:
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:
Code:
bodyMesh = grabObjs()

There is no need to pass bodyMesh to the fn.

Other way is to use this:

Code:
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:

Code:
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-21-2013 at 09:39 PM.
 
Old 11-22-2013, 05:39 PM   #6
Yorgo12345
www.luminous-arts.com
 
Yorgo12345's Avatar
portfolio
George Lambrakis
3D lead
Luminous Arts
New York City, USA
 
Join Date: Mar 2003
Posts: 321
Send a message via AIM to Yorgo12345
Quote:
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!
__________________
http://www.luminous-arts.com
 
Old 11-28-2013, 06:57 PM   #7
butterbr0t
Scout
Christoph Bülter
Germany
 
Join Date: Dec 2004
Posts: 40
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:
Code:
function grabObjs &objs = ( objs = pickobject() ) grabObjs &bodyMesh grabObjs &cloakMesh
__________________
www.cbuelter.de
 
Old 11-28-2013, 06:57 PM   #8
CGTalk Moderation
Lord of the posts
CGTalk Forum Leader
 
Join Date: Sep 2003
Posts: 1,066,480
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


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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
CGSociety
Society of Digital Artists
www.cgsociety.org

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

All times are GMT. The time now is 10:22 AM.


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