<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: The scope of the variable confuses me in MaxScript in 3ds Max Programming Forum</title>
    <link>https://forums.autodesk.com/t5/3ds-max-programming-forum/the-scope-of-the-variable-confuses-me-in-maxscript/m-p/8783001#M8398</link>
    <description>&lt;P&gt;Okay, that's a bug. To make it go away it's enough to change the condition a bit. Here's the reduced code to reproduce:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;global test = 10
(
	if test &amp;gt; 0 then
	(
		local test = -20
		&amp;amp;test
	)
)
format "test = %\n" test --&amp;gt; -20&lt;/PRE&gt;
&lt;P&gt;The expression inside the if test returns Global:test, ie the local test is identical to the global one. It looks as if a local test variable was created by the if test implicitly yet at the same time the global one was used, and when you explicitly use 'local' later on, no error is raised as maxscript allows specifying local/global multiple times like this. To make it go away, you could use global lookup directly by prefixing the variable name with double colon:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;global test = 10
(
	if ::test &amp;gt; 0 then --&amp;gt; explicitly accessing the global variable
	(
		local test = -20
		&amp;amp;test
	)
)
format "test = %\n" test --&amp;gt; 10&lt;/PRE&gt;</description>
    <pubDate>Fri, 10 May 2019 11:09:14 GMT</pubDate>
    <dc:creator>Swordslayer</dc:creator>
    <dc:date>2019-05-10T11:09:14Z</dc:date>
    <item>
      <title>The scope of the variable confuses me in MaxScript</title>
      <link>https://forums.autodesk.com/t5/3ds-max-programming-forum/the-scope-of-the-variable-confuses-me-in-maxscript/m-p/8781018#M8395</link>
      <description>&lt;P&gt;&lt;SPAN&gt;In the maxscript help manual, the description of local variables makes me feel that its very similar to the scope rules of C language.&amp;nbsp; However, by writing a piece of test code, I feel that things are not so simple&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;global foo = 23, x = 20 --explicit declare foo,x ,scope is global &lt;BR /&gt;y = 10 --implicit declare y,scope is global &lt;BR /&gt;format "context level global: foo= %\n" foo&lt;BR /&gt;if x &amp;gt; y then&lt;BR /&gt;( -- start the first scope &lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; local fk = foo + 1 --first local variable fk，And assign the fk with the global foo &lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; local foo = y - 1 --declare a local foo，cover the global foo &lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;format "context level 1: foo= %\n" foo &lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (foo &amp;gt; 0) then&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;(--start second scope &lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;local foo = y - x --nested local variable foo，cover the first local foo &lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;format " context level 2: foo= %\n" foo&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;) --end of second scope &lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;format "context level 1: foo= %\n" foo --this output is not value in level 1,but value in level 2, bewildering!&lt;BR /&gt;) --end of first scope&lt;BR /&gt;format "context level global: foo= %\n" foo --not 23, but -10, why?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;The thing that confused me the most was if you&amp;nbsp;Get rid of this code&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt; local fk = foo + 1 &lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;the last line output global foo is 23 ,if I use foo and assigen it to fk, the last output is -10&amp;nbsp; !&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 09 May 2019 14:35:14 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/3ds-max-programming-forum/the-scope-of-the-variable-confuses-me-in-maxscript/m-p/8781018#M8395</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2019-05-09T14:35:14Z</dc:date>
    </item>
    <item>
      <title>Re: The scope of the variable confuses me in MaxScript</title>
      <link>https://forums.autodesk.com/t5/3ds-max-programming-forum/the-scope-of-the-variable-confuses-me-in-maxscript/m-p/8782517#M8396</link>
      <description>&lt;P&gt;Yes, the &lt;EM&gt;if expression&lt;/EM&gt; doesn't open a new scope. There's a list of when new scope is opened in the reference, under Scope of Variables:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;New scope contexts are opened for the following:&lt;BR /&gt;&lt;SPAN class="st"&gt;• &lt;/SPAN&gt;Top-level open parentheses in a script file or in the Listener&lt;BR /&gt;&lt;SPAN class="st"&gt;• &lt;/SPAN&gt;Start of a function body&lt;BR /&gt;&lt;SPAN class="st"&gt;• &lt;/SPAN&gt;Start of a for loop body&lt;BR /&gt;&lt;SPAN class="st"&gt;• &lt;/SPAN&gt;Start of a utility, rollout, right-click menu, Macro Script, or tool definition&lt;BR /&gt;&lt;SPAN class="st"&gt;• &lt;/SPAN&gt;Start of a rollout, utility, right-click menu, Macro Script, or tool event handler&lt;BR /&gt;&lt;SPAN class="st"&gt;• &lt;/SPAN&gt;Start of a when body&lt;/BLOCKQUOTE&gt;</description>
      <pubDate>Fri, 10 May 2019 06:49:35 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/3ds-max-programming-forum/the-scope-of-the-variable-confuses-me-in-maxscript/m-p/8782517#M8396</guid>
      <dc:creator>Swordslayer</dc:creator>
      <dc:date>2019-05-10T06:49:35Z</dc:date>
    </item>
    <item>
      <title>Re: The scope of the variable confuses me in MaxScript</title>
      <link>https://forums.autodesk.com/t5/3ds-max-programming-forum/the-scope-of-the-variable-confuses-me-in-maxscript/m-p/8782908#M8397</link>
      <description>&lt;P&gt;Thank you very much for answering my question . At your suggestion, I reconsidered the output of the code。I still don't understand why the last line "global foo " output is effected by Line 6 "local fk = foo + 1",if I comment out this line,the output is 23, if I keep this line work,the out put is -10 ?&lt;/P&gt;</description>
      <pubDate>Fri, 10 May 2019 10:20:50 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/3ds-max-programming-forum/the-scope-of-the-variable-confuses-me-in-maxscript/m-p/8782908#M8397</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2019-05-10T10:20:50Z</dc:date>
    </item>
    <item>
      <title>Re: The scope of the variable confuses me in MaxScript</title>
      <link>https://forums.autodesk.com/t5/3ds-max-programming-forum/the-scope-of-the-variable-confuses-me-in-maxscript/m-p/8783001#M8398</link>
      <description>&lt;P&gt;Okay, that's a bug. To make it go away it's enough to change the condition a bit. Here's the reduced code to reproduce:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;global test = 10
