Global not always global

Get help with using AutoHotkey and its commands and hotkeys
Peabianjay
Posts: 47
Joined: 07 Nov 2015, 22:50

Global not always global

09 Nov 2015, 22:32

I'm using "global" within a function to create a pseudo array of values from file data.

Two of the globals created are: items_Pierce_MC_x, items_Pierce_MC_y....which I can (and do) use outside the function defining them. Displayed with my log() function.
However, when I try to access those same two variables from within another function ( join() )....they don't appear to be defined.
And, finally, two OTHER variables defined at the same time exactly the same say, work just fine in other functions ( e.g. use() )

The following is only part of the code, but hopefully is sufficient to illustrate the problem:

Code: Select all

load_items( )

log( items_Pierce_MC_x, items_Pierce_MC_y )				; this works just fine

join()
exitapp

Join()
{
    log( "Novalues?" items_Pierce_MC_x items_Pierce_MC_y " Oh no " )	; this does not work
}

load_items( )
{
    global													; make all our new variables global
    static fields := [ "Arena", "MC", "Shop1", "Shop2" ]

    local file := FileOpen( "Items.txt", "r" )
    local each, field

    while( !File.AtEOF )
    {
	item := scanword(file)									; trauma, bandage, etc.
	if ( item <> ";" )										; comment line in file
	{
	    for each, field in fields
	    {
		items_%item%_%field%_x := scanword( file )			; load co-ordinates
		items_%item%_%field%_y := scanword( file )
	    }
	}
	file.readline()
    }
    file.close()
}

use( item, location := "Arena" )
{
    click( items_%item%_%location%_x, items_%item%_%location%_y )

    if ( account[item][Account_Active]-- < 1 )
	restock_state := TRUE    
}
User avatar
MasterFocus
Posts: 146
Joined: 01 Oct 2013, 09:47
GitHub: MasterFocus
Location: Rio de Janeiro - RJ - Brasil
Contact:

Re: Global not always global

09 Nov 2015, 23:39

The LoadItems() function understands that it has to check globally for known variables, because you specified it using the "global" keyword.

But poor Join() function is lost, because you directly refer to certain items_Pierce_MC_* variables, which are unkown to the function! :lol:

So, all you have to do is specify the "global" keyword for the Join() function as well.
Antonio França - git.io | github.com | ahk4.net | sites.google.com
Member of the AHK community since 08/Apr/2009. Moderator since mid-2012.
Need help? Please post on the forum before sending me a PM.
Peabianjay
Posts: 47
Joined: 07 Nov 2015, 22:50

Re: Global not always global

09 Nov 2015, 23:53

I expanded "Join()" slightly, and also used "Listvars" to examine stuff....

Code: Select all

join()
{
    for each, item in items
    {
        runlog( items_%item%_MC_x, items_%item%_MC_y )      ; This works for all values of "item" *except* items_Pierce_MC_x and items_Pierce_MC_y. These are defined as local variables by "join()". Why?
        use( item, "MC" )                                                          ; This works for ALL values of "item", including "Pierce"
    }
}
User avatar
MasterFocus
Posts: 146
Joined: 01 Oct 2013, 09:47
GitHub: MasterFocus
Location: Rio de Janeiro - RJ - Brasil
Contact:

Re: Global not always global

09 Nov 2015, 23:59

Still, because you didn't specify the "global" keyword. You only did it for the LoadItems() function.
You're actually using a pseudo-array in assume-global mode: http://www.autohotkey.com/docs/Functions.htm#Locals
Antonio França - git.io | github.com | ahk4.net | sites.google.com
Member of the AHK community since 08/Apr/2009. Moderator since mid-2012.
Need help? Please post on the forum before sending me a PM.
Peabianjay
Posts: 47
Joined: 07 Nov 2015, 22:50

Re: Global not always global

10 Nov 2015, 00:23

Ya...but...um....

It works for ALL the other variables (pseudo-array elements) defined and used exactly the same way.
It even works for THOSE two variables in a different function.

Thus, my befuddlement. Same define, same code, same file, different results.

From Listvars:

Local Variables for Join()
--------------------------------------------------
item[5 of 7]: Molly
items_Pierce_MC_x[0 of 0]: <<< See? ONLY these two are ignored as globals by join(), and ONLY in this function
items_Pierce_MC_y[0 of 0]: <<< See? ONLY these two are ignored as globals by join(), and ONLY in this function
location[2 of 3]: MC

Global Variables (alphabetical)
--------------------------------------------------
...
items_Pierce_Arena_x[2 of 3]: 31
items_Pierce_Arena_y[3 of 3]: 410
items_Pierce_MC_x[2 of 3]: 31
items_Pierce_MC_y[3 of 3]: 485
items_Pierce_Shop1_x[3 of 3]: 575
items_Pierce_Shop1_y[3 of 3]: 329
....
Peabianjay
Posts: 47
Joined: 07 Nov 2015, 22:50

Re: Global not always global

10 Nov 2015, 00:47

Problem solved:

[Edit: Deleted this solution, 'cuz it wasn't, actually.]

Now it works.
Last edited by Peabianjay on 10 Nov 2015, 01:34, edited 1 time in total.
guest3456
Posts: 3123
Joined: 09 Oct 2013, 10:31

Re: Global not always global

10 Nov 2015, 00:48

from the help file:
http://autohotkey.com/docs/Functions.htm wrote:
Within a function, any dynamic variable reference such as Array%i% always resolves to a local variable unless no variable of that name exists, in which case a global is used if it exists. If neither exists and the usage requires the variable to be created, it is created as a local variable unless the assume-global mode is in effect. Consequently, a function can create a global array manually (by means such as Array%i% := A_Index) only if it has been defined as an assume-global function.

Common source of confusion: Any non-dynamic reference to a variable creates that variable the moment the script launches. For example, when used outside a function, MsgBox %Array1% creates Array1 as a global the moment the script launches. Conversely, when used inside a function MsgBox %Array1% creates Array1 as one of the function's locals the moment the script launches (unless assume-global is in effect), even if Array and Array0 are declared global.

Peabianjay
Posts: 47
Joined: 07 Nov 2015, 22:50

Re: Global not always global

10 Nov 2015, 01:33

Methinks I finally understand!!

MasterFocus did say, "...because you directly refer...."
And guest3456 clarified the documentation, "... Any non-dynamic reference...."

(I had a direct reference later in the function, so all my tweaking/testing wasn't affecting the already-defined-as-local variable.)

Thanks to both of you!
guest3456
Posts: 3123
Joined: 09 Oct 2013, 10:31

Re: Global not always global

10 Nov 2015, 01:33

i figured you hadn't posted your full code..

either post the full code next time, or shorten it to a working example which fails

Peabianjay
Posts: 47
Joined: 07 Nov 2015, 22:50

Re: Global not always global

10 Nov 2015, 01:35

Ya. I tried (and failed) to keep the relevant bits. The actual code is far too long to post here. Thanks again.

Return to “Ask For Help”

Who is online

Users browsing this forum: anhnha, littlegandhi1199, RubbeH, sobuj53, VibinonAHk, Xeo786 and 30 guests