Referencing constants

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
stevesax
Posts: 22
Joined: 12 Jun 2017, 15:25

Referencing constants

06 Sep 2017, 09:28

Hi. I'm starting a project where I'll be creating a number of ahk files, 1 for each window in an application, as it's a big project. I want to create 1 file with all the constants I would like to reference from each of these ahk files. Could anyone give advice on how to arrange this set up? For example, do I create a constants.ahk file and then include it within the auto execute section of each subsequent file?

Also, is this the correct way to go in order to break up what could be a lot of code, as, I'm guessing all of the .ahk files would then be compiled into numerous .exe's? or is it better to create 1 large .ahk with other files for constants and any globals etc? Any help would be greatly appreciated. THX
SirRFI
Posts: 404
Joined: 25 Nov 2015, 16:52

Re: Referencing constants

06 Sep 2017, 11:13

I guess #Include would do the trick.

Whether to split script onto many or stick to one big - probably depends on what exactly You want to do. I would split only if I would need multiple threads (loops and so on) going on simultaneously (SetTimer is usually enough though), otherwise You could #If (WinActive("ahk_exe notepad.exe")) (#If) for hotkeys etc.
Use

Code: Select all

[/c] forum tag to share your code.
Click on [b]✔[/b] ([b][i]Accept this answer[/i][/b]) on top-right part of the post if it has answered your question / solved your problem.
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Referencing constants

06 Sep 2017, 11:42

I'd opt for a more 'modular' approach. Especially if it's a huge project that afterwards has to be supported and maintained.
In ancient times I've ported a 'one script' billing system to a set of about 10 different scripts. I've never regretted that.
stevesax
Posts: 22
Joined: 12 Jun 2017, 15:25

Re: Referencing constants

06 Sep 2017, 15:09

Thanks for the replies. Theoretically a modular approach makes sense, but I'm not sure if this will result in lots of .exe's, as I'll be distributing them as exe files. I will need to maintain this. So, regarding constants, I guess a constants.ahk file is OK to include all functions used across any given ahk? Then, if this file is included, simply referencing any function from that constants.ahk file will work? I'll do some more reading up on the include statement. THX for any further advice.
SirRFI
Posts: 404
Joined: 25 Nov 2015, 16:52

Re: Referencing constants

06 Sep 2017, 16:28

What I meant in my previous post is about exes. If You are planning to do huge script, splitting it to various ahk components then it is fine, as You will gain readability. Making multiple exes though sounds like a waste of hardware resources - CPU time and memory on repetitive parts. It's likely nothing to be bothered with nowdays.

#include sorta works like You literally copy-pasted code on top of the once where it's included in. That being said if any included file will contain a return in main flow / auto-exec, then what ever would go next won't happen. When compiling into an exe, the exe will contain full code, and thus become a stand-alone. Making multiple exes with same included library won't bring any harm other than wasting memory space. This is likely why DLLs were invented.
Use

Code: Select all

[/c] forum tag to share your code.
Click on [b]✔[/b] ([b][i]Accept this answer[/i][/b]) on top-right part of the post if it has answered your question / solved your problem.
User avatar
Masonjar13
Posts: 1555
Joined: 20 Jul 2014, 10:16
Location: Не Россия
Contact:

Re: Referencing constants

06 Sep 2017, 16:37

As mentioned, #include is what you'll want for this. However, it's important to know that there are no literal constants in AHK. Meaning, they can act as constants theoretically, but if you set any other data to them, it will set it like normal. Take care to not mix up your variable names. Alternatively, you could create a class and keep all your constants in there, then have __Set() do nothing. That's the closest to literal constants you can probably get. And, like a simple variable list, you can make the class in a separate file and #include it.
OS: Windows 10 Pro | Editor: Notepad++
My Personal Function Library | Old Build - New Build
stevesax
Posts: 22
Joined: 12 Jun 2017, 15:25

Re: Referencing constants

12 Sep 2017, 16:32

Thanks again for the help and sorry for the late reply back on this, been away. Creating a class and keeping the constants in there sounds more preferable, so I'll look more into that. I haven't come across "__Set()" before, where exactly does this go and what is its purpose? I suppose having 1 big file would be possible, but locating specific code will be a pain, unless there's some utility that can help you out. THX Steve.
User avatar
Masonjar13
Posts: 1555
Joined: 20 Jul 2014, 10:16
Location: Не Россия
Contact:

Re: Referencing constants

13 Sep 2017, 03:29

