Problem with "Class" in V2

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
Topfpflanze
Posts: 3
Joined: 28 Mar 2023, 06:44

Problem with "Class" in V2

Post by Topfpflanze » 28 Mar 2023, 07:16

My Collegue from Work has left our Company, so iam here now with an AutoHotKey Function and some Problems iam not able to solve right now.
Iam more from the Microsoft Power Automate Area, so basics are here but not much. Did already some search on the v2 Boards, but getting lost with all the other Topics, so i try it with some basics.

The Script has already been working in V1, but it was converted to V2 (because i dont know)

Code: Select all

HasVal(haystack, needle) {
    if !(IsObject(haystack)) || (haystack.Length = 0)
        return 0
    for index, value in haystack
        if (value = needle)
        return index
    return 0
}

class Button {
    __New(pos1,pos2){
        this.pos1 := pos1
        this.pos2 := pos2
        this.center := this._getCenter()
    }
    _getCenter(){
        x := this.pos1["x"] + ((this.pos2["x"] - this.pos1["x"]) / 2)
        y := this.pos2["y"] + ((this.pos1["y"] - this.pos2["y"]) / 2)
        return {x:x,y:y}
    }
    print(){
        MsgBox("this.pos1[`"x`"] this.pos1[`"y`"]")
    }
    isExists(){
        color1 := PixelGetColor(this.pos1["x"], this.pos1["y"], ) 
        color2 := PixelGetColor(this.pos2["x"], this.pos2["y"], ) 
        return hasVal(this.pos1["colors"], color1) or hasVal(this.pos2["colors"], color2)
    }
    click(){
        MouseClick("left", this.center["x"], this.center["y"], , 0)
    }
}

startBMD := Button({x: 346,y: 639,colors: [0xF57000 , 0xFF7500]}, {x: 351,y: 631,colors: [0xF59808, 0xFF9E08]})
NTCS := Button({x: 244,y: 621,colors: [0x232339 , 0x232339]}, {x: 453,y: 278,colors: [0x4EAA94, 0x4EAA94]})
I get an Error "Error: This value of type "Object" has no property named "__Item"."
Why should i need an Item?
The reason behind all this is to use a class for different Calls with different Color Pickers.
I personally would prefer some kind of imagesearch, but well, first Babysteps.

would be nice if somebody tell me what iam doing wrong and where to find the correct Infos.

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

Re: Problem with "Class" in V2

Post by swagfag » 28 Mar 2023, 08:19

because ur code contains stuff like this.pos1["x"]
in which the square brackets are an operator for accessing the __Item property

since ure passing Objects in the constructor, u can rewrite all of these to read this.pos1.x
or u can keep using this.pos1["x"], but then u have to pass not Objects, but Maps instead

Topfpflanze
Posts: 3
Joined: 28 Mar 2023, 06:44

Re: Problem with "Class" in V2

Post by Topfpflanze » 28 Mar 2023, 08:32

Thanks a lot!
I also changed this now

Other things now should just be the usual Beginner Problems :)

Code: Select all

