Code: Select all
; Top of script
SetTimer, DoDeclareGlobals
DoDeclareGlobals
{
global var1
global var2
global var3
...
SetTimer, DoDeclareGlobals, Off
}
Any idea what I am doing wrong?
Code: Select all
; Top of script
SetTimer, DoDeclareGlobals
DoDeclareGlobals
{
global var1
global var2
global var3
...
SetTimer, DoDeclareGlobals, Off
}
Code: Select all
DoDeclareGlobals()
MyFunc()
return
DoDeclareGlobals()
{
global varr := "hi"
}
MyFunc()
{
global varr
MsgBox, %varr%
}
Code: Select all
DoDeclareGlobals
{
global var1
global var2
global var3
...
SetTimer, DoDeclareGlobals, Off
}
Code: Select all
global var1 := "hello" ; prefixing a var with global, while already in global scope makes var super-global
MyFunc()
return
MyFunc(){
msgbox % var1
}
Code: Select all
RunTimeCheck()
{
TimeCheck := GetTimeSinceSold()
if (TimeCheck > 45)
{
Gui, TimeCheckWindow: New
Gui, -SysMenu
Gui, Add, Text,, % "Item has not sold in " . TimeCheck . " days; continue?"
Gui, Add, Button, gButtonClose Default, No
fn := Func("IgnoreTime").Bind(PromoType)
Gui, Add, Button, vTempVar1 X40 Y25, Yes
GuiControl, +g, TempVar1, % fn
Gui, Show
}
else
PromptPromo(PromoType)
return
}
Code: Select all
#SingleInstance force
RunTimeCheck()
RunTimeCheck()
{
TimeCheck := GetTimeSinceSold()
if (TimeCheck > 45)
{
Gui, New, hwndTimeCheckWindow
Gui, -SysMenu
Gui, Add, Text,, % "Item has not sold in " . TimeCheck . " days; continue?"
Gui, Add, Button, gButtonClose Default, No
fn := Func("IgnoreTime").Bind(PromoType)
Gui, Add, Button, HwndTempVar1 X40 Y25, Yes
GuiControl, +g, % TempVar1 , % fn
Gui, Show
}
else
PromptPromo(PromoType)
return
}
GetTimeSinceSold(){
return 50
}
PromptPromo(blah := ""){
}
IgnoreTime(blah := ""){
ToolTip % A_TickCount ; Show tooltip to prove function is being called
}
ButtonClose:
return
Code: Select all
#SingleInstance force
RunTimeCheck()
RunTimeCheck()
{
static TimeCheckWindow := 0
TimeCheck := GetTimeSinceSold()
if (TimeCheck > 45)
{
if (!TimeCheckWindow)
Gui, New, hwndTimeCheckWindow
Gui, -SysMenu
Gui, Add, Text,, % "Item has not sold in " . TimeCheck . " days; continue?"
Gui, Add, Button, gButtonClose Default, No
fn := Func("IgnoreTime").Bind(PromoType)
Gui, Add, Button, HwndTempVar1 X40 Y25, Yes
GuiControl, +g, % TempVar1 , % fn
Gui, Show
}
else
PromptPromo(PromoType)
return
}
GetTimeSinceSold(){
return 50
}
PromptPromo(blah := ""){
}
IgnoreTime(blah := ""){
ToolTip % A_TickCount ; Show tooltip to prove function is being called
}
ButtonClose:
return
Code: Select all
#SingleInstance force
RunTimeCheck()
RunTimeCheck()
{
static TimeCheckWindow := 0
TimeCheck := GetTimeSinceSold()
if (TimeCheck > 45)
{
if (!TimeCheckWindow)
{
Gui, New, hwndTimeCheckWindow
Gui, -SysMenu
Gui, Add, Text, hwndTimeSince w200,
Gui, Add, Button, gButtonClose Default, No
fn := Func("IgnoreTime").Bind(PromoType)
Gui, Add, Button, HwndTempVar1 X40 Y25, Yes
GuiControl, +g, % TempVar1 , % fn
}
GuiControl, , % TimeSince, % "Item has not sold in " TimeCheck " days; continue?"
Gui, % TimeCheckWindow ":Show"
}
else
PromptPromo(PromoType)
return
}
GetTimeSinceSold(){
return 50
}
PromptPromo(blah := ""){
}
IgnoreTime(blah := ""){
ToolTip % A_TickCount ; Show tooltip to prove function is being called
}
ButtonClose:
return
Code: Select all
#SingleInstance force
MyEdit1 := new CEdit(Func("EditChanged"), "w200", "Blah")
Gui, Add, Button, gTest, Click Me
Gui, Show
return
Test:
MyEdit1.Set("Hello")
return
EditChanged(value){
Tooltip % Value
}
GuiClose:
ExitApp
; CEdit Class - place in an #include or something.
Class CEdit {
__New(callback, options := "", text := ""){
this.callback := callback
Gui, Add, Edit, % options " hwndhwnd", % text
this.hwnd := hwnd
fn := this.ValueChanged.Bind(this)
GuiControl, +g, % this.hwnd, % fn
}
Get(){
GuiControlGet, out, , % this.hwnd
return out
}
Set(value){
GuiControl, , % this.hwnd, % value
}
ValueChanged(){
this.callback.call(this.Get())
}
}
Code: Select all
#SingleInstance force
MyEdit1 := new CEdit(Func("EditChanged").Bind("Edit 1"), "w200", "Type something")
MyEdit2 := new CEdit(Func("EditChanged").Bind("Edit 2"), "w200", "Anything")
Gui, Add, Button, gTest, Click Me
Gui, Show
return
Test:
MyEdit1.Set("Hello")
return
EditChanged(name, value){
Tooltip % Name " changed to: " Value
}
Why a "small" set? There is no reason that you would need more variables if they were not "super", only more declarations for the same variables (unless you use the assume-global mode, in which case you don't need any declarations).MaxAstro wrote:I basically decided to reuse a small set of super-global variables rather than declaring global variables inside each function with a GUI, mostly for readability reasons.
There is a difference between a definition and a declaration. global var1 is just a declaration, var1 := 1 is an assignment which could be called a definition, and global var1 := 1 is both. It is only the declaration that needs to be either outside a function or inside every function which needs it. If the declaration is outside a function, the assignment can be anywhere (subject to control flow).Honestly it annoys me a little bit that global variables defined inside a function are not super-global...
Code: Select all
; Top of script
DoInitializeGlobals()
return ; End of auto-execute section.
;... lots of other code...
MsgBox, This line is never executed.
global var1, var2, var3 ; This line takes effect despite never being executed.
DoInitializeGlobals()
{
var1 := 42
;... initialize variables as needed ...
}
Correct. What it does take is more lines of code - I can declare a variable once as superglobal, or five or six times as regular global. It also runs the risk of me forgetting to import a variable. It probably would actually result in more variables overall, actually, because I'd probably end up using a different global in each function with a name appropriate to its use in that function, instead of the TempVar1/TempVar2/TempVar3 thing. Which I guess might be smarter anyway, but... xDlexikos wrote:Why a "small" set? There is no reason that you would need more variables if they were not "super", only more declarations for the same variables (unless you use the assume-global mode, in which case you don't need any declarations).
That's all helpful to know. At the very least, it helps me understand why you can't declare a super-global inside a function.lexikos wrote:There is a difference between a definition and a declaration. global var1 is just a declaration, var1 := 1 is an assignment which could be called a definition, and global var1 := 1 is both. It is only the declaration that needs to be either outside a function or inside every function which needs it. If the declaration is outside a function, the assignment can be anywhere (subject to control flow).
Declarations are not executed; they are read by the pre-parser (aka "compiler") before the script starts. As a result, they are not subject to control flow - i.e. they can go anywhere outside a function, even in a place that a command would never execute.
Another way to think of it is that the declaration(s) and the variable are separate things. The declaration(s) controls where that particular name resolves to that particular variable. A declaration inside function X is only visible inside function X, so even though the variable exists globally, function Y will not access it by default. By contrast, a declaration outside any function is visible to all functions.
I understand that. As I said initially, the only reason I want to put it in a function is because Notepad++ allows me to collapse functions, so I wouldn't have to scroll past all my globals when browsing my script. Obviously that isn't possible, so I'll just live with the minor inconvenience.lexikos wrote:By the way, a function which consists solely of lines like global var1 and global var2 without any assignment won't have any effect when it is called, but its mere existence causes the variables to be created when the script starts. If your function only contains declarations, there really is no need for the function at all. (I guess your actual function contains some assignments, unlike what you showed in your first post.)
that seems like a poor reason, but depending on your NP++ collapse feature, you could just wrap the globals in a redundant {block}:MaxAstro wrote: As I said initially, the only reason I want to put it in a function is because Notepad++ allows me to collapse functions, so I wouldn't have to scroll past all my globals when browsing my script. Obviously that isn't possible, so I'll just live with the minor inconvenience.
Code: Select all
{ ; unnecessary but used for editor highlighting/collapse
global varr := "hi"
}
MyFunc()
return
MyFunc()
{
MsgBox, %varr%
}
yes it does. did you try my code?MaxAstro wrote:Oddly, putting it in a redundant block ALSO causes the variables to not be super-global.
editors are wildly personal and will open up a can of worms. but its the one program that you will use for the rest of your computing lifetime, so you might as well find one that is extremely customizable to all of your needs. as you bump into limitations as you have, you either want to look up how you can solve them in your current editor, or look for a new editorMaxAstro wrote: Can you recommend a good editor? I've used NPP for a while, but a couple things about it do bother me - one is that the way themes are set up means I can't use themes while preserving AHK-appropriate color coding, and another is that it is extremely aggressive when it comes to autocomplete (it will try to autocomplete variable names using option strings or silly things like that... for that matter, it tries to autocomplete my comments as variable names...).
Users browsing this forum: No registered users and 151 guests