Way to exclude script sections without editing? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Onefreeman
Posts: 3
Joined: 09 Dec 2022, 12:00

Way to exclude script sections without editing?

Post by Onefreeman » 09 Dec 2022, 12:33

I am curious if it is possible to exclude or skip portions of my script at load time, and whether doing so will have the effect I am after.

I remember using #IF in C++ to exclude large sections of optional code at compile time. Usually something like #IF DEBUGGING followed by code that you only wanted to compile when actively testing & developing, but exclude in the final build.

I know AHK isn't compiled, but is there something I can do to achieve a similar outcome? My current project is getting pretty large, so I'm starting to think about performance optimizations. I have a lot of script sections that end up looking like this when I'm writing and testing out new script:

Code: Select all

reactToChange:

  debugAppend( "Waiting to React" )

  ;- Disable to timer so we can set a new interval later.
  SetTimer, reactToChange, Off

  watchColor := getPixelColor( watchX, watchY )

  if (  watchColor = reactColor ) ;- Pixel is trigger color
  {

    debugAppend( "Saw Reaction Color" )

    reactColor := getPixelColor( reactX, reactY ) ;- Check reaction

    if( reactions.HasKey( reactColor ) ) ;- Reaction is valid
    {

      debugAppend( "Valid Reaction" )
      reaction := reactions[reactColor]

      if( rangeCheck( reaction, target ) ) {
        executeReaction( reaction )
      }

      if( !verifyReaction() )
      {
        debugAppend( "Reaction Failed" )
        missedReactions += 1
      }

    }

  }

  nextTimer := baseWatchFrequencyMS + ( missedReactions * frequencyScaleMS )
  SetTimer, reactToChange, %nextTimer%

  Return
The debugAppend() function performs I/O, either to a file or an active GUI window, which is obviously pretty time intensive. So I would love to be able to turn debugging on when I'm developing & testing, then off again when actively using my script.

Is there any way to do this without manually editing the script to comment these lines out, or doing some kind of global search-and-replace?


[Mod edit: Fixed code tags to include the last line of code in the code box.]

User avatar
mikeyww
Posts: 26854
Joined: 09 Sep 2014, 18:38

Re: Way to exclude script sections without editing?

Post by mikeyww » 09 Dec 2022, 15:23

Welcome to this AutoHotkey forum!

#IF DEBUGGING sure sounds like an editing event to me! :)

You can comment a whole section at once.
In addition, the /* and */ symbols can be used to comment out an entire section, but only if the symbols appear at the beginning of a line (excluding whitespace).
AHK comes with a free compiler (as cheap as the program itself). If actually wanting to distinguish a compiled program, you can use A_IsCompiled.

Unless you are writing an AutoHotkey script to replicate Microsoft Word, you might never actually have this problem!

User avatar
boiler
Posts: 16902
Joined: 21 Dec 2014, 02:44

Re: Way to exclude script sections without editing?

Post by boiler » 09 Dec 2022, 15:43

This sounds like a valid wish list item to me. I can move this thread to the Wish List sub-forum if no one knows of a way to accomplish this without having to edit the script (other than the one-time initial adding of the directives).


User avatar
boiler
Posts: 16902
Joined: 21 Dec 2014, 02:44

Re: Way to exclude script sections without editing?

Post by boiler » 09 Dec 2022, 16:07

I might be missing something, but that would require compiling the script, correct? I’m not sure I’m seeing directives that apply only when being run by a debugger (i.e., only one file — the .ahk script file itself, and certain code is executed only when that file is run by a debugger).

User avatar
mikeyww
Posts: 26854
Joined: 09 Sep 2014, 18:38

Re: Way to exclude script sections without editing?

Post by mikeyww » 09 Dec 2022, 16:09

You're right; sorry if I missed that detail!

User avatar
boiler
Posts: 16902
Joined: 21 Dec 2014, 02:44

Re: Way to exclude script sections without editing?

Post by boiler » 09 Dec 2022, 16:15