isExists(){
        PixelGetColor(this.pos1.x, this.pos1.y,0)
        PixelGetColor(this.pos2.x, this.pos2.y,0)
        return HasVal(this.pos1.colors, color1) or HasVal(this.pos2.colors, color2)
Dont understand this return, but i think i can handle this.

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

Re: Problem with "Class" in V2

Post by swagfag » 28 Mar 2023, 09:09

and i dont understand what u dont understand. what is the question?

Topfpflanze
Posts: 3
Joined: 28 Mar 2023, 06:44

Re: Problem with "Class" in V2

Post by Topfpflanze » 29 Mar 2023, 00:49

@swagfag
This part
return HasVal(this.pos1.colors, color1) or HasVal(this.pos2.colors, color2)
How can a Return Value1 OR Value2 sein?

User avatar
Mono919
Posts: 71
Joined: 05 May 2022, 02:36

Re: Problem with "Class" in V2

Post by Mono919 » 29 Mar 2023, 01:31

Topfpflanze wrote:
28 Mar 2023, 07:16
My Collegue from Work has left our Company, so iam here now with an AutoHotKey Function and some Problems iam not able to solve right now.
Iam more from the Microsoft Power Automate Area, so basics are here but not much. Did already some search on the v2 Boards, but getting lost with all the other Topics, so i try it with some basics.

The Script has already been working in V1, but it was converted to V2 (because i dont know)

Code: Select all

HasVal(haystack, needle) {
    if !(IsObject(haystack)) || (haystack.Length = 0)
        return 0
    for index, value in haystack
        if (value = needle)
        return index
    return 0
}

class Button {
    __New(pos1,pos2){
        this.pos1 := pos1
        this.pos2 := pos2
        this.center := this._getCenter()
    }
    _getCenter(){
        x := this.pos1["x"] + ((this.pos2["x"] - this.pos1["x"]) / 2)
        y := this.pos2["y"] + ((this.pos1["y"] - this.pos2["y"]) / 2)
        return {x:x,y:y}
    }
    print(){
        MsgBox("this.pos1[`"x`"] this.pos1[`"y`"]")
    }
    isExists(){
        color1 := PixelGetColor(this.pos1["x"], this.pos1["y"], ) 
        color2 := PixelGetColor(this.pos2["x"], this.pos2["y"], ) 
        return hasVal(this.pos1["colors"], color1) or hasVal(this.pos2["colors"], color2)
    }
    click(){
        MouseClick("left", this.center["x"], this.center["y"], , 0)
    }
}

startBMD := Button({x: 346,y: 639,colors: [0xF57000 , 0xFF7500]}, {x: 351,y: 631,colors: [0xF59808, 0xFF9E08]})
NTCS := Button({x: 244,y: 621,colors: [0x232339 , 0x232339]}, {x: 453,y: 278,colors: [0x4EAA94, 0x4EAA94]})
I get an Error "Error: This value of type "Object" has no property named "__Item"."
Why should i need an Item?
The reason behind all this is to use a class for different Calls with different Color Pickers.
I personally would prefer some kind of imagesearch, but well, first Babysteps.

would be nice if somebody tell me what iam doing wrong and where to find the correct Infos.
You confused the relationship between map and object-attributes. In v1, xxx. yyy=xxx ["yyy"], but in v2, this is obviously not the case.
Real Usage(Object):

Code: Select all

HasVal(haystack, needle) {
    if !(haystack is Array) || (haystack.Length = 0)
        return 0
    for index, value in haystack
        if (value = needle)
        return index
    return 0
}

class Button {
    __New(pos1,pos2){
        this.pos1 := pos1
        this.pos2 := pos2
        this.center := this._getCenter()
    }
    _getCenter(){
        x := this.pos1.x + ((this.pos2.x - this.pos1.x) / 2)
        y := this.pos2.y + ((this.pos1.y - this.pos2.y) / 2)
        return {x:x,y:y}
    }
    print(){
        MsgBox("this.pos1.x this.pos1.y")
    }
    isExists(){
        color1 := PixelGetColor(this.pos1.x, this.pos1.y, ) 
        color2 := PixelGetColor(this.pos2.x, this.pos2.y, ) 
        return hasVal(this.pos1.colors, color1) or hasVal(this.pos2.colors, color2)
    }
    click(){
        MouseClick("left", this.center.x, this.center.x, , 0)
    }
}

startBMD := Button({x: 346,y: 639,colors: [0xF57000 , 0xFF7500]}, {x: 351,y: 631,colors: [0xF59808, 0xFF9E08]})
NTCS := Button({x: 244,y: 621,colors: [0x232339 , 0x232339]}, {x: 453,y: 278,colors: [0x4EAA94, 0x4EAA94]})
Real Usage(Map):

Code: Select all

HasVal(haystack, needle) {
    if !(haystack is Array) || (haystack.Length = 0)
        return 0
    for index, value in haystack
        if (value = needle)
        return index
    return 0
}

class Button {
    __New(pos1,pos2){
        this.pos1 := pos1
        this.pos2 := pos2
        this.center := this._getCenter()
    }
    _getCenter(){
        x := this.pos1["x"] + ((this.pos2["x"] - this.pos1["x"]) / 2)
        y := this.pos2["y"] + ((this.pos1["y"] - this.pos2["y"]) / 2)
        return map("x", x, "y", y)
    }
    print(){
        MsgBox("this.pos1[`"x`"] this.pos1[`"y`"]")
    }
    isExists(){
        color1 := PixelGetColor(this.pos1["x"], this.pos1["y"], ) 
        color2 := PixelGetColor(this.pos2["x"], this.pos2["y"], ) 
        return hasVal(this.pos1["colors"], color1) or hasVal(this.pos2["colors"], color2)
    }
    click(){
        MouseClick("left", this.center["x"], this.center["y"], , 0)
    }
}

startBMD := Button(map("x", 346,"y", 639,"colors", [0xF57000 , 0xFF7500]), map("x", 351,"y", 631,"colors", [0xF59808, 0xFF9E08]))
NTCS := Button(map("x", 244,"y", 621,"colors", [0x232339 , 0x232339]), map("x", 453,"y", 278,"colors", [0x4EAA94, 0x4EAA94]))

just me
Posts: 9407
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Problem with "Class" in V2

Post by just me » 29 Mar 2023, 02:41

Topfpflanze wrote:How can a Return Value1 OR Value2 sein?
Short-circuit Boolean Evaluation

Moin,

if the first condition is true, the function also returns true. Otherwise (Or)t he second condition will be checked. If true, the function also returns true, otherwise false.

Post Reply

Return to “Ask for Help (v2)”