Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Help me use global variables



  • Please log in to reply
7 replies to this topic
Chunjee
  • Members
  • 57 posts
  • Last active: Jan 13 2015 09:18 PM
  • Joined: 30 Nov 2012

Help me use global variables because apparently I can't make it work.

 

 

Edit: Solution!

If its not clear from the thread. The solution is that global must be specified at the start of each function to give it access to those variables.

 

 

Here is my script. It has two messageboxes that confirm the "global" variable is not working in my 2nd function, but the local one is. The second messagebox shows that the 3rd function cannot reach the "global" or 2nd function variables.
http://pastebin.com/Et7Dc2L5

When I add a global next to each variable at the top I get this error, which seems to be telling me I am not declaring these correctly.
918705.jpeg

raw script:

;~~~~~~~~~~~~~~~~~~~~~
;Global Variables and Settings
;~~~~~~~~~~~~~~~~~~~~~
; These are needed in more then one function, or I want to change them in one function and have that value in another function
SetDefaults(void)
{
global
upper_left_X := 111
upper_left_Y := 325
lower_right_X := 813
lower_right_Y := 884
x := 3
return
}

;~~~~~~~~~~~~~~~~~~~~~
;IconScan
;~~~~~~~~~~~~~~~~~~~~~
IconScan(Resource)
{

{
If (Resource = "GBR")
{
Color1 := 0xFF0000 ;Green
Color2 := 0x00FF00 ;Blue
Color3 := 0x0000FF ;Red
} else if (Resource = "Stash")
{
Color1 := 0x57567C ;purple1
Color2 := 0x696998 ;purple2
Color3 := 0x62E6E5 ;purple3
} else if (Resource = "Spice")
{
;Color1 := 0x57567C ;purple1
;Color2 := 0x696998 ;purple2
;Color3 := 0x696998 ;purple3
} else if (Resource = "Vegetable")
{
;Color1 := 0x57567C ;purple1
;Color2 := 0x696998 ;purple2
;Color3 := 0x696998 ;purple3
} else
{
Gosub, Skipclick1
}

}

Msgbox, 1The global function is %upper_left_X%, the other one I dont need is %Color3%
PixelSearch, IconVar1_X, IconVar1_Y, %upper_left_X%, %upper_left_Y%, %lower_right_X%, %lower_right_Y%, %Color1%, Variation := 0, Fast
if ErrorLevel ; If not found, skip clicking
Goto, Skipclick1
else
X_1 := IconVar1_X
Y_1 := IconVar1_Y

PixelSearch, IconVar2_X, IconVar2_Y, %upper_left_X%, %upper_left_Y%, %lower_right_X%, %lower_right_Y%, %Color2%, Variation := 0, Fast
if ErrorLevel ; If not found, skip clicking
Goto, Skipclick1
else
X_2 := IconVar2_X
Y_2 := IconVar2_Y

PixelSearch, IconVar3_X, IconVar3_Y, %upper_left_X%, %upper_left_Y%, %lower_right_X%, %lower_right_Y%, %Color3%, Variation := 0, Fast
if ErrorLevel ; If not found, skip clicking
Goto, Skipclick1
else
X_3 := IconVar3_X
Y_3 := IconVar3_Y


;Add found pixels up and get average

AverageVarX := X_1 + X_2 + X_3
AverageVarY := Y_1 + Y_2 + Y_3
AverageVarX := AverageVarX / 3
AverageVarY := AverageVarY / 3


;Move mouse to averaged location

Mousemove, %AverageVarX%, %AverageVarY%


Skipclick1: ;Skip to here if you cant find anything
return
}

;~~~~~~~~~~~~~~~~~~~~~
;Random 3rd function
;~~~~~~~~~~~~~~~~~~~~~
;Just curious if this will pull variables from the 2nd function
KickAssFunction(void)
{
Msgbox, 2The global function is %upper_left_X%, the other one I dont need is %Color3%
return
}
;~~~~~~~~~~~~~~~~~~~~~
;MAIN PROGRAM STARTS HERE
;~~~~~~~~~~~~~~~~~~~~~

PGDN::
{
SetDefaults(0)
}
{
IconScan("Stash")
}
{
KickAssFunction(0)
}
return


END::Pause
return


TLM
  • Administrators
  • 3864 posts
  • Last active:
  • Joined: 21 Aug 2006

If your using AHK_L you can delcare globals with unlimited scope or Super-Globals like this

global var1 := 1, var2 := 2, var3 := 3
 
func()
 
func() {
    msgbox % var1 " " var2 " " var3
}


Posted Image

don't duplicate, iterate!


OnlyHuman
  • Members
  • 284 posts
  • Last active: Aug 22 2015 10:11 PM
  • Joined: 18 Feb 2011
