macroscript function scope question

macroscript function scope question

Anonymous
Not applicable
486 Views
2 Replies
Message 1 of 3

macroscript function scope question

Anonymous
Not applicable
Hi Guys,

I'm experiencing a strange problem that I can't figure out, even after reading the documentation. I have two macroscripts that are run from buttons on the max interface. They are very similar scripts at the moment as I am converting one into dotnet, so there are some functions in them that share the same name.

One of the functions i'm using is called RegisterCallbacks() and is in a separate file from the main script. It is in an include script, but this include script is not being shared between the scripts.

The problem is, that each macroscript is not using it's own RegisterCallbacks() function. It seems to use the last one that was evaluated. I'm possibly not doing something that makes these functions part of the macroscripts scope and they are in the global scope. But I wondered if this was a common occurence.

If it makes a difference, my scripts are saved in my documents and are edited and evaluated from there.

Any help is appreciated.
0 Likes
487 Views
2 Replies
Replies (2)
Message 2 of 3

Anonymous
Not applicable
With a bit of tinkering you could make the function local by wrapping the
Calling function/script in its own scope.

testScript.ms

(--My Scope Start
include "C:\\Users\\Bill\\Documents\\3dsMax\\script2.ms"
fn dostuff1 =
(
myscriptfunc()
)
dostuff1()
)--My Scope End

(--My Scope Start
include "C:\\Users\\Bill\\Documents\\3dsMax\\script3.ms"
rollout test "Test"
(
group "Group"
(
button pressme "Press Me"
on pressme pressed do
(
myscriptfunc()
)
)
)
createDialog test
)--My Scope End


And inside the include files make the function itself local
script2.ms

local myscriptfunc --Local To Its Calling Script
fn myscriptfunc =
(
messagebox "hello world"
)

script3.ms

local myscriptfunc --Local To Its Calling Script
fn myscriptfunc =
(
messagebox "hello Dolly !!"
)

Note that in this code the include files use
the same function name but with different results.
0 Likes
Message 3 of 3

Anonymous
Not applicable
Thanks for the demonstration. I seem to have fixed it using scope brackets in the included file. Seems that max was treating what was in the include file as global because if I tried to declare the function name as local in the include file, it causes the error 'cannot declare local variable at the top level'.

It thinks the code inside the include file is top level even though its included inside the scope of the macroscript.

Note, this error occurs when I evaluate the 'included' file first. I have to do this because if I just evaluate the main script, it does not get the latest code in the include file. 😕
0 Likes