Jump to content
Macro Express Forums

Problem with global boolean variables?


Recommended Posts

I defined a boolean variable in one script and made it global. But when I try to use the variable in another script, Macro Express cannot find it. Instead, I get a message in the "Insert Variable" dialog box:

 

"There are no variables of the required type defined"

 

Can anybody duplicate this problem? Or am I missing something about variable handling?

Link to comment
Share on other sites

It works for me. I just created macro Test and defined a boolean var as global. In the macro I set the var to true and ran macro Test 2 which has no variables defined and a simple text box with the variable defined in Test. When run the text box displays "True". So it works. Can you give a simple example of what isn't working for you? Perhaps create a simple macro file demonstrating and attach it to a message?

Link to comment
Share on other sites

I've had the same thing happed Alan, and have to create the variables again in the Sub Macro.

The interesting thing is that if you just have a text box in the Sub Macro, the variables can be read

without having to declare them.

 

I am discovering that this may be a more general problem. My global Integer variables are not seen by other macros, either...

 

For example, here is a script called "Variable Test" that checks a pixel colour, and then sets (1) global Boolean variable "IsTargetColour" and, for good measure, (2) global Integer variable "IsTargetColourInteger".

 

Mouse Move: 500, 40 Relative to Current Window

Get Pixel Color from Beneath the Mouse into %PixelColour%

If Variable %PixelColour% Equals "12345678"

Variable Set Bool %IsTargetColour% to "True"

Variable Set Integer %IsTargetColourInteger% to 1

Else

Variable Set Bool %IsTargetColour% to "False"

Variable Set Integer %IsTargetColourInteger% to 0

End If

 

I save the macro. When executed, it works.

 

Then I create a second script called "Foo":

 

Macro Run: Variable Test

// When I insert the "If Variable" command, "IsTargetColour" and "IsTargetColourInteger" do not appear on the list of available variables

// ----- Do this if TRUE

Else

// ----- Do this if FALSE

End If

 

So... what is wrong?

Link to comment
Share on other sites

The term 'Global Variable' does not mean that the variable is available to all macros. Macro Express Pro's Global Variables are available to the macro they are defined in and all macros called by that macro. This is the same behavior as Macro Express 3.

 

Think of Global Variables as global to each macro thread, where a macro thread is the macro that runs with a single macro activation.

 

The non-global variables are limited in scope to a single macro, even if that macro is called by another macro.

Link to comment
Share on other sites

Macro Express Pro's Global Variables are available to the macro they are defined in and all macros called by that macro...

 

The non-global variables are limited in scope to a single macro, even if that macro is called by another macro.

 

Hi Kevin,

 

Please clarify.

 

I create a macro called AAA. In it, I define a global variable %V%.

 

I create a second macro, called BBB. It contains the following line:

 

Macro Run: AAA

 

Is this considered an example of a macro that calls another? And if yes, how to I access the value of %V% from within BBB?

Link to comment
Share on other sites

Alan please see my example I posted yesterday, I explained it there with an example very simialr to yours. But in your example just use the variable defined in the parent macro.

 

Perhaps you think it's not available because you don't see it on the variable tab of the called macro? If so don't fret, it will not appear there but you can still use it. And you might get a complaint from a command dialog that the variable you used is undefined but just ignore that.

 

I think people like you believe that the global variables should appear on that tab but take a moment and think of how impossible that would be. Any macro can be called by another macro in the current file or any other macro file It would be impossible to know what variables to list. But consider just one macro file. Let's say I have a macro file with 100 macros with an average of 10 global variables in each. Any of these macros could call the other so in order to cover all the possibilities one would have to display 1000 variables to you in every single macro. Not only that but some would have to appear multiple times since I can have a variable of the same name in multiple macros. So I see no way they could display them all.

Link to comment
Share on other sites

I create a macro called AAA. In it, I define a global variable %V%.

 

I create a second macro, called BBB. It contains the following line:

 

Macro Run: AAA

 

Is this considered an example of a macro that calls another? And if yes, how to I access the value of %V% from within BBB?

This thread reminds me of a similar discussion on another thread. You may want to read it here. Pay particular attention to the conclusion posted by Cory.

 

Our recommendation is that you define all global variables in the top-level macro, not the called macros. It may work to define them in the called macros but you may also have some extra prompts.

Link to comment
Share on other sites

FWIW I do a lot of sub-macros and I do define the globals used in them. It avoids the warnings but more importantly it helps me keep track of them when I use them later in a parent macro.

 

I've thought quite a bit about how to make these universal and not step on variables and I've decided on making only the passed variable global in the sub macro. And the passed variable I am making unique by adding the acronym for the macro name at the beginning like "GRFT File Name" so if I already have a name "File Name" in the parent macro I don't have a problem.

Link to comment
Share on other sites

I have read this and the other posts about global variables. Yet, I'm still have a problem. Below are the two macros I am using to test.