I tried, AFAIK, every global example in the docs to get those variables to pass around to each function. I couldn't but, I did set up the functions to use ByRef. This allows the functions to get the same content in memory (variable) when you pass it through the functions params. This code gets the variables from SetDefaults(). It does not pass the color though. I hope this helps get you started Posted Image

;~~~~~~~~~~~~~~~~~~~~~
;MAIN PROGRAM STARTS HERE
;~~~~~~~~~~~~~~~~~~~~~
PGDN::
SetDefaults(upper_left_X, upper_left_Y, lower_right_X, lower_right_Y, x)
IconScan(Resource, upper_left_X, upper_left_Y, lower_right_X, lower_right_Y, x)
KickAssFunction(upper_left_X, upper_left_Y, lower_right_X, lower_right_Y, x)
return
;~~~~~~~~~~~~~~~~~~~~~
;Global Variables and Settings
;~~~~~~~~~~~~~~~~~~~~~
; These are needed in more then one function, or I want to change them in one function and have that value in another function
SetDefaults(ByRef upper_left_X, ByRef upper_left_Y, ByRef lower_right_X, ByRef lower_right_Y, ByRef x)
{
upper_left_X := 111
upper_left_Y := 325
lower_right_X := 813
lower_right_Y := 884
x := 3
return
}
;~~~~~~~~~~~~~~~~~~~~~
;IconScan
;~~~~~~~~~~~~~~~~~~~~~
IconScan(Resource, ByRef upper_left_X, ByRef upper_left_Y, ByRef lower_right_X, ByRef lower_right_Y, ByRef x)
{
Resource := "GBR"

If (Resource = "GBR")
{
Color1 := 0xFF0000 ;Green
Color2 := 0x00FF00 ;Blue
Color3 := 0x0000FF ;Red
} else if (Resource = "Stash")
{
Color1 := 0x57567C ;purple1
Color2 := 0x696998 ;purple2
Color3 := 0x62E6E5 ;purple3
} else if (Resource = "Spice")
{
;Color1 := 0x57567C ;purple1
;Color2 := 0x696998 ;purple2
;Color3 := 0x696998 ;purple3
} else if (Resource = "Vegetable")
{
;Color1 := 0x57567C ;purple1
;Color2 := 0x696998 ;purple2
;Color3 := 0x696998 ;purple3
} else
{
Gosub, Skipclick1
}

Msgbox, 1The global function is %upper_left_X%, the other one I don't need is %Color3%
PixelSearch, IconVar1_X, IconVar1_Y, %upper_left_X%, %upper_left_Y%, %lower_right_X%, %lower_right_Y%, %Color1%, Variation := 0, Fast
if ErrorLevel ; If not found, skip clicking
Goto, Skipclick1
else
X_1 := IconVar1_X
Y_1 := IconVar1_Y

PixelSearch, IconVar2_X, IconVar2_Y, %upper_left_X%, %upper_left_Y%, %lower_right_X%, %lower_right_Y%, %Color2%, Variation := 0, Fast
if ErrorLevel ; If not found, skip clicking
Goto, Skipclick1
else
X_2 := IconVar2_X
Y_2 := IconVar2_Y

PixelSearch, IconVar3_X, IconVar3_Y, %upper_left_X%, %upper_left_Y%, %lower_right_X%, %lower_right_Y%, %Color3%, Variation := 0, Fast
if ErrorLevel ; If not found, skip clicking
Goto, Skipclick1
else
X_3 := IconVar3_X
Y_3 := IconVar3_Y


;Add found pixels up and get average

AverageVarX := X_1 + X_2 + X_3
AverageVarY := Y_1 + Y_2 + Y_3
AverageVarX := AverageVarX / 3
AverageVarY := AverageVarY / 3


;Move mouse to averaged location

Mousemove, %AverageVarX%, %AverageVarY%


Skipclick1: ;Skip to here if you can't find anything
return
}

;~~~~~~~~~~~~~~~~~~~~~
;Random 3rd function
;~~~~~~~~~~~~~~~~~~~~~
;Just curious if this will pull variables from the 2nd function
KickAssFunction(ByRef upper_left_X, ByRef upper_left_Y, ByRef lower_right_X, ByRef lower_right_Y, ByRef x)
{
Msgbox, 2The global function is %upper_left_X%, the other one I don't need is %Color3%
return
}

END::
Pause
return

Esc::ExitApp

Docs
I use AHK 1.1.20.03 32-bit ~~ Windows 10 (64 bit) ~~ Firefox


Chunjee
  • Members
  • 57 posts
  • Last active: Jan 13 2015 09:18 PM
  • Joined: 30 Nov 2012
I'm trying both of these when I get a chance.

So far I think AHK_L is throwing a wrench into my errorlevel if statement, but I can access the variables inside the function.



I had the idea to pass all the things I needed globally using arguments, but didn't because I may need to change them on the global level from inside the function. Maybe ByRef will let me do that if it is like a pointer to memory or something.


I don't understand why I can't get it to work like described in the documentation here: http://www.autohotkey.com/docs/Functions.htm#Locals

