acantor Posted April 23, 2009 Report Share Posted April 23, 2009 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? Quote Link to comment Share on other sites More sharing options...
Cory Posted April 23, 2009 Report Share Posted April 23, 2009 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? Quote Link to comment Share on other sites More sharing options...
redcordial Posted April 23, 2009 Report Share Posted April 23, 2009 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. Quote Link to comment Share on other sites More sharing options...
acantor Posted April 24, 2009 Author Report Share Posted April 24, 2009 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? Quote Link to comment Share on other sites More sharing options...
kevin Posted April 24, 2009 Report Share Posted April 24, 2009 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. Quote Link to comment Share on other sites More sharing options...
acantor Posted April 24, 2009 Author Report Share Posted April 24, 2009 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? Quote Link to comment Share on other sites More sharing options...
Cory Posted April 24, 2009 Report Share Posted April 24, 2009 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. Quote Link to comment Share on other sites More sharing options...
kevin Posted April 24, 2009 Report Share Posted April 24, 2009 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. Quote Link to comment Share on other sites More sharing options...
Cory Posted April 24, 2009 Report Share Posted April 24, 2009 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. Quote Link to comment Share on other sites More sharing options...
redwards Posted April 30, 2009 Report Share Posted April 30, 2009 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 Quote Link to comment Share on other sites More sharing options...
redwards Posted April 30, 2009 Report Share Posted April 30, 2009 Well, after much experimenting I found the problem. I didn't check the box "Wait for this macro to terminate before proceeding" I'm bad SORRY Quote Link to comment Share on other sites More sharing options...
acantor Posted April 30, 2009 Author Report Share Posted April 30, 2009 -----------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?" Quote Link to comment Share on other sites More sharing options...
terrypin Posted April 30, 2009 Report Share Posted April 30, 2009 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 Quote Link to comment Share on other sites More sharing options...
paul Posted May 1, 2009 Report Share Posted May 1, 2009 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. Quote Link to comment Share on other sites More sharing options...
terrypin Posted May 1, 2009 Report Share Posted May 1, 2009 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 Quote Link to comment Share on other sites More sharing options...
paul Posted May 1, 2009 Report Share Posted May 1, 2009 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. Quote Link to comment Share on other sites More sharing options...
Cory Posted May 1, 2009 Report Share Posted May 1, 2009 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. Quote Link to comment Share on other sites More sharing options...
Cory Posted May 2, 2009 Report Share Posted May 2, 2009 Hey, I started a page to make a better visual and I was wondering if this cleared things up for anyone. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.