GuiControl Line not running when run from an If statement

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
SpanishMoss00
Posts: 16
Joined: 08 Apr 2021, 10:09

GuiControl Line not running when run from an If statement

Post by SpanishMoss00 » 17 Jun 2021, 23:01

This code is the beginnings of a game I am making, however, when I run the code so far, the GuiControl Statements within the If statements are not being run, and thus the Gui is not being updated. I have no idea why this is occurring, I have set a var for the text I want changed, and I have specified the Gui window I want to be selected, but it doesn't work

Code: Select all

#SingleInstance Force

Gui, Add, Text, x94 y26 w192 h19 vMainText +Center, Are you ready?
Gui, Add, Text, x21 y74 w96 h28 vStarting, Starting in 3...
Gui, Show, w385 h168, ReactionTime 
Gui, Add, Button, , Exit Game
sleep 1000
GuiControl, , Starting, Starting in 2....				;counting down in the start screen
sleep 1000
GuiControl, , Starting, Starting in 1...
Sleep 1000
GuiControl, Hide, Starting
GuiControl, Hide, MainText
Gui, New,
Gui, Add, Picture, x-55 y-50 w1132 h633 ,		 ; image file goes here
Gui, Add, Button, x40 y45 w144 h67, RED BUTTON
Gui, Show, w236 h159, Blue
Gui, New
Gui, Add, Picture, x-55 y-50 w1132 h633, 			; image file goes here
Gui, Add, Button, x40 y45 w144 h67, BLUE BUTTON
Gui, Show, w236 h159, Red
WinMove Red, , 0, 0
WinMove Blue, , 0, 800
Random, QuestionType, 1, 4	
MsgBox, random got the value %QuestionType%							; debugging to see what random got
sleep 1000	
If (QuestionType = 1)	
{
	GuiControl, ReactionTime:Text, MainText, Click the Button Labeled Red Button 	 		; Labled Red Button --This statement and the other similar statements below are not being run
}
If (QuestionType = 2)
{
	GuiControl, ReactionTime:Text, MainText, Click the Button in the Red Box								; Button in the red box
}
If (QuestionType = 3)
{
	GuiControl, ReactionTime:Text, MainText, Click the Button Labled Blue Button						; Labled Blue Button
}
If (QuestionType = 4)
{
	GuiControl, ReactionTime:Text, MainText, Click the Button in the Blue Box							; Button in the blue box
}
return

ButtonExitGame:
{
ExitApp
}

GuiClose:
ExitApp

F7:: 
ExitApp

F8::
Reload
Thanks for any and all help
-SpanishMoss

User avatar
Hellbent
Posts: 2102
Joined: 23 Sep 2017, 13:34

Re: GuiControl Line not running when run from an If statement

Post by Hellbent » 18 Jun 2021, 00:12

You are trying to use the windows title as its name

Code: Select all

Gui, Show, w385 h168, ReactionTime   ; <<<< --------- ReactionTime    is the title not the name

Code: Select all

Gui, Name: Add, Checkbox, vCH, Press Numpad1
Gui, Name: Show, w200 h200, ReactionTime   
return

Numpad1::
	if(Tog:=!Tog)
		GuiControl, Name:, CH, 1
	else
		GuiControl, Name:, CH, 0
	return
HTH

SpanishMoss00
Posts: 16
Joined: 08 Apr 2021, 10:09

Re: GuiControl Line not running when run from an If statement

Post by SpanishMoss00 » 18 Jun 2021, 09:25

Thanks for responding, I am still confused as to what the "name" of the GUI is, and/or how to set the name. I also do not understand what the second set of code you posted is meant to accomplish.
Thanks for understanding.

-SpanishMoss

User avatar
boiler
Posts: 16705
Joined: 21 Dec 2014, 02:44

Re: GuiControl Line not running when run from an If statement

Post by boiler » 18 Jun 2021, 10:17

Hellbent’s example shows how you set the GUI name. He precedes the Gui sub-commands with Name: to name and refer to it as Name.

In your script, you are attempting to refer to your GUI in your GuiControl commands as if it were named ReactionTime (example below). You did not name it that; you gave the GUI window that title in your Gui, Show command. As Hellbent mentioned, the title of a GUI window is not its name, unless you happen to make them the same.

Code: Select all

GuiControl, ReactionTime:Text, MainText, Click the Button Labeled Red Button

The documentation under the section for Gui, New discusses naming GUIs with these types of labels and how using New creates a new unnamed GUI.

