Is it possible to change local variable's value from a different macro?

Macro 1 contains variable "local_number" and it's set to "false"
Can I have Macro 2 change that value to "true", or are local variables really just for that particular macro and can't be accessed at all from another macro?

If you want the value of "true" or "false" to persist after both macros have done their run you should use a Global Variable.

If Macro 2 is called by Macro 1 and the value only needs to be set for that one run, use an Instance Variable.

If Macro 2 is run as a Subroutine then you can also pass the Local Variable to it as a Parameter, but again the value will only persist for that one run.

It sounds to me that you want the value to be set by either of the two Macros in which case just use a Global Variable (i.e. a Variable without Local or Instance in its name).

1 Like

It's even tighter than that -- they're for that particular instance of that particular macro.

@Zabobon's bang on with his advice, especially with how the best way to do this will depend on what you are trying to achieve.

And having said all that, let's prove it wrong by writing a a pair of macros -- one with a local variable, the other that changes that local variable!

Local Variable Test Macros.kmmacros (5.7 KB)

Images


(I've left a 10 second pause in there so that you can disable Mains "Execute Macro" step and instead run Setter manually, just to prove there's no monkey business.)

So you can bend KM's variable scoping rules, but it takes a deliberate act -- and I certainly wouldn't recommend it!