Dynamic properties / Meta-functions: ELI5

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
jbscout
Posts: 8
Joined: 02 Nov 2021, 03:56

Dynamic properties / Meta-functions: ELI5

Post by jbscout » 23 Jan 2022, 03:09

I am trying to create a class that dynamically defines properties based on an array.

The idea is a Settings class that read/writes to an INI file and can be used across different scripts.

I would initiate the object with

Code: Select all

settings := Settings(["PosX", "PosY"])
(actually different syntax in the code below, but you get the idea from that pseudo-code)
and then use code like ...

Code: Select all

windowPositionX := settings.PosX
I tried following the example from Objects->Dynamic Properties (https://www.autohotkey.com/docs/Objects.htm#Dynamic_Properties), but something is wrong.

The script crashes in __New() at the use of this.settingsValues := {}. I never get to the "hello2" MsgBox.

What am I doing wrong? And ELI5, please.

Thanks

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
#SingleInstance force
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.


arrayOfProperties := []
arrayOfProperties[1] := new SettingsProperty("PosX", "PosX", -1, false)
arrayOfProperties[2] := new SettingsProperty("PosY", "PosY", -1, false)
arrayOfProperties[3] := new SettingsProperty("DrivesToShow", "DrivesToShow", "C|D|E|N", true)
arrayOfProperties[4] := new SettingsProperty("correctResChanges", "correctResChanges", true, true)

s := ""
for k, v in arrayOfProperties
{
	s .= k "=" 
	if isObject(v)
	{
		for kk, vv in v
			s .= "`n`t" kk "=" vv
	}
	else
	{
		s .= v
	}
	s .= "`n"
}
MsgBox % s

zettings := new Settings(arrayOfProperties)

s := ""
s .= "PosX=" zettings.PosX

MsgBox % s

Exit


class Settings
{
	__New(_arrayOfProperties)
	{
		msgbox, "hello" 
		this.settingsValues := {}
		; this.settingsConfigutations := {}
		
		msgbox, % "hello2 -- " _arrayOfProperties.Length()
		Loop, % _arrayOfProperties.Length()
		{
			currentSetting := _arrayOfProperties[A_Index]
			settingName := currentSetting.propertyName
			msgbox, % A_Index " -- " settingName " -- " currentSetting.defaultValue
			this.settingsValues[settingName] := currentSetting.defaultValue
			; this.settingsConfigurations[settingName] := currentSetting
		}
	}

	__Get(aName)
	{
		if (this.settingsValues.HasKey(aName))
		{
			aValue := this.settingsValues[aName]
			return aValue
		}
	}

	__Set(aName, aValue)
	{
		if (this.settingsValues.HasKey(aName))
		{
			this.settingsValues[aName] := aValue
			return aValue
		}
		; NOTE: Using 'return' here would break this.stored_RGB and this.RGB.
	}
	
	
	readINIFile(aIniFile="")
	{
		; read values from INI file and put in this.settingsValues{}
	}

	writeINIFile(aIniFile="")
	{
		; write values to INI file from this.settingsValues{}
	}

	writeINIFileOnClose(aIniFile="")
	{
		; fired when application closes.
		; write values to INI file from this.settingsValues{}
	}

}

class SettingsProperty
{
	__New(propertyName, iniName, defaultValue, isWriteOnClose)
	{
		this.propertyName := propertyName
		this.iniName := iniName
		this.defaultValue := defaultValue
		this.isWriteOnClose := isWriteOnClose
	}
}

swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Dynamic properties / Meta-functions: ELI5

Post by swagfag » 23 Jan 2022, 03:36

  • in the constructor, u try to set the property settingsValues. it doesnt exist yet, so metaset will be called
  • in metaset, u try to get(if statement) the property settingsValues. it doesnt exist yet, so metaget will be called
  • in metaget, u try to get(if statement) the property settingsValues. it doesnt exist yet, so metaget will be called
  • in metaget, u try to get(if statement) the property settingsValues. it doesnt exist yet, so metaget will be called
  • in metaget, u try to get(if statement) the property settingsValues. it doesnt exist yet, so metaget will be called
  • ...
use ObjRawSet() to avoid calling metafuctions or declare the property as an instance variable(so __Init() would set it)

jbscout
Posts: 8
Joined: 02 Nov 2021, 03:56

Re: Dynamic properties / Meta-functions: ELI5

Post by jbscout » 23 Jan 2022, 04:10

> use ObjRawSet() to avoid calling metafuctions or declare the property as an instance variable(so __Init() would set it)

Could you explain that in more detail? An example maybe?


jbscout
Posts: 8
Joined: 02 Nov 2021, 03:56

Re: Dynamic properties / Meta-functions: ELI5

Post by jbscout » 23 Jan 2022, 05:14

Oh, ObjRawSet() is so much easier than what I was doing!!!

ObjRawSet just dynamically creates the properties exactly how I wanted them

Thanks

The changed code is (notice __Get/Set are completely unnecessary)

Code: Select all

class Settings
{
	__New(_arrayOfProperties)
	{
		msgbox, "hello" 
		; this.settingsValues := {}
		this.settingsConfigutations := {}
		
		msgbox, % "hello2 -- " _arrayOfProperties.Length()
		Loop, % _arrayOfProperties.Length()
		{
			currentSetting := _arrayOfProperties[A_Index]
			settingName := currentSetting.propertyName
			msgbox, % A_Index " -- " settingName " -- " currentSetting.defaultValue
			ObjRawSet(this, settingName, currentSetting.defaultValue)
			; this.settingsValues[settingName] := currentSetting.defaultValue
			this.settingsConfigurations[settingName] := currentSetting
		}
	}

	readINIFile(aIniFile="")
	{
		; read values from INI file and put in this.settingsValues{}
	}

	writeINIFile(aIniFile="")
	{
		; write values to INI file from this.settingsValues{}
	}

	writeINIFileOnClose(aIniFile="")
	{
		; fired when application closes.
		; write values to INI file from this.settingsValues{}
	}

}

swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Dynamic properties / Meta-functions: ELI5

Post by swagfag » 23 Jan 2022, 05:36

if metaget/set were unnecessary, then so are ObjRawXXX() - their purpose is only to counteract metafunctions.
this[settingName] := currentSetting.defaultValue would have worked just as well

jbscout
Posts: 8
Joined: 02 Nov 2021, 03:56

Re: Dynamic properties / Meta-functions: ELI5

Post by jbscout » 23 Jan 2022, 05:47

Of course it was ... :cry:

I made that 3 levels harder than it needed to be.

Post Reply

Return to “Ask for Help (v1)”