SpanishMoss00
Posts: 16
Joined: 08 Apr 2021, 10:09

Re: GuiControl Line not running when run from an If statement

Post by SpanishMoss00 » 18 Jun 2021, 11:40

Thanks, That worked! I Added a new line at the top : Gui, ReactionTime:New a nd that fixed it
Thanks for your help.

-SpanishMoss

SpanishMoss00
Posts: 16
Joined: 08 Apr 2021, 10:09

Re: GuiControl Line not running when run from an If statement

Post by SpanishMoss00 » 18 Jun 2021, 22:53

New Problem, I want to define what the buttons do when pressed, but only when certain values for QuestionType are given. I tried the below code, but when run, the Gui will display the GuiControl change without ever pressing the button. In addition, this won't work for other QuestionType values because you can only define a button this way once. For debugging purposes, QuestionType will always be 1, but keep in mind that there will be 3 other values it can be (see original code).

Code: Select all

Random, QuestionType, 1, 1								; Question type will always be 1 for debugging, but in the real code, it will be 1-4
If (QuestionType = 1)	
{
GuiControl, ReactionTime:, MainText, Click the Button Labeled Red Button 	 		; Click the Button Labeled Red Button ; Labled Red Button
ButtonREDBUTTON:											; it should only execute the following if red button is pressed, but it executes it anyway without the button being pressed
	{
GuiControl, ReactionTime:, MainText, Correct!
return
	}
ButtonBLUEBUTTON:											; this line is never exucted even if the button is pressed
	{
GuiControl, ReactionTime:, MainText, WRONG! Game Over...
return
	}
}
I think there may be a way to use an If statement, But I don't know the syntax for that with buttons.
As a note, the buttons have the variable name RedButton and BlueButton respectively.
Thanks for any and all help
-SpanishMoss00

User avatar
boiler
Posts: 16705
Joined: 21 Dec 2014, 02:44

Re: GuiControl Line not running when run from an If statement

Post by boiler » 18 Jun 2021, 23:24

You say that the code after a label like ButtonRedBUTTON should only be executed when the button is pressed, but then you actually are routing the code flow right through that label. When you execute the GuiControl line before the label, the label doesn’t stop the flow of execution from proceeding past that point, so it executes whatever comes next.

To make what gets executed after a button press conditional based on the value of the QuestionType variable, put your conditional statements and their related code in the lines that follow each of those labels. Something like:

Code: Select all