__Set() is a meta-function. It is already present in an object, but may be overridden to do something during the process of setting. So, let's say you have a basic class and instantiate it. Every time you directly reference a variable with a setting operator objectVar.varName:=, it will run __Set(), even if you don't have it explicitly stated. By default, all it does is set it as normal, but you can use the following to prevent setting at all:

Code: Select all

class demoClass {
    var1:=73

    __Set(var,val){
        return
    }
}
Alternatively, you could put some checks in to allow some variables to be set.

Code: Select all

class demoClass {
    var1:=73
    var2:=75

    __Set(var,val){
        if(var="var2") ; will only allow setting of var2
            this.var2:=val
        return
    }
}
You could also reverse the filter logic.

Code: Select all

class demoClass {
    var1:=73
    var2:=75
    var3:=78

    __Set(var,val){
        if(var!="var2") ; will only disallow setting of var2
            this[var]:=val
        return
    }
}
OS: Windows 10 Pro | Editor: Notepad++
My Personal Function Library | Old Build - New Build
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Referencing constants

13 Sep 2017, 05:50

Masonjar13 wrote:__Set() is a meta-function. It is already present in an object, but may be overridden to do something during the process of setting. So, let's say you have a basic class and instantiate it. Every time you directly reference a variable with a setting operator objectVar.varName:=, it will run __Set(), even if you don't have it explicitly stated. By default, all it does is set it as normal, but you can use the following to prevent setting at all:

Code: Select all

class demoClass {
    var1:=73

    __Set(var,val){
        return
    }
}
Alternatively, you could put some checks in to allow some variables to be set.

Code: Select all

class demoClass {
    var1:=73
    var2:=75

    __Set(var,val){
        if(var="var2") ; will only allow setting of var2
            this.var2:=val
        return
    }
}
You could also reverse the filter logic.

Code: Select all

class demoClass {
    var1:=73
    var2:=75
    var3:=78

    __Set(var,val){
        if(var!="var2") ; will only disallow setting of var2
            this[var]:=val
        return
    }
}
@ Masonjar13. I do not understand your first code, not even ref.var1 will have a value if you do ref := new demoClass and ofc, demoClass.var1 is blank too. Your two other examples will cause infinite recursion.
@ Op, if you want to store values in a global object, maybe you will find it convenient and readable to do it like this,

Code: Select all

msgbox % globals.var1
class globals {
    static var1:=1
    static var2:=2
    static var3:=3
    ; ...	
}
the variable globals is super-global and you can access it anyware in the script, including inside functions. If you are really worried about the values not being constants, i.e., that they can be altered, you can do something like this I guess,

Code: Select all

class constants {
	f(v){
		; Define your constants here:
		
		static var1:=1
		static var2:=2
		static var3:=3
		
		;...
		
		; end define constants
		
		return (%v%)
	}
	__set(p*){
		return
	}
	__get(v){
		return this.f(v)
	}
}
and then try this,

Code: Select all

global myConstantGlobals := new constants
msgbox % myConstantGlobals.var1
myConstantGlobals .var1:="new value"
msgbox % myConstantGlobals.var1
Note that you can always override the __set() meta function with objrawset. So maybe just use a function,

Code: Select all

msgbox % globalConstants("var1")

globalConstants(v){
		static var1:=1
		static var2:=2
		static var3:=3
		return (%v%)
}
Cheers.
User avatar
Masonjar13
Posts: 1555
Joined: 20 Jul 2014, 10:16
Location: Не Россия
Contact:

Re: Referencing constants

13 Sep 2017, 06:39

Correct.. I forgot all references go through __Set. I meant something like:

Code: Select all

class demoClass {
    static var1:=73
    static var2:=75

    __Set(var,val){
        if(var="var2"){ ; will only allow setting of var2
            objRawSet(this,"var2",val)
            return val
        }
        return
    }
}
But then, as you mentioned, that uses the very function that could be used again to simply bypass __Set anyway. My mistake there.

The best way to implement constants might be to create a file with them set in it, then open it as a fileObject with read-only privileges with exclusive access. You'd have to wrap that into a function or something to use it reasonably, though.

A basic function would probably be the best approach in this scenario. If you put it in one of the standard libraries (local makes sense), you won't have to even use #include, it will auto-include it when the function is referenced.
OS: Windows 10 Pro | Editor: Notepad++
My Personal Function Library | Old Build - New Build
stevesax
Posts: 22
Joined: 12 Jun 2017, 15:25

Re: Referencing constants

14 Sep 2017, 10:12

Ok thanks so much for the detailed explanations and example code, a big help :). I'll need to go over this a couple more times to digest it, but definitely got some great options there. Will try to get some basic format together over the weekend....thanks again! Steve.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], LAPIII, peter_ahk and 304 guests