AutoHotkey Community

It is currently May 27th, 2012, 12:12 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 9 posts ] 
Author Message
PostPosted: December 30th, 2005, 6:07 pm 
Offline

Joined: December 30th, 2005, 5:01 pm
Posts: 448
What's the reason that the associated variables of the GUI controls, have to be global? :?
Will local variables maybe possible in some next version?

The less global variables, the better it seems to me.

Thanks


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 31st, 2005, 1:05 am 
Offline

Joined: March 2nd, 2004, 3:36 pm
Posts: 10720
Part of the reason is that GUI windows and controls are global in the sense that any part of the script (function or otherwise) can operate upon them with the Gui commands. Furthermore, a function's local variables cease to exist when the function returns to its caller, which would leave a Gui window's controls pointing to nonexistent variables. This in turn would lead to complications when "Gui Submit" and similar commands are used.

At the time local variables were designed, I didn't see any way to make them work with Gui without a lot of extra code and complexity, and maybe not even then.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 25th, 2006, 1:07 am 
Offline

Joined: December 30th, 2005, 5:01 pm
Posts: 448
I discovered some solution for using local variables in Gui controls.
So this avoids Gui variables from the Include script to interfere with the main script.
Unfortunately, I still have to use GuiControlGet. "Gui, submit" presumes to find the variable in the global section of the script.
[edit] AHK v1.0.46.01+ can use Static variables for Gui as well.
Code:
   TestGui()
Return
TestGui()
{
   ;function script...
   SetTimer, MakeGui , 10   ;allows to use non global variables for Gui
   Return
;------------------------LABELS-------------------------------
MakeGui:
   SetTimer, MakeGui , off
   Gui, add, edit, vField1, test1  ;no global variables!
   Gui, add, edit, vField2, test2
   Gui, add, button, ,ControlGetVar
   Gui, show
Return
ButtonControlGetVar:
   GuiControlGet, Field1  ;this works (also without Static variables)
   GuiControlGet, Field2
   MsgBox, %Field1% - %Field2%
Return


Last edited by Peter on June 5th, 2007, 6:10 pm, edited 1 time in total.

Report this post
Top
 Profile  
Reply with quote  
PostPosted: June 5th, 2007, 3:53 pm 
Offline

Joined: May 11th, 2007, 1:08 pm
Posts: 26
I have a GUI, one of its button trigger a function, but the variables submitted in the GUI part cease to exist after I call the function. As far I as I understood the Manual, GUI variables are global, right? So I have no clue what is my fault.

Code:
Gui, Add, Button, x126 y193 w80 h30 , &Cancel
Gui, Add, Button, x226 y193 w80 h30 , &SLEEP
Gui, Add, Text, x6 y23 w150 h20 vTIME, Time
Gui, Add, Edit, x166 y23 w140 h20 vmuteTimer, 0650
Gui, Add, CheckBox, x6 y83 w250 h20 vTORRENT_KI, P2P shall run?
Gui, Add, CheckBox, x6 y113 w250 h20 vSLSK_KI, Slsk shall run?
Gui, Add, CheckBox, x6 y143 w250 h20 vMIRANDA_ON, IM status?
Gui, Show, x338 y221 h239 w328, Sunrise
Return

ButtonCancel:
GuiClose:
ExitApp

ButtonSLEEP:
Gui, submit
MsgBox,%muteTimer%`n%TORRENT_KI%`n%SLSK_KI%`n%MIRANDA_ON%
Main()
ExitApp

Main()
{
   MsgBox,%muteTimer%`n%TORRENT_KI%`n%SLSK_KI%`n%MIRANDA_ON%
[...]


I check the existence of my variables with the two MsgBox, and the first one is OK, the second is totally empty, all the variables are blank.

_________________
dA Profile


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 5th, 2007, 4:15 pm 
Offline

Joined: May 11th, 2007, 1:08 pm
Posts: 26
I pass these values as parameters, but I still don't unerstand this. If they are global, I dont have to pass them, right?

_________________
dA Profile


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 5th, 2007, 4:32 pm 
Avalon wrote:
If they are global, I dont have to pass them, right?
If they are global, you have to declare 'global' at the start of the function to access them... Read up on the functions in the manual.


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: June 5th, 2007, 6:25 pm 
Offline

Joined: December 30th, 2005, 5:01 pm
Posts: 448
Helpy is right.
BTW, your problem was not really related to my problem.
I had a problem of creating a Gui inside a function which is now possible in AHK v1.0.46.01+ without any tricks by the use of Static variables.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 5th, 2007, 7:33 pm 
Offline

Joined: May 11th, 2007, 1:08 pm
Posts: 26
Peter wrote:
Helpy is right.
BTW, your problem was not really related to my problem.
I had a problem of creating a Gui inside a function which is now possible in AHK v1.0.46.01+ without any tricks by the use of Static variables.

I know, and now I see what was my error.
I posted my problem here because I had thought it's unnecessary to create a whole new topic for this question, and the title was just okay for my problem.
Anyway, thanks Helpy, I read through again the manual, and it's clear now. Shame on me I missed this. :(

_________________
dA Profile


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 6th, 2007, 10:56 am 
No problems. The scope of variables in function is a delicate topic in AutoHotkey, and it might seem unatural to non PHP programmers...


Report this post
Top
  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Google Feedfetcher, Yahoo [Bot] and 15 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group