Random, QuestionType, 1, 1								; Question type will always be 1 for debugging, but in the real code, it will be 1-4
return ; end of auto-execute section (GUI creation and other stuff to be executed when script initially runs also goes above

ButtonREDBUTTON:
If (QuestionType = 1)	
{
	MsgBox, Red button pressed with Q type 1
	; ...
}
If (QuestionType = 2)
{
	MsgBox, Red button pressed with Q type 2
	; ...
}
; etc.
return

ButtonBLUEBUTTON:
If (QuestionType = 1)	
{
	MsgBox, Blue button pressed with Q type 1
	; ...
}
If (QuestionType = 2)
{
	MsgBox, Blue button pressed with Q type 2
	; ...
}
; etc.
return

SpanishMoss00
Posts: 16
Joined: 08 Apr 2021, 10:09

Re: GuiControl Line not running when run from an If statement

Post by SpanishMoss00 » 19 Jun 2021, 11:31

Thanks! that also worked.
-SpanishMoss00

SpanishMoss00
Posts: 16
Joined: 08 Apr 2021, 10:09

Re: GuiControl Line not running when run from an If statement

Post by SpanishMoss00 » 19 Jun 2021, 23:57

Why won't this section of code Loop? (This is still from the same code btw). If I move the return function outside the }, it errors out, stating a return function is required before the last }.

Code: Select all

#SingleInstance Force
Score:=0
TimeOutCheck:=0
Gui, ReactionTime:New
Gui, Add, Text, x94 y26 w192 h19 vMainText +Center, Are you ready?
Gui, Add, Text, x21 y74 w96 h28 vStarting, Starting in 3...
Gui, Add, Text, x250 y74 w80 vScoreCount, Score : %Score%
Gui, Show, w385 h168, ReactionTime 
sleep 1000
GuiControl, ReactionTime:, Starting, Starting in 2....				;counting down in the start screen
sleep 1000
GuiControl, ReactionTime:, Starting, Starting in 1...
Sleep 1000
GuiControl, Hide, Starting
Gui, New
Gui, Color, Blue
Gui, Add, Button, x40 y45 w144 h67 vRedButton, RED BUTTON
Gui, Show, w236 h159, Blue
Gui, New
Gui, Color, Red
Gui, Add, Button, x40 y45 w144 h67 vBlueButton, BLUE BUTTON
Gui, Show, w236 h159, Red
Loop																		; start of the loop
{
	sleep 1000
	TimeOutCheck:=0
	Random, Redx, 0, 1690
	Random, Redy, 0, 800
	Random, Bluex, 0, 1690
	Random, Bluey, 0, 800
	Random, RTx, 0, 1400
	Random, RTy, 0, 800
	WinMove, ReactionTime, , %RTx%, %RTy%
	WinMove Red, , %Redx%, %Redy%
	WinMove Blue, , %Bluex%, %Bluey%
	Random, QuestionType, 1, 4
	;MsgBox, random got %QuestionType% `nKey: `n1 = red button `n2 = blue button `n3 = blue button `n4 = red button ; debugging box to see what random got
	If (QuestionType = 1)
	{
		GuiControl, ReactionTime:, MainText, Click the Button Labeled Red Button						; Labled Red Button
	}
	If (QuestionType = 2)
	{
		GuiControl, ReactionTime:, MainText, Click the Button in the Red Box								; Button in the red box
	}
	If (QuestionType = 3)
	{
		GuiControl, ReactionTime:, MainText, Click the Button Labled Blue Button						; Labled Blue Button
	}
	If (QuestionType = 4)
	{	
		GuiControl, ReactionTime:, MainText, Click the Button in the Blue Box							; Button in the blue box
	}	
	sleep 5000
	If (TimeOutCheck = 0)			; Checking to see if var TimeOutCheck is still 0, if it is, player was too slow and the game ends. If it isn't the following is not run
		{
			GuiControl, ReactionTime:, MainText, Too SLOW! Game Over...
			Gui, Red:Submit, Destory
			Gui, Blue:Submit, Destroy
			sleep 2000
			;run file path goes here
			ExitApp
		}
	return

	ButtonREDBUTTON:
	{
		If (QuestionType = 1)
		{
			TimeOutCheck:=1
			GuiControl, ReactionTime:, MainText, Correct!
			GuiControl, ReactionTime:, ScoreCount, % "Score : " Score+=1
		}
		If (QuestionType = 2)
		{
			TimeOutCheck:=1
			GuiControl, ReactionTime:, MainText, WRONG! Game Over...
			Gui, Red:Submit, Destory
			Gui, Blue:Submit, Destroy
			sleep 2000
			;run file path goes here
			ExitApp
		}
		If (QuestionType = 3)
		{
			TimeOutCheck:=1
			GuiControl, ReactionTime:, MainText, WRONG! Game Over...
			Gui, Red:Submit, Destory
			Gui, Blue:Submit, Destroy
			sleep 2000
			;run file path goes here
			ExitApp
		}
		If (QuestionType = 4)
		{
			TimeOutCheck:=1
			GuiControl, ReactionTime:, MainText, Correct!
			GuiControl, ReactionTime:, ScoreCount, % "Score : " Score+=1
		}
	MsgBox, Gets this far
	}
	ButtonBLUEBUTTON:
	{
		If (QuestionType = 1)
		{
			TimeOutCheck:=1
			GuiControl, ReactionTime:, MainText, WRONG! Game Over...
			Gui, Red:Submit, Destory
			Gui, Blue:Submit, Destroy
			sleep 2000
			;run file path goes here
			ExitApp
		}
		If (QuestionType = 2)
		{
			TimeOutCheck:=1
			GuiControl, ReactionTime:, MainText, Correct!
			GuiControl, ReactionTime:, ScoreCount, % "Score : " Score+=1
		}
		If (QuestionType = 3)
		{
			TimeOutCheck:=1
			GuiControl, ReactionTime:, MainText, Correct!
			GuiControl, ReactionTime:, ScoreCount, % "Score : " Score+=1
		}
		If (QuestionType = 4)
		{
			TimeOutCheck:=1
			GuiControl, ReactionTime:, MainText, WRONG! Game Over...
			Gui, Red:Submit, Destory
			Gui, Blue:Submit, Destroy
			sleep 2000
			;run file path goes here
			ExitApp
		}
	}
return											; It errors out if this return function is not here
}
return
; etc...
It doesn't make any sense, In other scripts, I don't put in a return function before the last } in a loop, but for some reason, it can't run without it??
Thanks for the help

-SpanishMoss00

User avatar
boiler
Posts: 16705
Joined: 21 Dec 2014, 02:44

Re: GuiControl Line not running when run from an If statement

Post by boiler » 20 Jun 2021, 02:40

SpanishMoss00 wrote:
19 Jun 2021, 23:57
It doesn't make any sense, In other scripts, I don't put in a return function before the last } in a loop, but for some reason, it can't run without it??
It does make sense. What doesn't make sense is to put your labeled subroutines inside the loop itself. You went back to having your code flow through the labeled subroutines like I said not to do. The code in subroutines that are to be triggered by GUI button clicks should be stand-alone, below the auto-execute section of your script, not inside a loop that is already executing.

You need to think about how events such as GUI button clicks and hotkey presses get triggered. They shouldn’t be in line of your code flow. They need to be outside of it so that the code execution jumps there when the event occurs, then when it encounters a return statement at the end of the subroutine, it returns to where it was executing before the click event occurred and continues executing where it left off. You are getting errors because you are clicking the buttons while in that loop, and it’s looking for a return for where the labeled subroutine ends. The proper structure is like this:

Code: Select all

#SingleInstance Force
Score:=0
TimeOutCheck:=0
Gui, ReactionTime:New
Gui, Add, Text, x94 y26 w192 h19 vMainText +Center, Are you ready?
Gui, Add, Text, x21 y74 w96 h28 vStarting, Starting in 3...
Gui, Add, Text, x250 y74 w80 vScoreCount, Score : %Score%
Gui, Show, w385 h168, ReactionTime 
sleep 1000
GuiControl, ReactionTime:, Starting, Starting in 2....				;counting down in the start screen
sleep 1000
GuiControl, ReactionTime:, Starting, Starting in 1...
Sleep 1000
GuiControl, Hide, Starting
Gui, New
Gui, Color, Blue
Gui, Add, Button, x40 y45 w144 h67 vRedButton, RED BUTTON
Gui, Show, w236 h159, Blue
Gui, New
Gui, Color, Red
Gui, Add, Button, x40 y45 w144 h67 vBlueButton, BLUE BUTTON
Gui, Show, w236 h159, Red
Loop																		; start of the loop
{
	sleep 1000
	TimeOutCheck:=0
	Random, Redx, 0, 1690
	Random, Redy, 0, 800
	Random, Bluex, 0, 1690
	Random, Bluey, 0, 800
	Random, RTx, 0, 1400
	Random, RTy, 0, 800
	WinMove, ReactionTime, , %RTx%, %RTy%
	WinMove Red, , %Redx%, %Redy%
	WinMove Blue, , %Bluex%, %Bluey%
	Random, QuestionType, 1, 4
	;MsgBox, random got %QuestionType% `nKey: `n1 = red button `n2 = blue button `n3 = blue button `n4 = red button ; debugging box to see what random got
	If (QuestionType = 1)
	{
		GuiControl, ReactionTime:, MainText, Click the Button Labeled Red Button						; Labled Red Button
	}
	If (QuestionType = 2)
	{
		GuiControl, ReactionTime:, MainText, Click the Button in the Red Box								; Button in the red box
	}
	If (QuestionType = 3)
	{
		GuiControl, ReactionTime:, MainText, Click the Button Labled Blue Button						; Labled Blue Button
	}
	If (QuestionType = 4)
	{	
		GuiControl, ReactionTime:, MainText, Click the Button in the Blue Box							; Button in the blue box
	}	
	sleep 5000
	If (TimeOutCheck = 0)			; Checking to see if var TimeOutCheck is still 0, if it is, player was too slow and the game ends. If it isn't the following is not run
	{
		GuiControl, ReactionTime:, MainText, Too SLOW! Game Over...
		Gui, Red:Submit, Destory
		Gui, Blue:Submit, Destroy
		sleep 2000
		;run file path goes here
		ExitApp
	}
}
return ; end of auto-execute section

ButtonREDBUTTON:
	If (QuestionType = 1)
	{
		TimeOutCheck:=1
		GuiControl, ReactionTime:, MainText, Correct!
		GuiControl, ReactionTime:, ScoreCount, % "Score : " Score+=1
	}
	If (QuestionType = 2)
	{
		TimeOutCheck:=1
		GuiControl, ReactionTime:, MainText, WRONG! Game Over...
		Gui, Red:Submit, Destory
		Gui, Blue:Submit, Destroy
		sleep 2000
		;run file path goes here
		ExitApp
	}
	If (QuestionType = 3)
	{
		TimeOutCheck:=1
		GuiControl, ReactionTime:, MainText, WRONG! Game Over...
		Gui, Red:Submit, Destory
		Gui, Blue:Submit, Destroy
		sleep 2000
		;run file path goes here
		ExitApp
	}
	If (QuestionType = 4)
	{
		TimeOutCheck:=1
		GuiControl, ReactionTime:, MainText, Correct!
		GuiControl, ReactionTime:, ScoreCount, % "Score : " Score+=1
	}
return ; end of ButtonREDBUTTON subroutine

ButtonBLUEBUTTON:
	If (QuestionType = 1)
	{
		TimeOutCheck:=1
		GuiControl, ReactionTime:, MainText, WRONG! Game Over...
		Gui, Red:Submit, Destory
		Gui, Blue:Submit, Destroy
		sleep 2000
		;run file path goes here
		ExitApp
	}
	If (QuestionType = 2)
	{
		TimeOutCheck:=1
		GuiControl, ReactionTime:, MainText, Correct!
		GuiControl, ReactionTime:, ScoreCount, % "Score : " Score+=1
	}
	If (QuestionType = 3)
	{
		TimeOutCheck:=1
		GuiControl, ReactionTime:, MainText, Correct!
		GuiControl, ReactionTime:, ScoreCount, % "Score : " Score+=1
	}
	If (QuestionType = 4)
	{
		TimeOutCheck:=1
		GuiControl, ReactionTime:, MainText, WRONG! Game Over...
		Gui, Red:Submit, Destory
		Gui, Blue:Submit, Destroy
		sleep 2000
		;run file path goes here
		ExitApp
	}
return ; end of ButtonBLUEBUTTON subroutine

By the way,, Gui, Submit, Destroy is not correct. You can either have Gui, Submit or Gui, Destroy, but not both in the same Gui command. You also misspelled Destroy in half of the instances.

SpanishMoss00
Posts: 16
Joined: 08 Apr 2021, 10:09

Re: GuiControl Line not running when run from an If statement

Post by SpanishMoss00 » 20 Jun 2021, 11:49

Thanks for helping me so much, I am very new to Ahk (as evident by my questions).
I have another small question, when I set the Gui name for the Red and Blue Boxes with

Code: Select all

Gui, Red:New
and

Code: Select all

Gui, Blue:New
respectively, so that I can

Code: Select all

Gui, Red:Destroy 
them, the normal buttons stop working. I assume this is because when I define the Buttons, I don't define which Gui's they come from. Is there a way to do this? If not, how would I fix this issue?

Code: Select all

Gui, Blue:New											; setting this to Gui, Blue:New, causes the button to break
Gui, Color, Blue
Gui, Add, Button, x40 y45 w144 h67 vRedButton, RED BUTTON
Gui, Show, w236 h159, Blue
Gui, Red:New											; setting this to Gui, Red:New, causes the button to break
Gui, Color, Red
Gui, Add, Button, x40 y45 w144 h67 vBlueButton, BLUE BUTTON
Gui, Show, w236 h159, Red
Thanks for all the help, and the previous fix you gave me works perfectly. Thanks (Also I changed the Gui, Submit, Destroy into Gui, Destroy {spelled correctly})
-SpanishMoss00

User avatar
boiler
Posts: 16705
Joined: 21 Dec 2014, 02:44

Re: GuiControl Line not running when run from an If statement

Post by boiler » 20 Jun 2021, 12:14

Per the Gui, Add, Button documentation:
For GUI windows other than the first, the window number is included in front of the button's automatic label; for example: 2ButtonOK.

So in this case, it would be:

Code: Select all

Gui, Blue:New
Gui, Color, Blue
Gui, Add, Button, x40 y45 w144 h67 vRedButton, RED BUTTON
Gui, Show, w236 h159, Blue
Gui, Red:New
Gui, Color, Red
Gui, Add, Button, x40 y45 w144 h67 vBlueButton, BLUE BUTTON
Gui, Show, w236 h159, Red
return

BlueButtonREDBUTTON:
	MsgBox, Red button pressed
return

RedButtonBLUEBUTTON:
	MsgBox, Blue button pressed
return

SpanishMoss00
Posts: 16
Joined: 08 Apr 2021, 10:09

Re: GuiControl Line not running when run from an If statement

Post by SpanishMoss00 » 20 Jun 2021, 17:48

Thanks again!
-SpanishMoss00

Post Reply

Return to “Ask for Help (v1)”