Question about Global, Local and just plain variable types

Become a member of the CGSociety

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

THREAD CLOSED
 
Thread Tools Search this Thread Display Modes
Old 06 June 2013   #1
Question about Global, Local and just plain variable types

Hey Everybody,

The further and further I have gotten into Max scripting the the more and more questions I have about how to exactly use variable identifiers like Global and Local. This is how I think they should be used, please correct me if I am mistaken.

Global: By setting a variable to global means that at any time and anywhere in the script you can access this variable. It also means that you can access this variable outside of the script. For example if I need one script to talk to another one I can use global to send information between the two.

Local: By setting a variable to local means that you can only access this in the function that it is associated with. Meaning that you can not call it from anywhere in the script and it can not read or write data from other scripts.

Undefined: This is the one that I understand the least and its probably the easiest one to understand. An undefined is what I call the following...

a = $Foo


So a is equal to your selection but since I did not define a as either local or global which is it? Is it a local variable, a global one or other? I have read the documentation about this already to try and clear this up but I just wanted to make sure that I was understanding the information correctly. So am I understanding this correctly? As always any and all help would be greatly appreciated.
__________________
|*BILLY$CLINT*|



 
Old 06 June 2013   #2
It depends where you define a...

a = 10 -- this is at no indentation point so is assumed to be global

(
    a = 20 -- this is within a scope so it is bound by the scope and is implicitly local, but as we already have the variable a defined globally, it picks up the global value.
)

print a -- returns 20


If however we define 'a' as local within a scope it is bound only to that level of scope.

a = 10 -- this is at no indentation point so is assumed to be global

(
    local a = 20 -- this is within a scope so it is bound by the scope and is defined as local, it doesn't change the global value
)

print a -- returns 10


If we just define a as local without assigning a value it doesn't pick up the global value as you might expect
a = 10
(
	local a
	print a --prints undefined
)

print a



See more on my blog....

http://davewortley.wordpress.com/20...topped-working/
__________________
Maxscript Made Easy...
http://davewortley.wordpress.com/
 
Old 06 June 2013   #3
Awesome dude that was exactly what I was looking for.
__________________
|*BILLY$CLINT*|



 
Old 06 June 2013   #4
Originally Posted by DaveWortley: It depends where you define a...

a = 10 -- this is at no indentation point so is assumed to be global

(
    a = 20 -- this is within a scope so it is bound by the scope and is implicitly local, but as we already have the variable a defined globally, it picks up the global value.
)

print a -- returns 20
This example doesn't work. If the second declaration of a were implicitly local the print expression should return 10 as this example does:
a = 10

(
    local a = 20
)

print a -- returns 10
This is why you never declare a variable without explicitly stating whether it is local or global.
 
Old 06 June 2013   #5
I'm pretty sure what I wrote was correct did you read the entire line of comment I wrote?

Quote: a = 20 -- this is within a scope so it is bound by the scope and is implicitly local, but as we already have the variable a defined globally, it picks up the global value.
__________________
Maxscript Made Easy...
http://davewortley.wordpress.com/
 
Old 06 June 2013   #6
Sorry I see what you mean, I didn't read it properly.
 
Old 06 June 2013   #7
Originally Posted by Raytracer05: Sorry I see what you mean, I didn't read it properly.


No worries, good to double check
__________________
Maxscript Made Easy...
http://davewortley.wordpress.com/
 
Old 06 June 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
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 03:04 PM.


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