Starting a function automatically at script startup

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
thomastthai
Posts: 18
Joined: 12 Mar 2020, 01:51

Starting a function automatically at script startup

28 Mar 2020, 16:50

I came across a code by @lexikos to have a function called automatically upon the script startup. I read about functions from the manual and did some Googling. Unfortunately, I'm still at a loss for why this works. Would someone mind help explain?

Code: Select all

init_this_file() {
    static _ := init_this_file()
    /*
    This function is called automatically when the script starts,
    regardless of where it is #included.
    */
}
Thank you in advance.

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

Re: Starting a function automatically at script startup

28 Mar 2020, 17:12

Look up static in the documentation.

In particular (see bold section):
Static Initializers: In versions prior to 1.0.46, all static variables started off blank; so the only way to detect that one was being used for the first time was to check whether it was blank. [v1.0.46+]: A static variable may be initialized to something other than "" by following it with := or = followed by one of the following: true, false, a literal integer, a literal floating point number, or a literal/quoted string such as "fox". For example: static X:=0, Y:="fox". Each static variable is initialized only once (before the script begins executing).

[AHK_L 58+]: Static var := expression is supported. All such expressions are evaluated immediately before the script's auto-execute section in the order they are encountered in the script.
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
thomastthai
Posts: 18
Joined: 12 Mar 2020, 01:51

Re: Starting a function automatically at script startup

28 Mar 2020, 20:46

Thank you @TAC109 :bravo: for taking the time to highlight the answer! I must have missed that one sentence when I read the static page in the documentation.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Chunjee, Google [Bot], macromint, peter_ahk and 312 guests