| View previous topic :: View next topic |
| Author |
Message |
kiu
Joined: 18 Dec 2005 Posts: 229 Location: Italy - Galatro(RC)
|
Posted: Thu Jan 26, 2006 6:02 pm Post subject: toggle function |
|
|
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 |
|
 |
toralf
Joined: 31 Jan 2005 Posts: 3841 Location: Bremen, Germany
|
Posted: Thu Jan 26, 2006 6:49 pm Post subject: |
|
|
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 |
|
 |
kiu
Joined: 18 Dec 2005 Posts: 229 Location: Italy - Galatro(RC)
|
Posted: Thu Jan 26, 2006 7:56 pm Post subject: |
|
|
| 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 |
|
 |
Titan
Joined: 11 Aug 2004 Posts: 5376 Location: /b/
|
Posted: Thu Jan 26, 2006 8:35 pm Post subject: |
|
|
| 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 |
|
 |
kiu
Joined: 18 Dec 2005 Posts: 229 Location: Italy - Galatro(RC)
|
Posted: Thu Jan 26, 2006 9:09 pm Post subject: |
|
|
and this(tested)
| Code: | Loop, 5
MsgBox % "Toggle: " . toggle()
toggle() {
static t
t := !t
Return, t
} |
_________________ ____________________
______________________
kiu |
|
| Back to top |
|
 |
toralf
Joined: 31 Jan 2005 Posts: 3841 Location: Bremen, Germany
|
Posted: Thu Jan 26, 2006 9:22 pm Post subject: |
|
|
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 |
|
 |
kiu
Joined: 18 Dec 2005 Posts: 229 Location: Italy - Galatro(RC)
|
Posted: Thu Jan 26, 2006 10:00 pm Post subject: |
|
|
OTB  _________________ ____________________
______________________
kiu |
|
| Back to top |
|
 |
Titan
Joined: 11 Aug 2004 Posts: 5376 Location: /b/
|
Posted: Thu Jan 26, 2006 11:00 pm Post subject: |
|
|
| kiu wrote: | OTB  |
OTB |
|
| Back to top |
|
 |
kiu
Joined: 18 Dec 2005 Posts: 229 Location: Italy - Galatro(RC)
|
Posted: Thu Jan 26, 2006 11:15 pm Post subject: |
|
|
thanks  _________________ ____________________
______________________
kiu |
|
| Back to top |
|
 |
Guest
|
Posted: Mon Sep 15, 2008 8:07 pm Post subject: |
|
|
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
|
Posted: Wed Sep 17, 2008 12:49 am Post subject: |
|
|
| 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 |
|
 |
Krogdor
Joined: 18 Apr 2008 Posts: 1089 Location: The Interwebs
|
Posted: Wed Sep 17, 2008 12:54 am Post subject: |
|
|
Here's one even shorter:
| Code: | toggle() {
static t
Return, ( t := !t)
} |
_________________ PlayAHK! Try it out  |
|
| Back to top |
|
 |
|