Enumeration constants?

Get help with using AutoHotkey and its commands and hotkeys
User avatar
Coiler
Posts: 77
Joined: 29 Nov 2020, 09:06

Enumeration constants?

03 Dec 2020, 11:05

Is there any way to define numerical values in AHK (V1 or V2) that never change?

For example, if I have 20 specific timing values for specific actions that I don't want to spread throughout all of my script, I would do something like this..

Code: Select all

global TimeJump = 200
global TimeAttack = 100
global TimePunch = 250
global TimeKick = 50
etc..
These variables would never change, and could be optimized completely out of the script automatically when it is loaded (replaced with their values). There would also likely be some type of friendlier syntax for it, such as..

Code: Select all

enum ModifierModes
{
	ACTION = 0,
	TOOLS,
	FILTERS,
	EFFECTS,
	NUM_MODIFIERS
}
I'm not asking for a feature, just wondering if something like this currently exists? Global constants, constant enum, etc?

Thank you!
User avatar
mikeyww
Posts: 5366
Joined: 09 Sep 2014, 18:38

Re: Enumeration constants?

03 Dec 2020, 11:25

Outside functions, all variables are global constants by default. If the script doesn't change it, then it will be constant, right? :)

IniWriteIniReadGlobal variables

If you want to use inside functions, too:

Code: Select all

Global TimeJump   := 200
Global TimeAttack := 100
Global TimePunch  := 250
Global TimeKick   := 50
Or:

Code: Select all

Global TimeJump, TimeAttack, TimePunch, TimeKick
User avatar
Coiler
Posts: 77
Joined: 29 Nov 2020, 09:06

Re: Enumeration constants?

03 Dec 2020, 12:36

mikeyww wrote:
03 Dec 2020, 11:25
Outside functions, all variables are global constants by default.
Does this mean AHK optimizes the variable usage when one is not modified in the script? For example, not committing any memory to it?
mikeyww wrote:
03 Dec 2020, 11:25
If the script doesn't change it, then it will be constant, right? :)
Not in the majority of programming languages. The most important difference is that a constant variable typically prevents the programmer from changing it. Being constant also helps identify its purpose. If it can't be changed, then anyone who looks at it knows it is a value container, rather than a normal variable.

In my case, I just want to make sure I'm defining constants in the most efficient way. I'm gathering from your answer that I already am?
mikeyww wrote:
03 Dec 2020, 11:25
IniWriteIniReadGlobal variables
I appreciate the links, but I'm assuming reading and writing to an ini file would actually be less efficient than storing data in non-constant global variables? My purpose is just to keep constant arbitrary programming states organized in one place, rather than having the same static numbers written all over the script (where changing a value means searching down every instance of it and manually retyping it). Sounds like simple global variables are the way to go then?

Thanks!
User avatar
Masonjar13
Posts: 1530
Joined: 20 Jul 2014, 10:16
GitHub: Masonjar13
Location: Не Россия

Re: Enumeration constants?

03 Dec 2020, 12:45

AHK does not have literal constants. The best idea would be to create a class to house these variables, then use methods. It would likely also be much better to use an associative array (aka dict or just an object) for these times instead. Much easier for iterating through.
OS: Windows 10 Pro | Editor: Notepad++
My Personal Function Library | Old Build - New Build
User avatar
mikeyww
Posts: 5366
Joined: 09 Sep 2014, 18:38

Re: Enumeration constants?

03 Dec 2020, 12:55

Great points. Yes, there is no const.
SOTE
Posts: 1211
Joined: 15 Jun 2015, 06:21

Re: Enumeration constants?

04 Dec 2020, 08:34

Coiler wrote:
03 Dec 2020, 11:05
Is there any way to define numerical values in AHK (V1 or V2) that never change?

For example, if I have 20 specific timing values for specific actions that I don't want to spread throughout all of my script, I would do something like this..

Code: Select all

global TimeJump = 200
global TimeAttack = 100
global TimePunch = 250
global TimeKick = 50
etc..
What you can do is store such "non changing" values in an Object inside of a function. You won't have to worry about them being changed accidentally. In this way, whenever you want to call your constants, you call the function with the Object.

The other great thing about this, you can do whatever with the values in the Object variables that got the result from your function.

Code: Select all

oStart := GetBase()
MsgBox % oStart.TimeJump
Return