I think the comparison to compile directives in a truly compiled language like C++ might have made it less clear. In a language like that, compiling is necessary whether to be run in debugging mode or not. In AHK, I believe the only way to debug (using a debugger) is uncompiled (and is never truly compiled anyway, just wrapped with the AHK binary executable).

User avatar
mikeyww
Posts: 26854
Joined: 09 Sep 2014, 18:38

Re: Way to exclude script sections without editing?

Post by mikeyww » 09 Dec 2022, 16:17

Thank you for the explanation! :salute: Yes, I was thinking of all situations except the one mentioned (debug)!

Onefreeman
Posts: 3
Joined: 09 Dec 2022, 12:00

Re: Way to exclude script sections without editing?

Post by Onefreeman » 09 Dec 2022, 18:30

Thanks for the kind welcome and the discussion. I had seen mention recently of compiling AHK scripts and I plan on looking into it, but it sounds like it might not be an answer to this specific wish. To be clear, what I'm looking for is the equivalent of this in C++.

#IF DEBUGGING
An arbitrary number of instructions that are completely ignored when DEBUGGING = 0
#ENDIF

I can accomplish something really close using a global boolean and wrapping my debug code in if-statements, but not only does this involve repeatedly querying a boolean whose value never changes dynamically, but it also requires me to have valid definitions for all of the variables and functions which the debug code references, even though they will never actually be used during execution of the script.

Something like #IF would allow me to excise everything - variables, functions, labels, etc. - in one fell swoop. Then by setting DEBUGGING from a command-line argument, I could use a keybind to hot-swap between the "production" and "debug" versions of the script on the fly.

Onefreeman
Posts: 3
Joined: 09 Dec 2022, 12:00

Re: Way to exclude script sections without editing?

Post by Onefreeman » 09 Dec 2022, 18:46

As a poor man's version of this, would it be possible to use a static string to replicate the behavior?

Can I set DEBUGMODE = "" when I want to debug and DEBUGMODE ="; " otherwise, then preface all debug-related instructions with %DEBUGMODE%?

In other words, is:

Code: Select all

DEBUGMODE = "; "
%DEBUGMODE%someFunction()
Functionally equivalent to:

Code: Select all

; someFunction()
It would be ugly as all get-out, but would it work?

User avatar
boiler
Posts: 16902
Joined: 21 Dec 2014, 02:44

Re: Way to exclude script sections without editing?

Post by boiler » 09 Dec 2022, 18:49

No, you can't dynamically create a comment like that.

User avatar
mikeyww
Posts: 26854
Joined: 09 Sep 2014, 18:38

Re: Way to exclude script sections without editing?  Topic is solved

Post by mikeyww » 09 Dec 2022, 19:08

by setting DEBUGGING from a command-line argument, I could use a keybind to hot-swap between the "production" and "debug" versions of the script on the fly.
A simulation of this effect would be to use the compiler directive as mentioned. You could then use a script or Windows batch file to compile the script and then run the compiled program. I think it would require no more steps than what you have described.

TAC109
Posts: 1111
Joined: 02 Oct 2013, 19:41
Location: New Zealand

Re: Way to exclude script sections without editing?

Post by TAC109 » 09 Dec 2022, 19:10

  • You could structure your script so that when run directly it includes the debugging code, but when compiled the debugging code is removed. See the ;@Ahk2Exe-IgnoreBegin ;@Ahk2Exe-IgnoreEnd statements :arrow: IgnoreBegin. (Similar to what Mickeyww wrote earlier.)
  • You could define one or more Debugging variables and include

    Code: Select all

    if Debugging = 1
      DebuggingFunction(parameters…)
    Obviously there would be a small extra cost when not debugging.
Cheers
My scripts:-
XRef - Produces Cross Reference lists for scripts
ReClip - A Text Reformatting and Clip Management utility
ScriptGuard - Protects Compiled Scripts from Decompilation
I also maintain Ahk2Exe

Post Reply

Return to “Ask for Help (v1)”