The variable %sVar% is defined in the Main_Macro as global but not in the Sub_Macro.

 

This works fine. But if I remove or disable the Save command in the Main_Macro and the Restore command in the Sub_Macro I receive an error:

 

The following error was encountered:

Undefined variable or the variable is the wrong type "%sVar%"

Macro Name: Sub_Macro

Line Number: 2

 

Am I suppose to have the save command prior to calling the sub_macro?

 

-----------

Main_Macro:

Variable Set String %sVar% to "Sample Text"

Text Box Display: Main Macro (displays the variable %sVar%)

Variable Save: Save Text Variables

Macro Run: Sub_Macro

 

Sub_Macro:

Variable Restore: Restore Text Variables

Variable Modify String %sVar%: Append Text (<)

Text Box Display: Sub-Macro (displays the variable %sVar%)

-------------

 

Thanks for baring with me on all the questions :blink:

Link to comment
Share on other sites

-----------

Main_Macro:

Variable Set String %sVar% to "Sample Text"

Text Box Display: Main Macro (displays the variable %sVar%)

Variable Save: Save Text Variables

Macro Run: Sub_Macro

 

Sub_Macro:

Variable Restore: Restore Text Variables

Variable Modify String %sVar%: Append Text (<)

Text Box Display: Sub-Macro (displays the variable %sVar%)

-------------

 

Is it always necessary to use "Variable Save" and "Variable Restore" when passing values between macros? If so, under what conditions? I have not been saving and restoring variables, and perhaps this is the reason I have problems when "Main_Macro" runs "Sub_Macro."

 

Does declaring the scope of a variable to "Local" and "Global" make a difference? In other words, if my macros are saving and restoring variable values, can "Main_Macro" access the value of the local variable evaluated in "Sub_Macro?"

Link to comment
Share on other sites

Is it always necessary to use "Variable Save" and "Variable Restore" when passing values between macros? If so, under what conditions? I have not been saving and restoring variables, and perhaps this is the reason I have problems when "Main_Macro" runs "Sub_Macro."

 

Does declaring the scope of a variable to "Local" and "Global" make a difference? In other words, if my macros are saving and restoring variable values, can "Main_Macro" access the value of the local variable evaluated in "Sub_Macro?"

 

This remains a murky subject to me too. I'm not using Save / Restore because, rightly or wrongly, I got the impression that it doesn't work - or at least, not in the way I'd expect.

 

An associated issue is that I recently learned from a post by Kevin that 'global' doesn't mean what I thought. There are in fact no truly global variables supported by ME Pro in the usual meaning of the word.

 

I suggested a few months ago that half a dozen worked examples from ISS would be very helpful. I still think so.

 

--

Terry, East Grinstead, UK

Link to comment
Share on other sites

An associated issue is that I recently learned from a post by Kevin that 'global' doesn't mean what I thought. There are in fact no truly global variables supported by ME Pro in the usual meaning of the word.

Au contraire, I find global variables to be pretty well what I expected. I cannot think of any language that allows you to declare a global variable which then becomes available to any program or process written in that language. A global variable is simply one that is available at and below the level it's declared at in the program which is currently running. A local variable is available only in the module that declares it.

 

All ME3 variables are global: If M1 assigns "ThisWord" to %T1%, then calls M2, M2 can address %T1% (read it, change it).

This is equally true in ME4, unless %t1% (or %t[1]%) has been declared to be local to M1, or M2 is called without having M1 wait for M2 to finish.

 

The commands to Save and Restore variables are used in a different context. If M1 assigns "ThisWord" to %tVar% and then calls M2 (waiting for M2 to finish), as stated above M2 can address %tVar%. But if M2 is activated independently, then M2 cannot see %tVar% unless M1 saves text variables, and M2 restores text variables. When you invoke a macro using the /V parameter, as in Meproc /AM2 /VtVar:ThisWord, this is similar to having M1 assign a value to %tVar% and save Text variables, and then you running M2 independently.

Link to comment
Share on other sites

Au contraire, I find global variables to be pretty well what I expected. I cannot think of any language that allows you to declare a global variable which then becomes available to any program or process written in that language.

 

But unlike you I'm not a programmer. And Macro Express is not a programming language. And I'm guessing most of its users are not programmers. So words like 'global' need more careful definition and explanation IMO.

 

--

Terry, East Grinstead, UK

Link to comment
Share on other sites

But unlike you I'm not a programmer. And Macro Express is not a programming language. And I'm guessing most of its users are not programmers. So words like 'global' need more careful definition and explanation IMO.

That's all fair comment - actually, to put it more strongly, you're quite right, and I've made the same error as below.

 

This reminds me of when I was trying to explain what the word "default" means, as in a default option. It never occurred to me that we computer nerds have taken this word "default" and completely changed its natural meaning.

Link to comment
Share on other sites

Var save/restore is not only not needed but a really bad idea. I have several large macro file where I have been spending hours removing all the save/restores and changing all my subroutines to use locals.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...