Chunjee
  • Members
  • 57 posts
  • Last active: Jan 13 2015 09:18 PM
  • Joined: 30 Nov 2012
Reading the documentation over a few times I think I figured it out. I was only declaring the variable and value in SetDefaults(), It needed to be "declared" in the target function as well.


;~~~~~~~~~~~~~~~~~~~~~
;Global Variables
;~~~~~~~~~~~~~~~~~~~~~
SetDefaults(void)
{
;search area
global upper_left_X := 12
global upper_left_Y := 120
global lower_right_X := 1064
global lower_right_Y := 974


}
;~~~~~~~~~~~~~~~~~~~~~
;IconScan
;~~~~~~~~~~~~~~~~~~~~~
IconScan(Resource)
{
;search area
global upper_left_X
global upper_left_Y
global lower_right_X
global lower_right_Y

Msgbox, global lower_right_Y is %global lower_right_Y%	  ; returns 974 correctly.
}


;~~~~~~~~~~~~~~~~~~~~~
;MAIN PROGRAM STARTS HERE
;~~~~~~~~~~~~~~~~~~~~~
PGDN::
SetDefaults("globes")
Loop
{
IconScan("SUPYO")
sleep 1000
}
END::Pause
return


OnlyHuman
  • Members
  • 284 posts
  • Last active: Aug 22 2015 10:11 PM
  • Joined: 18 Feb 2011
That's great how you figured that out. Here is another way that involves both functions.

;~~~~~~~~~~~~~~~~~~~~~
;Global Variables
;~~~~~~~~~~~~~~~~~~~~~
global upper_left_X := 12
global upper_left_Y := 120
global lower_right_X := 1064
global lower_right_Y := 974

;~~~~~~~~~~~~~~~~~~~~~
;SetDefaults
;~~~~~~~~~~~~~~~~~~~~~
SetDefaults(void)
{
MsgBox, Function 1 `nglobal upper_left_Y is %upper_left_Y% ; returns 120 correctly.
}

;~~~~~~~~~~~~~~~~~~~~~
;IconScan
;~~~~~~~~~~~~~~~~~~~~~
IconScan(Resource)
{
;search area
Msgbox, Function 2 `nglobal lower_right_Y is %lower_right_Y% ; returns 974 correctly.
}

;~~~~~~~~~~~~~~~~~~~~~
;MAIN PROGRAM STARTS HERE
;~~~~~~~~~~~~~~~~~~~~~
PGDN::
Loop
{
SetDefaults("globes")
IconScan("SUPYO")
sleep 1000
}
END::Pause
return
This cuts the code needed in half and keeps the variables in one place. I think you said earlier you were just wanting to figure out global variables so, again, I'm just suggesting this way also. Hope you find what your looking for.

EDIT: I just read TLM's post and I agree. This post is what it would like with code.

Docs
I use AHK 1.1.20.03 32-bit ~~ Windows 10 (64 bit) ~~ Firefox


Chunjee
  • Members
  • 57 posts
  • Last active: Jan 13 2015 09:18 PM
  • Joined: 30 Nov 2012

Hey I appreciate that. I'm going a little crazy as I add more features and having to juggle even more variables across different functions.

 

I'm going to try this today. I also noticed that AHK_L seems to be the default download but I don't know when that occurred.



Chunjee
  • Members
  • 57 posts
  • Last active: Jan 13 2015 09:18 PM
  • Joined: 30 Nov 2012
✓  Best Answer

This cuts the code needed in half and keeps the variables in one place. I think you said earlier you were just wanting to figure out global variables so, again, I'm just suggesting this way also. Hope you find what your looking for.


EDIT: I just read TLM's post and I agree. This post is what it would like with code.

 

 

Figured this out a long time ago but just cleaning up the thread in case someone from google stumbles in. 

 

I really don't like declaring gobals outside of a function because it has to be at the very top of the .ahk. Maybe this can be solved with a #include globals.ahk but actually my favorite solution is to use a modified version of Superfraggle's Auto read,load and save an .ini that lets you specify an ini and converts everything in there to global variables.

 

To answer my original question, each function just needs to specify the word global at the beginning and it will have access to all global variables, it doesn't need them written again.

 

 

Here is an example:

CleanParsing(TargetWord,Label,TrimLeft,TrimRight)
{
global

ValueLine := 0


	IfInString, A_LoopReadLine, %TargetWord%
	{
	Linetarget = %Label%
	StringTrimRight, Stringy, A_LoopReadLine, %TrimRight%
	StringTrimLeft, Stringy, Stringy, %TrimLeft%
	ValueLine = 1
	}
}
return

Here global is at the top so any global variable can be accessed, and a variable "ValueLine" is added to the pool of globals.

 

 

Keep in mind that overuse of globals can have drawbacks, but the vast majority of ahk uses should not run into any of these issues.