GetBase()
{
	oBase := {}
	oBase.TimeJump := 200
	oBase.TimeAttack := 100
	oBase.TimePunch := 250
	oBase.TimeKick := 50
	Return oBase
}
User avatar
mikeyww
Posts: 5366
Joined: 09 Sep 2014, 18:38

Re: Enumeration constants?

04 Dec 2020, 08:39

Hmm,... I don't get it. Is it really different?

Code: Select all

oStart := GetBase(), oStart.TimeJump := "A"
MsgBox % oStart.TimeJump

GetBase() {
 oBase := {}
 oBase.TimeJump := 200
 Return oBase
}
SOTE
Posts: 1211
Joined: 15 Jun 2015, 06:21

Re: Enumeration constants?

04 Dec 2020, 08:46

mikeyww wrote:
04 Dec 2020, 08:39
Hmm,... I don't get it. Is it really different?
You can only change the contents of the Object variable, oStart. You can't change oBase. Therefore you can treat the Object variable values in oBase, as if they were constants. Those values are only retrieved by calling the function.

An example can be that you built a game, where a player must restart multiple times from a basic set of values throughout the course of that game. Without constants, you can be in danger of your variables having their values changed to something unwanted. In the way in which I showed, you don't have to worry about such accidental problems like that. You can retrieve the correct starting values whenever you want.
Last edited by SOTE on 04 Dec 2020, 08:55, edited 3 times in total.
User avatar
kczx3
Posts: 1254
Joined: 06 Oct 2015, 21:39

Re: Enumeration constants?

04 Dec 2020, 20:54

You’d probably want to consider making the object static inside the function so as to not recreate it every time you call the function. Maybe not though
User avatar
Coiler
Posts: 77
Joined: 29 Nov 2020, 09:06

Re: Enumeration constants?

14 Dec 2020, 14:57

kczx3 wrote:
04 Dec 2020, 20:54
You’d probably want to consider making the object static inside the function so as to not recreate it every time you call the function. Maybe not though
The thing I don't like about that technique (that SOTE wrote) is that the entire object must be recreated every time any single property is accessed. Making it static would fix that, but it also reverses the point of the system (I think), which is to "create the values on demand so they cannot change". However, if you're like me and worry about things being optimized, its probably possible to come up with a clever way to have a function return property values without having to record them into variables or objects at all.

A switch statement would work, where properties are strings (value := func("MyProperty") ), but it would not be much more efficient than the object creation method (maybe worse, depending on how AHK looks up case values). A hash table lookup would probably be the most efficient way to convert string -> value, but then you are probably storing values in variables at some point to get it done.

I am perfectly fine using standard global variables as long as nothing better exists for the moment.
User avatar
kczx3
Posts: 1254
Joined: 06 Oct 2015, 21:39

Re: Enumeration constants?

14 Dec 2020, 16:38

Constant values typically are not "created on-demand". They are initialized (and often global, such as in PHP) once. It seems like maybe you want some kind of calculated/dynamic default? Constants, especially enumerations, are probably never dynamic.

Making the obect static inside the function just means that it is initialized once and remembered between function calls. You also make it immutable this way. You could pass a key to the getter function to retrieve just a single value as well rather than returning the entire object.
just me
Posts: 7747
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Enumeration constants?

15 Dec 2020, 05:00

Super-global creating a constant value on demand ;)

Code: Select all

MsgBox, % TimeJump()

TimeJump() {
	Return 200
}
User avatar
Coiler
Posts: 77
Joined: 29 Nov 2020, 09:06

Re: Enumeration constants?

23 Dec 2020, 11:50

What I'm looking for is just a static (unchanging) value. Essentially I want a C/C++ preprocessor macro (#define STUFF). Or a normal C/C++ const value would work too. I want to be able to avoid variables altogether, but still retain the flexibility of writing the value only once in the script.

By the way, V2's fat arrows would make function constants pretty easy to write:

Code: Select all

max_stars() => 69
time_decay() => 300
spec_key() => "Space"
If I knew AHK would optimize and remove the overhead for the function calls, I would hop right in and use that setup. Of course, for all I know, accessing a variable may have just as much overhead.

Return to “Ask For Help”

Who is online

Users browsing this forum: Arsonistic, Bing [Bot], Google [Bot], paul1965, roysubs and 63 guests