Notifications
Clear all

[Closed] accessing MacroScript local vars

I have a macroscript but many of the functions are defined in external ms files which are loaded by “fileIn”. This code is not in the scope of the macroscipt so they cannot access the local variables. I would run into issues if I used global variables if I tried to run multiple instances of the macroscript.

I would really like to be able to somehow use these local macroscript variables within the other code blocks as if they were in scope…

The workaround I know of is to put all the local macroscript vars into a struct and then pass a reference to that struct to every single function or struct defined elsewhere…but thats so messy, is there a better way?

7 Replies

i would just pass the variables to the function, i know its no help to you, but thats what id do.

mark

i would just pass the variables to the function, i know its no help to you, but thats what id do.

What if it were 5 thousand instances of 10 different structs, each one wanting access to some 100 vars?

well then id make the fn’s part of the macro script and run locally

mark

The fn’s are separated into separate ms files because they are thousands of lines of code and it really slows down the workflow when you cant find the right sections quickly…

I suppose I can just use globals and then someday paste them all together when I’m done…

Have you tried using fileIn to include your scripts? This command (as opposed to include) inserts the script at compile-time which means the current scope is maintained.

Martijn

Martijn,

Actually I am using filein, as I described in my initial post…but fileIn does not maintain the scope as you say.


fn test=
(
 fn test2=()
 
 local a = 5
 
 fileIn "test2.ms"
 
 test2()
)

“file2.ms”


fn test2=
(
 messageBox (a as string)
)

output ->> undefined

Hi Stuh,

I’m sorry, I shouldn’t have replied at that time of the day

It’s actually the other way around, include is executed at compile-time, not fileIn so include is what you should use…

The example you posted wouldn’t work even if you put it in a single script like this. A nested function cannot use the locals from it’s ‘parent’ function.

(
fn test =
(
   local a = 5
 
   fn test2 =
   (
      messagebox (a as string)
   )
	
   test2()
)

test()
)

Something like this should work:

includeScript.ms :

fn includeFN =
(
	messagebox (a as string)
)

Main script :

(

local a = 5

include "includeScript.ms"

includeFN()

)

Hope this helps,
Martijn