How to call a class only once and access it as many times as needed? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
braveknightrs
Posts: 6
Joined: 04 Jun 2023, 22:58

How to call a class only once and access it as many times as needed?  Topic is solved

Post by braveknightrs » 04 Jun 2023, 23:09

I'm attempting to call a class called "Grid" and use that instance as many times as needed throughout my script. It only seems to work if I make another instance everytime I need to use it in different function.

What i'm trying to achieve: Using the same instance in the entire script.

What i'm trying to achieve but doesn't seem to work. Nothing seems to execute

Code: Select all

#NoEnv
#SingleInstance, Force
SendMode, Input
SetBatchLines, -1
SetWorkingDir, %A_ScriptDir%

#Include, GridClass.ahk

MyGrid := new Grid() ;i've tried making this global aswell, same outcome

func1() {
    MyGrid.clickcell(5)
}

func2() {
    MyGrid.ShowCoordinates()
}

func3() {
    MyGrid.RandomPattern()
}

1::
func1()
return

2::
func2()
return

3::
func3()
return
This is what works:

Code: Select all

#NoEnv
#SingleInstance, Force
SendMode, Input
SetBatchLines, -1
SetWorkingDir, %A_ScriptDir%

#Include, GridClass.ahk


func1() {
    MyGrid := new Grid()
    MyGrid.clickcell(5)
}

func2() {
    MyGrid := new Grid()
    MyGrid.ShowCoordinates()
}

func3() {
    MyGrid := new Grid()
    MyGrid.RandomPattern()
}
1::
func1()
return

2::
func2()
return

3::
func3()
return
Snippet of my Grid class:

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
#SingleInstance, Force

class Grid {
    ;other code in here;

    ClickCell(cellNumber) {
        
    }

    RandomPattern() {
        
    }

    ShowCoordinates() {
        
    }
    
}

braveknightrs
Posts: 6
Joined: 04 Jun 2023, 22:58

Re: How to call a class only once and access it as many times as needed?

Post by braveknightrs » 04 Jun 2023, 23:33

updated code of what my grid class looks like..

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
#SingleInstance, Force

class Grid {
    __New() {
    ;stuff in here
     }

    ;functions
    ClickCell(cellNumber) {
        
    }

    RandomPattern() {
        
    }

    ShowCoordinates() {
        
    }
    
}

User avatar
boiler
Posts: 16960
Joined: 21 Dec 2014, 02:44

Re: How to call a class only once and access it as many times as needed?

Post by boiler » 05 Jun 2023, 00:24

You must have implemented the global declaration incorrectly or tested it incorrectly when you implemented it. It works fine when you do that. If your simplified version of the class is used to demonstrate, what are you expecting to see? The class methods don’t do anything. Stick a MsgBox in each to see that it works.

By the way, why would you put all the header lines in the class file to be included? Then you have them twice since they’re already in the main file. Your include file should contain just the class itself.

braveknightrs
Posts: 6
Joined: 04 Jun 2023, 22:58

Re: How to call a class only once and access it as many times as needed?

Post by braveknightrs » 05 Jun 2023, 01:16

boiler wrote:
05 Jun 2023, 00:24
You must have implemented the global declaration incorrectly or tested it incorrectly when you implemented it. It works fine when you do that. If your simplified version of the class is used to demonstrate, what are you expecting to see? The class methods don’t do anything. Stick a MsgBox in each to see that it works.

By the way, why would you put all the header lines in the class file to be included? Then you have them twice since they’re already in the main file. Your include file should contain just the class itself.
Hey, It seems you were right I might've implemented the global declaration incorrectly. Not sure how but after retesting it with global mygrid := new Grid() , it works.
The classes were just skeletons to see if I had done something wrong structuring it as to not clutter my post,
As for the headers in the class file, it's my first time working with class files as i've only done simple loop scripts before which never required them so it wasn't something I looked out for. Appreciate the tip

User avatar
boiler
Posts: 16960
Joined: 21 Dec 2014, 02:44

Re: How to call a class only once and access it as many times as needed?

Post by boiler » 05 Jun 2023, 01:35

braveknightrs wrote: The classes were just skeletons to see if I had done something wrong structuring it as to not clutter my post,
Yes, I know, but like I said, they didn’t contain anything, so they didn’t indicate whether it was working unless you add a MsgBox or something in each one.

Post Reply

Return to “Ask for Help (v1)”