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 

toggle function

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
kiu



Joined: 18 Dec 2005
Posts: 229
Location: Italy - Galatro(RC)

PostPosted: Thu Jan 26, 2006 6:02 pm    Post subject: toggle function Reply with quote

maybe usefull for someone
this function return 1 the first time it is called, 0 the second time, 1 the third time, then 0, and so on.
You can use it in your if statement:
Code:
if(toogle())
 ....
else
 ....


so the script will execute the if block or the else one, depending on the state.

this is the function
Code:

toggle()
   {
      global
      if(firsttoggle="")
         {
            firsttoggle=nomore
            state=1
         }
      state := !state
      return state      
   }


this is an exemple(press "a"):
Code:
a::
msgbox,% toggle()
return

toggle()
   {
      global
      if(firsttoggle="")
         {
            firsttoggle=nomore
            state=1
         }
      state := !state
      return state      
   }

_________________
____________________
______________________
kiu
Back to top
View user's profile Send private message Visit poster's website
toralf



Joined: 31 Jan 2005
Posts: 3841
Location: Bremen, Germany

PostPosted: Thu Jan 26, 2006 6:49 pm    Post subject: Reply with quote

Thanks for sharing.

But I wouldn't set the vars to global. since it is not needed in this case.

I think this is sufficient:
Code:
toggle()
   {
      static state
      return !state       
   }
*not tested*
_________________
Ciao
toralf
Back to top
View user's profile Send private message Send e-mail Visit poster's website
kiu



Joined: 18 Dec 2005
Posts: 229
Location: Italy - Galatro(RC)

PostPosted: Thu Jan 26, 2006 7:56 pm    Post subject: Reply with quote

toralf wrote:

I wouldn't set the vars to global. since it is not needed in this case.

yes,i know, but for some reason without global the function doesn't work

Quote:
I think this is sufficient:
Code:
toggle()
   {
      static state
      return !state       
   }
*not tested*

you must initialize the variable somewhere in the script(not with my function).

Anyway, tested your code: it doesn't work

my regards to your Auto-Syntax-Tid,it's very helpful
_________________
____________________
______________________
kiu
Back to top
View user's profile Send private message Visit poster's website
Titan



Joined: 11 Aug 2004
Posts: 5376
Location: /b/

PostPosted: Thu Jan 26, 2006 8:35 pm    Post subject: Reply with quote

kiu wrote:
Anyway, tested your code: it doesn't work

Try this (tested):
Code:
Loop, 5
   MsgBox % "Toggle: " . toggle()

toggle() {
   static t
   If t
      t := false
   Else
      t := true
   Return, % t
}
Back to top
View user's profile Send private message Visit poster's website
kiu



Joined: 18 Dec 2005
Posts: 229
Location: Italy - Galatro(RC)

PostPosted: Thu Jan 26, 2006 9:09 pm    Post subject: Reply with quote

and this(tested)

Code:
Loop, 5
 MsgBox % "Toggle: " . toggle()

toggle() {
 static t
 t := !t
 Return, t
}

_________________
____________________
______________________
kiu
Back to top
View user's profile Send private message Visit poster's website
toralf



Joined: 31 Jan 2005
Posts: 3841
Location: Bremen, Germany

PostPosted: Thu Jan 26, 2006 9:22 pm    Post subject: Reply with quote

Now this is a nice solution. Well done.

Thanks for the regards, but it will take some time before it will support OTB as in your example.
_________________
Ciao
toralf
Back to top
View user's profile Send private message Send e-mail Visit poster's website
kiu



Joined: 18 Dec 2005
Posts: 229
Location: Italy - Galatro(RC)

PostPosted: Thu Jan 26, 2006 10:00 pm    Post subject: Reply with quote

OTB Question
_________________
____________________
______________________
kiu
Back to top
View user's profile Send private message Visit poster's website
Titan



Joined: 11 Aug 2004
Posts: 5376
Location: /b/

PostPosted: Thu Jan 26, 2006 11:00 pm    Post subject: Reply with quote

kiu wrote:
OTB Question

Arrow OTB
Back to top
View user's profile Send private message Visit poster's website
kiu



Joined: 18 Dec 2005
Posts: 229
Location: Italy - Galatro(RC)

PostPosted: Thu Jan 26, 2006 11:15 pm    Post subject: Reply with quote

thanks Very Happy
_________________
____________________
______________________
kiu
Back to top
View user's profile Send private message Visit poster's website
Guest






PostPosted: Mon Sep 15, 2008 8:07 pm    Post subject: Reply with quote

Hello, i am trying to figure out how to setup this scrips to actually toggle between 2 different buttons, but it's very esoteric. Please, help.

I had less trouble understanding the concept behind fissile materials.
Back to top
Trikster



Joined: 15 Jul 2007
Posts: 1223
Location: Enterprise, Alabama

PostPosted: Wed Sep 17, 2008 12:49 am    Post subject: Reply with quote

Code:
Gui, Add, Button, w100 h20 vButton1 gButton1, Button1
Gui, Add, Button, w100 h20 +Disabled vButton2 gButton2, Button2
Gui, Show
Return

Button1:
Button2:
x++
GuiControl, % (x & 1) ? "Disable" : "Enable", % (x & 1) ? "Button2" : "Button1"
GuiControl, % (x & 1) ? "Enable" : "Disable", % (x & 1) ? "Button1" : "Button2"


I think that'll work. creds to Laszlo for the x & 1 method.
_________________
ScriptPad | ~dieom | dieom | izwian2k7 | Ian | God
Back to top
View user's profile Send private message
Krogdor



Joined: 18 Apr 2008
Posts: 1089
Location: The Interwebs

PostPosted: Wed Sep 17, 2008 12:54 am    Post subject: Reply with quote

Here's one even shorter:

Code:
toggle() {
 static t
 Return, ( t := !t)
}

_________________
PlayAHK! Try it out Very Happy
Back to top
View user's profile Send private message AIM Address
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions 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