(
	if test &amp;gt; 0 then
	(
		local test = -20
		&amp;amp;test
	)
)
format "test = %\n" test --&amp;gt; -20&lt;/PRE&gt;
&lt;P&gt;The expression inside the if test returns Global:test, ie the local test is identical to the global one. It looks as if a local test variable was created by the if test implicitly yet at the same time the global one was used, and when you explicitly use 'local' later on, no error is raised as maxscript allows specifying local/global multiple times like this. To make it go away, you could use global lookup directly by prefixing the variable name with double colon:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;global test = 10
(
	if ::test &amp;gt; 0 then --&amp;gt; explicitly accessing the global variable
	(
		local test = -20
		&amp;amp;test
	)
)
format "test = %\n" test --&amp;gt; 10&lt;/PRE&gt;</description>
      <pubDate>Fri, 10 May 2019 11:09:14 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/3ds-max-programming-forum/the-scope-of-the-variable-confuses-me-in-maxscript/m-p/8783001#M8398</guid>
      <dc:creator>Swordslayer</dc:creator>
      <dc:date>2019-05-10T11:09:14Z</dc:date>
    </item>
    <item>
      <title>Re: The scope of the variable confuses me in MaxScript</title>
      <link>https://forums.autodesk.com/t5/3ds-max-programming-forum/the-scope-of-the-variable-confuses-me-in-maxscript/m-p/8850846#M8399</link>
      <description>&lt;P&gt;I'm adding a warning message being written to listener when a variable being explicitly declared as local when it has already been used in the local scope as a global (added in MAXX-50742). With that, the situation is more clear. Running your script the output is:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;20&lt;BR /&gt;10&lt;BR /&gt;context level global: foo= 23&lt;BR /&gt;OK&lt;BR /&gt;Warning - Variable 'foo' explicitly declared local when already present as global in current scope - see 'Scope of Variables' in MAXScript help - filename: G:\3dswin\bin\x64\hybrid\scripts\ScopeTest.ms; line number: 8&lt;BR /&gt;Warning - Variable 'foo' explicitly declared local when already present as global in current scope - see 'Scope of Variables' in MAXScript help - filename: G:\3dswin\bin\x64\hybrid\scripts\ScopeTest.ms; line number: 13&lt;BR /&gt;context level 1: foo= 9&lt;BR /&gt;context level 2: foo= -10&lt;BR /&gt;context level 1: foo= -10&lt;BR /&gt;OK&lt;BR /&gt;context level global: foo= -10&lt;BR /&gt;OK&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The first warning is because global foo has already been introduced into the local scope by the line:&lt;BR /&gt;local fk = foo + 1 --first local variable fk，And assign the fk with the global foo&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The second warning is because the following is not true:&lt;BR /&gt;if (foo &amp;gt; 0) then&lt;BR /&gt;(--start second scope&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As documented in 'Scope' as quoted above, mxs does not create a new scope whenever it hits a parenthesis (like most languages), it only creates a new scope in the specific cases listed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So, by design, mxs does not create a new scope when it hits a parenthesis. Doing so is possible, but is a fairly major change that would likely have behavioral impact on existing scripts and would have performance impacts (latter is the reason for this behavior).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note that the first issue can be worked around by saying:&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;local fk = ::foo + 1 --first local variable fk，And assign the fk with the global foo&lt;/P&gt;
&lt;P&gt;with that, the output is:&lt;BR /&gt;20&lt;BR /&gt;10&lt;BR /&gt;context level global: foo= 23&lt;BR /&gt;OK&lt;BR /&gt;context level 1: foo= 9&lt;BR /&gt;context level 2: foo= -10&lt;BR /&gt;context level 1: foo= -10&lt;BR /&gt;OK&lt;BR /&gt;context level global: foo= 23&lt;BR /&gt;OK&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note that the second issue is still there because we are not creating a new context level 2 as assumed, we are still in context level 1.&lt;/P&gt;</description>
      <pubDate>Thu, 13 Jun 2019 21:03:31 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/3ds-max-programming-forum/the-scope-of-the-variable-confuses-me-in-maxscript/m-p/8850846#M8399</guid>
      <dc:creator>larryminton</dc:creator>
      <dc:date>2019-06-13T21:03:31Z</dc:date>
    </item>
  </channel>
</rss>

