Jump to content

Recommended Posts

Posted (edited)

Earlier, I wrote some code to test whether I could pass information from one script to another, but the variable may not always exist in the calling script.

The following is an example of what I had:
Script.au3:

#include "MyFunc.au3"
Global $file = FileOpen("text.txt", 1)

MyFunc.au3:

Local $file = "" ;Pre-script ToolTip. This will be overwritten by the calling script's $file if there is one.

If($file <> 0) Then ;If there is a $file in the calling script.
   FileWrite($file, "Hello")
EndIf

However, what I need now is the 2 lines in Script.au3 swapped around. That way, the variable's value can be defined before the include is done, e.g.:
Script.au3:

Global $startMessage = "CustomMessage"
#include "MyFunc.au3"

MyFunc.au3:

Local $startMessage = 0 ;Pre-script ToolTip. This will be overwritten by the calling script's $startMessage if there is one.

If($startMessage = 0) Then ;If there is no $startMessage in the calling script.
   ToolTip("Press ` to begin the script.", 0, 0) ;Display the default instruction.
Else
   ToolTip($startMessage, 0, 0) ;Display the $startMessage defined by the calling script.
EndIf


Is this possible? Obviously, if I just switch the 2 lines around, $startMessage will always be 0 while the include is happening.
I have also tried IsDeclared(), but that seems to only work inside MyFunc.au3 and can't see Script.au3's variable.

The reason I want my code like this is because I have a function which is included into all scripts (which pauses the script before it begins running and it displays a tooltip on how to begin the script, and what preconditions are expected - note this tooltip is displayed before the pause begins). Then, any script anyone makes just needs to define a Global $startMessage and #include "StartMessage.au3".

 

Thank you in advance.

Edited by IAMK
Posted

When you run the script MyFunc.au3. Your program  only  run code of MyFunc.au3.
But when you run script Script.au3, your program will include code for both script MyFunc.au3 and Script.au3.

Regards,
 

Posted

You might do it like this

; in the #include
Func _MyFunc($text)
  If($text = "") Then ;If there is no $startMessage in the calling script.
     ToolTip("Press ` to begin the script.", 0, 0) ;Display the default instruction.
  Else
     ToolTip($text, 0, 0) ;Display the $startMessage defined by the calling script.
  EndIf
EndFunc

; in the script
$startMessage = "CustomMessage"
_MyFunc($startMessage)

 

Posted

@VIP I never run MyFunc.au3 (it's purely an include file).

@mikell My goal was to not have function calls. I wanted just the include.
However, to achieve my goal, I will do what you wrote.

Thanks.

Posted

Your includes should never have variables used in them that aren't defined inside the include or included within that include file. 

Local $startMessage = 0 ;Pre-script ToolTip. This will be overwritten by the calling script's $startMessage if there is one.

This comment is incorrect. When you redeclare the variable, you redefine what the variable contents are.

Run this demo script to see what I mean.

Global $test = 1
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $test = ' & $test & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
Global $test = 2
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $test = ' & $test & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console

BTW, ONLY use Local if you're using it inside a function, any other use is confusing. While technically it's not wrong usage, it's not the right way to do it. Local used in a Global scope is still a Global variable.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Posted

@BrewManNH Trust me that it works (the first example).

Here is 100% complete test scripts:
temp.au3:

#include "temp2.au3"
Global $file = FileOpen("temptext.txt", 1)
LogToFile()
FileClose($file)

temp2.au3:

Local $file = ""

Func LogToFile()
   If($file <> "") Then ;If there is a $file in the calling script.
      FileWrite($file, "Hello")
   EndIf
EndFunc

It first includes temp2, which makes the Local $file = "", then it goes back to temp and makes the Global $file = Not "", then it calls the function in temp2, and $file holds the value of what was given in temp.

Does that make sense?

Posted

It works but looks inconsistent. Once compiled the whole will internally looks like this :

Local $file = ""

Func LogToFile()
   If($file <> "") Then ;If there is a $file in the calling script.
      FileWrite($file, "Hello")
   EndIf
EndFunc

Global $file = FileOpen("temptext.txt", 1)
LogToFile()
FileClose($file)

while it's much cleaner if done like this :

Func LogToFile($file)
   If($file <> "") Then ;If there is a $file in the calling script.
      FileWrite($file, "Hello")
   EndIf
EndFunc

Global $my_file = FileOpen("temptext.txt", 1)
LogToFile($my_file)
FileClose($my_file)

So in the other different scripts, if the variable name "$my_file"  changes, the script will work anyway

Posted
4 hours ago, IAMK said:

Trust me that it works (the first example).

I don't need to trust what you say, your comment is incorrect, it will NOT be overwritten if you define the variable first, then call the #include. If you put the include first, then redefine the variable, of course it will be changed, that's exactly what I said it would do. 

Defining a global variable, used in your include file, in the main script makes no sense, either define it in the main script, or in the include, do not redefine it in both. Otherwise you're going to make thing much more difficult for yourself when something gets a different value than you think it has.

Also, as I said before, don't use the Local keyword in the global scope, especially since you're using the variable as a Global inside your functions. It's global and there's no file scope in AutoIt.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...