Sending class members to functions?

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

Sending class members to functions?

Post by Coiler » 10 Dec 2020, 17:55

I'm getting an error when I try to send class member variables to a function - specifically MouseGetPos(). I'm assuming it uses something similar to ByRef on its parameters. The code is simply MouseGetPos( Gesture.PA.X, Gesture.PA.Y ), and the error is "Parameter #1 of MouseGetPos must be a variable." Gesture is a global class, with PA as a nested vector2 class, and X/Y being normal members (I hope) defined in the __New() function as this.X := ***.

I'm new to programming with classes in AHK, but I'm assuming we can normally use member variables as parameters? Am I doing something wrong somewhere? I searched a great deal and could find nothing about this, so I'm hoping I did something incorrectly somewhere. But I've double checked everything.

Is this just because this function captures its args by reference, or perhaps something specific to AHK V2?

Any help is appreciated!

Main part of the vector2 class:

Code: Select all

class XVEC2
{
	; SETUP
	__New(x:=0,y:=0)
	{
		this.X := x
		this.Y := y
	}
}

BoBo
Posts: 4333
Joined: 13 May 2014, 17:15

Re: Sending class members to functions?

Post by BoBo » 10 Dec 2020, 19:19

TBH, I only know :arrow: MouseGetPos as a legacy AHK-command (instead of an UDF)???
Might work to 'force an expression' where it expects the variable? Worth a try & yes, I might be wrong :shh:
MouseGetPos,% Gesture.PA.X,% Gesture.PA.Y, ...

User avatar
kczx3
Posts: 1250
Joined: 06 Oct 2015, 21:39

Re: Sending class members to functions?

Post by kczx3 » 10 Dec 2020, 20:52

Pretty sure they have to be plain variables. You can have it output to class properties.

SOTE
Posts: 1211
Joined: 15 Jun 2015, 06:21

Re: Sending class members to functions?

Post by SOTE » 11 Dec 2020, 01:17

Before getting too deep in the weeds with Classes or in carrying over Class-Based OOP thinking from a different language, AHK (like JavaScript and Lua), is a Prototype-Based language (https://en.wikipedia.org/wiki/Prototype-based_programming). Therefore, one should not feel constrained by creating a Class or needing it, in order to use Objects or make scripts. Also the way in which one would use Classes, if you persist that you must or want to, can be a bit different.

This thread with examples, might be useful to read.
https://www.autohotkey.com/boards/viewtopic.php?t=70676
(Base Classname)

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

Re: Sending class members to functions?

Post by swagfag » 11 Dec 2020, 08:13

its something specific to ahk in general(and v2, hopefully, only for the time being)
object properties cannot be the target of OutputVar function(command) parameters(fails silently in v1; u get an error in v2)
workarounds are:
  • introduce a method that queries the x/y and assigns them to the properties
  • or have x/y be dynamic properties whose getters themselves call MouseGetPos() and return the current respective value(downside is more mousegetpos invocations, if that matters. upside is u dont have an additional method to deal with)

Post Reply

Return to “Ask For Help”