AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

SetFormat, float - surprises when executed in a subroutine

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Bug Reports
View previous topic :: View next topic  
Author Message
icefire



Joined: 17 Jun 2004
Posts: 4

PostPosted: Thu Jul 01, 2004 5:11 pm    Post subject: SetFormat, float - surprises when executed in a subroutine Reply with quote

My apologies if this turns out not to be a bug but here goes.

I have been using “SetFormat, float” in a script and have found some strange behaviour. I am not sure if it is a bug or something I have missed. But I cannot find anything in the help file to explain it.

The problem is when using SetFormat within a subroutine. The first time the subroutine is performed, through dropping into it (rather than from a tray menu execution), it alters the float format as expected and the alteration also correctly takes effect in a subroutine which did not itself contain a SetFormat statement.

However, when the ChooseDigits subroutine that contains SetFormat is called from the tray menu, it only works within that subroutine and, surprisingly, not within the other subroutine.

Although my original script was fairly involved, I have appended, below, a (persistent) script stripped to the bare minimum to demonstrate the problem.

If you run the script, proceed as follows.
Input an integer to be used within the SetFormat command. Say you choose 3.
A message box will display the value of the system variable A_FormatFloat.
In this example, it will be 03.
Then choose TestMessage from the tray menu.
That will show the value of A_FormatFloat as it appears in the TestMessage subroutine.
In this example, it will be 03.
So far, so good.

Now select ChooseDigits from the tray menu.
Input a different value.
Say you choose 5.
A message box will display the value of the system variable A_FormatFloat.
In this example, it will be 05.
Then choose TestMessage from the tray menu again.
That will show the value of A_FormatFloat as it appears in the TestMessage subroutine.
In this example, I would expect it to be 05.
But in fact, it will be 03, the value set the first time around.

Is that a bug? I cannot understand why A_FormatFloat was set as expected in the TestMessage subroutine the first time (when execution dropped into the ChooseDigits subroutine) but not the second time (when ChooseDigits was called from the tray menu).

If you insert the following line of code as the first line of the TestMessage subroutine:
SetFormat, float, %MyFloatFormat%
the script then behaves as I would expect it to do even though I would not expect that additional line to be necessary.

If you insert the following above the ChooseDigits label
MsgBox, Select ChooseDigits from the tray menu
Exit

it proves that the ChooseDigits subroutine does not alter A-FormatFloat in the TestMessage subroutine at all when ChooseDigits is called from the tray menu. Thus, it only works globally if it is dropped into rather than called from the tray menu.

I am using version 1.0.13 on Windows 98SE.

Here is the script:

#Persistent
Gosub, SetMenu


ChooseDigits:
InputBox, MyDigit, Number of Digits, Enter number of digits for float format: integer between 1 and 9, , 400, 189
AutoTrim, Off
MyFloatFormat = 0%MyDigit%
SetFormat, float, %MyFloatFormat%
MsgBox, ChooseDigits Subroutine`n`nA_FormatFloat = %A_FormatFloat%
MsgBox, Now select "TestMessage" from the tray menu
Return


TestMessage:
MsgBox, TestMessage Subroutine`n`nA_FormatFloat = %A_FormatFloat%`n`n%MyFloatFormat% expected
MsgBox, Now select "ChooseDigits" from the tray menu and input a different number
Return



SetMenu:
Menu, Tray, Add, ChooseDigits
Menu, Tray, Add, TestMessage
Return
Back to top
View user's profile Send private message
Chris
Site Admin


Joined: 02 Mar 2004
Posts: 10471

PostPosted: Thu Jul 01, 2004 7:32 pm    Post subject: Reply with quote

From my test of your script, I believe this behavior is both normal and documented. Here are the rules that apply in this case:

1) The auto-execute section does not end until the first return, exit, or hotkey label is encountered.

2) The default for SetFormat (and many other settings) is put into effect after 100ms if the auto-execute section takes longer than that to execute (which this one does since it falls right into the ChooseDigits subroutine).

3) In this case, because your auto-execute section takes a long time to finish due to the dialog, the chosen default is put into effect when the first return is encountered.

4) Newly launched threads from a hotkey or custom menu item always start out fresh with the defaults as set in the auto-execute section (in this case, the standard default of 0.6 is the global default until the user choses a new one in the InputBox).

It is usually best to have the auto-execute section set the required defaults right at the very top. Then, those defaults will always be in effect for every newly launched thread, whether it be launched by hotkey, menu, or SetTimer.

The other big thing to keep in mind is that changes made to settings such as SetFormat and KeyDelay by one thread will not affect the others. This is done so that you don't have to continually worry about "putting back" the right default value prior to returning from a hotkey or menu subroutine that may have temporarily altered it for its own purposes.

This is discussed in the Scripts section at http://www.autohotkey.com/docs/Scripts.htm -- which I've updated to mention menus and SetFormat. If you find any other omissions or anything that needs clarification, let me know.

Thanks,
Chris
Back to top
View user's profile Send private message Send e-mail
icefire



Joined: 17 Jun 2004
Posts: 4

PostPosted: Thu Jul 01, 2004 11:27 pm    Post subject: Reply with quote

Thank you for the reply. Now I understand it! That is a beautifully clear explanation and somewhat explicitly clearer than the original http://www.autohotkey.com/docs/Scripts.htm section.

I think the very important point that I had not quite grasped is:

Chris wrote:
The other big thing to keep in mind is that changes made to settings such as SetFormat and KeyDelay by one thread will not affect the others.


And I possibly (my fault) missed it because it is explained only in the section on Threads. However, your addition of the reference to custom menu item in the How Scripts are Run and the specific inclusion of the SetFormat there greatly clarifies the original Help.

It might be worth (at the risk of labouring the point) emphasising there that changes to such attributes will therefore only affect subsequent threads if autoexecuted but, if executed within a custom user menu or hotkey, will not affect other threads.

Quote:
It is usually best to have the auto-execute section set the required defaults right at the very top


In the script I wrote, I needed to keep changing SetFormat rather than setting it just once at the top of the autoexecute part. I now realise why the only way to do that is to include a SetFormat command in every thread that needs to use the newly altered format. And the way to do that is clearly to pass the format through the use of a variable such as MyFloatFormat and use it in SetFormat, float, %MyFloatFormat% wherever it is required.

Thanks again!
Back to top
View user's profile Send private message
Chris
Site Admin


Joined: 02 Mar 2004
Posts: 10471

PostPosted: Fri Jul 02, 2004 12:39 am    Post subject: Reply with quote

Thanks for the suggestion. I've added the following to the Scripts page: "Also note that each thread retains its own collection of the above settings. Changes made to those settings will not affect other threads."
Back to top
View user's profile Send private message Send e-mail
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Bug Reports All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group