Trouble creating a switch

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
daqbancrackr
Posts: 10
Joined: 14 Jul 2023, 12:05

Trouble creating a switch

Post by daqbancrackr » 27 Mar 2024, 16:37

Code: Select all

switch := 1
MsgBox(switch) ;outputting current switch status at launch

F23::output()
F24::switch()

switch()
{
   if switch = 0
   {
      switch := 1 ;if switch is off, turn it on
   }   
}

output()
{
   if switch = 1
   {
      MsgBox("Yellow") if switch is on, output Yellow
      switch := 0 ;reset switch back to off
   }
   else
   {
      MsgBox("Blue") ;switch is off, output Blue
   }
}
when i run the switch function, i get this error
Error: This variable has not been assigned a value.

Specifically: local mySwitch (same name as a global)

010: }
015: {
▶ 016: If mySwitch == 0
017: {
018: mySwitch := 1
basically, i have a button, and based on another button i want it to change what it does

example: button is called color. when you press it, it'll always output Blue.
when i press my switch button, i want that color button to output Yellow, and then reset the switch so that it will output Blue again. make any sense?
User avatar
mikeyww
Posts: 26972
Joined: 09 Sep 2014, 18:38

Re: Trouble creating a switch

Post by mikeyww » 27 Mar 2024, 17:27

Hello,

I have two suggestions.
  1. Avoid using an existing function name such as "Switch".
  2. See the documentation about global variables and their scope. Assigning a global var within a function requires declaring it as global within the function.

Code: Select all

#Requires AutoHotkey v2.0
swich := True
MsgBox swich

F2::output
F3::switchy

switchy() {
 If !swich
  Global swich := True
}

output() {
 If swich {
  MsgBox 'Yellow'
  Global swich := False
 } Else MsgBox 'Blue'
}
Post Reply

Return to “Ask for Help (v2)”