Game script complains of no matching if with ELSE statement

Ask gaming related questions (AHK v1.1 and older)
GoneFishing
Posts: 126
Joined: 20 May 2022, 16:17

Game script complains of no matching if with ELSE statement

Post by GoneFishing » 27 May 2022, 16:27

Need some help with a script I've built to perform some functions inside a game. The script keeps complaining that there is an ELSE with no matching IF.
The script checks to see if a game windows is active, setups a key to press to start the script (and another to reload it). It then checks for an icon on the screen.
It then goes in to a loop reading a file and looking for a particular string and number. If it finds the number it executes a number of actions. The script works up to this point

If it does not find the string it should execute the else clause (and run that clause until it matches the IF clause). This is where it is breaking down and complaining about no matching IF for the ELSE. I've put the ELSE part where I believe it should go and as far as I can tell all the brackets match up. Where should the else part of the script go so the will loop and run properly ?

Code: Select all

#IfWinActive, Game Name
\::reload
$[::
loop 
{
pixelGetColor, pixel1, 282, 56, RGB     ;this is the grace period icon. It will appear to the right of any skill icon or by itself if no other character skills icons
sleep 530
if pixel1 = 0xFCD79D
{
 {
  FilePath := A_ScriptDir "\Client.txt" ; relative path to file
   ;MsgBox %A_ScriptDir%
   File := FileOpen(FilePath, "r")
   OldLogFileSize := 0
   File.Seek(0, 2) ; move the file pointer to the end of the file to start reading newly added lines
  Loop
 {
	LogFileSize := File.Length
	if (LogFileSize > OldLogFileSize)
	{
		Loop
		{
			if File.AtEOF
				Break
			TextLine := File.Readline()
			If InStr(TextLine, "seed")    ;searching for keyword
			{
			    RegExMatch(TextLine, "\bseed\s+\d*\K00$", RFNum)   ;find keyword then match 00 at the end of the number after keyword
				If RFNum = 00
				{
				 MsgBox, % "The Seed Number matches ! " RFNum ; Display number. Change this to action once working
				 run, C:\AHk\Scripts\English.mp3       ; play specific alert sound
				 pause
				} 
			}
		}
		else
		click, 1816, 1032                      ;world map co-ordinates in select world area
        sleep 500
        send ^{click, 654, 647}              ;ctrl+click for new instance 
        sleep 500
        click, 781, 698                       ;select instance menu
        sleep 400		
		OldLogFileSize := LogFileSize
	}
	Sleep, 1000 ; wait one second before checking again (change to whatever)
  }
 } 
}
return

User avatar
Xtra
Posts: 2744
Joined: 02 Oct 2015, 12:15

Re: Game script complains of no matching if with ELSE statement

Post by Xtra » 27 May 2022, 17:01

See Comments:

Code: Select all

#IfWinActive, Game Name

\::reload

$[::
loop 
{
    pixelGetColor, pixel1, 282, 56, RGB     ;this is the grace period icon. It will appear to the right of any skill icon or by itself if no other character skills icons
    sleep 530
    if pixel1 = 0xFCD79D
    {
;{    ; <<<<<<<<<<<<<<<<<<< Removed
        FilePath := A_ScriptDir "\Client.txt" ; relative path to file
        ;MsgBox %A_ScriptDir%
        File := FileOpen(FilePath, "r")
        OldLogFileSize := 0
        File.Seek(0, 2) ; move the file pointer to the end of the file to start reading newly added lines
        Loop
        {
            LogFileSize := File.Length
            if (LogFileSize > OldLogFileSize)
            {
                Loop
                {
                    if File.AtEOF
                        Break
                    TextLine := File.Readline()
                    If InStr(TextLine, "seed")    ;searching for keyword
                    {
                        RegExMatch(TextLine, "\bseed\s+\d*\K00$", RFNum)   ;find keyword then match 00 at the end of the number after keyword
                        If RFNum = 00
                        {
                            MsgBox, % "The Seed Number matches ! " RFNum ; Display number. Change this to action once working
                            run, C:\AHk\Scripts\English.mp3       ; play specific alert sound
                            pause
                        } 
                    }
                }
            }    ; <<<<<<<<<<<<<<<<<<< Added
            else
            {    ; <<<<<<<<<<<<<<<<<<< Added
                click, 1816, 1032                      ;world map co-ordinates in select world area
                sleep 500
                send ^{click, 654, 647}              ;ctrl+click for new instance 
                sleep 500
                click, 781, 698                       ;select instance menu
                sleep 400		
                OldLogFileSize := LogFileSize
            }
            Sleep, 1000 ; wait one second before checking again (change to whatever)
        }
    } 
}
return
edited: missed one }

GoneFishing
Posts: 126
Joined: 20 May 2022, 16:17

Re: Game script complains of no matching if with ELSE statement

Post by GoneFishing » 27 May 2022, 18:01

Thank you for fixing it ! The script runs without errors now but it won't start in game when I press the [ key. Any ideas why that would be, or how to troubleshoot why it is not going when [ is pressed ?
The script is the same as above (and I put the correct game name in) but I can reload it if needed.

User avatar
Xtra
Posts: 2744
Joined: 02 Oct 2015, 12:15

Re: Game script complains of no matching if with ELSE statement

Post by Xtra » 27 May 2022, 18:28

If [ is an in game binded key try using:

Code: Select all

~[::
~ causes the keyboard hook to be used so $ is not needed too.

GoneFishing
Posts: 126
Joined: 20 May 2022, 16:17

Re: Game script complains of no matching if with ELSE statement

Post by GoneFishing » 27 May 2022, 19:03

[ isn't an in game key bind. I have other scripts I use in the same game and use [ . It works fine with those. I just pulled the same config to use in this script. I did try your suggestion but it still did not work :-(
If the $[:: in the correct place for this script - should it be somewhere else instead ? Here's an example of a script for same game that works with [ key

Code: Select all

#IfWinActive, GameName
\::reload
$[::
loop
{
pixelGetColor, pixel1, 282, 56, RGB
sleep 750
if pixel1 = 0xFCD79D
{
 pixelGetColor, pixel2, 2034, 62, RGB
 sleep 750
 if pixel2 = 0x933296
 {
  run, c:\AHk\Scripts\English.wav
  pause
 }
 else
 click, 1809, 1000
 sleep 1500
 send ^{click, 1016, 1063}
 sleep 1500
 click, 780, 702
 sleep 1000
 click, 780, 702
 sleep 1200
 mousemove, 1282, 635
}
else
sleep 320
}
return
Last edited by GoneFishing on 27 May 2022, 19:13, edited 1 time in total.

User avatar
Xtra
Posts: 2744
Joined: 02 Oct 2015, 12:15

Re: Game script complains of no matching if with ELSE statement

Post by Xtra » 27 May 2022, 19:10

Hotkey placement looks fine.
You could try it removing this line: #IfWinActive, Game Name
If it works then the wintitle does not match.

GoneFishing
Posts: 126
Joined: 20 May 2022, 16:17

Re: Game script complains of no matching if with ELSE statement

Post by GoneFishing » 27 May 2022, 19:16

Removing #IfWinActive, Game Name did not help unfortunately - it still did not work

User avatar
Xtra
Posts: 2744
Joined: 02 Oct 2015, 12:15

Re: Game script complains of no matching if with ELSE statement

Post by Xtra » 27 May 2022, 19:20

I have other scripts I use in the same game and use [
Try this script by itself with no other scripts running.
When running multiple scripts with the same hotkey only the last run script will have the hotkey to use.

GoneFishing
Posts: 126
Joined: 20 May 2022, 16:17

Re: Game script complains of no matching if with ELSE statement

Post by GoneFishing » 27 May 2022, 19:28

If I edit out the pause the [ will work but it only executes as far as the MsgBox

Code: Select all

#IfWinActive, Game Name
\::reload
$[::
loop 
{
pixelGetColor, pixel1, 282, 56, RGB     ;this is the grace period icon. It will appear to the right of any skill icon or by itself if no other character skills icons
sleep 530
if pixel1 = 0xFCD79D
{
  FilePath := A_ScriptDir "\Client.txt" ; relative path to file
   MsgBox %A_ScriptDir%   ;  <------ This executes. Nothing after it though if pause is commented out below
   File := FileOpen(FilePath, "r")
   OldLogFileSize := 0
   File.Seek(0, 2) ; move the file pointer to the end of the file to start reading newly added lines
  Loop
 {
	LogFileSize := File.Length
	if (LogFileSize > OldLogFileSize)
	{
		Loop
		{
			if File.AtEOF
				Break
			TextLine := File.Readline()
			If InStr(TextLine, "seed")    ;searching for keyword
			{
			    RegExMatch(TextLine, "\bseed\s+\d*\K00$", RFNum)   ;find keyword then match 00 at the end of the number after keyword
				If RFNum = 00
				{
				 MsgBox, % "The Seed Number matches ! " RFNum ; Display number. Change this to action once working
				 run, C:\AHk\Scripts\English.mp3       ; play specific alert sound
				 ;pause.     <--------  Comment this out and the [ hot key will start the script but stops after the first MsgBox above before the loop
				} 
			}
		}
		}
		else
		{
		click, 1816, 1032                      ;world map co-ordinates in select world area
        sleep 500
        send ^{click, 654, 647}              ;ctrl+click for new instance 
        sleep 500
        click, 781, 698                       ;select instance menu
        sleep 400		
		OldLogFileSize := LogFileSize
	}
	Sleep, 1000 ; wait one second before checking again (change to whatever)
  }
 } 
}
return
Last edited by GoneFishing on 27 May 2022, 20:16, edited 1 time in total.

GoneFishing
Posts: 126
Joined: 20 May 2022, 16:17

Re: Game script complains of no matching if with ELSE statement

Post by GoneFishing » 27 May 2022, 19:40

Xtra wrote:
27 May 2022, 19:20
I have other scripts I use in the same game and use [
Try this script by itself with no other scripts running.
When running multiple scripts with the same hotkey only the last run script will have the hotkey to use.
Sorry - I should have explained better. I only have this one script running, nothing else.

User avatar
Xtra
Posts: 2744
Joined: 02 Oct 2015, 12:15

Re: Game script complains of no matching if with ELSE statement

Post by Xtra » 27 May 2022, 22:19

On this script line:
File := FileOpen(FilePath, "r")

change to:

Code: Select all

File := FileOpen(FilePath, "r")
if !IsObject(File)
    MsgBox, 4112, FileOpen() Error, % FilePath

GoneFishing
Posts: 126
Joined: 20 May 2022, 16:17

Re: Game script complains of no matching if with ELSE statement

Post by GoneFishing » 28 May 2022, 09:04

I put that code in. It did not display an error message. Script finds and reads the log file as expected. I also put in multiple MsgBox messages at certain points (see below). I have put comments as to what happens at each MsgBox message. The script will run after pressing [ now but it never goes into the ELSE clause (or does not seem to). I tried the script by commenting out the ELSE clause. It worked when I did that. Where do I go from here ?

Code: Select all

#IfWinActive, Game Name
\::reload
$[::         ; <-----   This seems to work now but the script will not execute the ELSE clause 
loop 
{
pixelGetColor, pixel1, 282, 56, RGB     ;this is the grace period icon. It will appear to the right of any skill icon or by itself if no other character skills icons
sleep 530
if pixel1 = 0xFCD79D
{
   FilePath := A_ScriptDir "\Client.txt" ; relative path to file
   ;MsgBox, %A_ScriptDir%   <----- This will execute and shows the correct path when enabled
   File := FileOpen(FilePath, "r")
   if !IsObject(File)
   MsgBox, 4112, FileOpen() Error, % FilePath   ; <------ Does not display any error message
   OldLogFileSize := 0
   File.Seek(0, 2) ; move the file pointer to the end of the file to start reading newly added lines
  Loop
 {
	LogFileSize := File.Length
	if (LogFileSize > OldLogFileSize)
	{
	Loop
		{
			if File.AtEOF
				Break
			TextLine := File.Readline()
			Msgbox, %TextLine%   ; <-------   This will correctly display the last line in the file as expected after pressing [ so script is getting this far
			If InStr(TextLine, "seed")    ;searching for keyword
			{
			    RegExMatch(TextLine, "\bseed\s+\d*\K00$", RFNum)   ;find keyword then match 00 at the end of the number after keyword
				If RFNum = 00
				{
				 MsgBox, % "The Seed Number matches ! " RFNum ; Display number. Change this to action once working
				 run, C:\AHk\Scripts\English.mp3       ; play specific alert sound
				 pause
				} 
			}
		}
		}
		else   ; <-------  This does not seem to get executed when the IF statement is false. The Msgbox below NEVER appears.
		{
		click, 1816, 1032                      ;world map co-ordinates in select world area
        sleep 500
        send ^{click, 654, 647}              ;ctrl+click for new instance 
        sleep 500
        click, 781, 698                       ;select instance menu
        sleep 400		
		OldLogFileSize := LogFileSize
		MsgBox, "Logfile size is " %OldLogFileSize%   <----   This does not display when IF condition is false
	}
	Sleep, 1000 ; wait one second before checking again (change to whatever)
  }
 } 
}
return
Last edited by GoneFishing on 28 May 2022, 12:33, edited 1 time in total.

User avatar
Xtra
Posts: 2744
Joined: 02 Oct 2015, 12:15

Re: Game script complains of no matching if with ELSE statement

Post by Xtra » 28 May 2022, 10:25

This line OldLogFileSize := LogFileSize needs to be moved to the bottom of the loop:

Code: Select all

#IfWinActive, Game Name

\::reload

$[::
loop 
{
    pixelGetColor, pixel1, 282, 56, RGB     ;this is the grace period icon. It will appear to the right of any skill icon or by itself if no other character skills icons
    sleep 530
    if pixel1 = 0xFCD79D
    {
        FilePath := A_ScriptDir "\Client.txt" ; relative path to file
        ;MsgBox %A_ScriptDir%
        File := FileOpen(FilePath, "r")
        OldLogFileSize := 0
        File.Seek(0, 2) ; move the file pointer to the end of the file to start reading newly added lines
        Loop
        {
            LogFileSize := File.Length
            if (LogFileSize > OldLogFileSize)
            {
                Loop
                {
                    if File.AtEOF
                        Break
                    TextLine := File.Readline()
                    If InStr(TextLine, "seed")    ;searching for keyword
                    {
                        RegExMatch(TextLine, "\bseed\s+\d*\K00$", RFNum)   ;find keyword then match 00 at the end of the number after keyword
                        If RFNum = 00
                        {
                            MsgBox, % "The Seed Number matches ! " RFNum   ; Display number. Change this to action once working
                            run, C:\AHk\Scripts\English.mp3                ; play specific alert sound
                            pause
                        } 
                    }
                }
            }
            else
            {
                click, 1816, 1032                    ;world map co-ordinates in select world area
                sleep 500
                send ^{click, 654, 647}              ;ctrl+click for new instance 
                sleep 500
                click, 781, 698                      ;select instance menu
                sleep 400		
            }
            OldLogFileSize := LogFileSize    ; <<< Moved to here <<<
            Sleep, 1000 ; wait one second before checking again (change to whatever)
        }
    } 
}
return

GoneFishing
Posts: 126
Joined: 20 May 2022, 16:17

Re: Game script complains of no matching if with ELSE statement

Post by GoneFishing » 28 May 2022, 12:56

It's all working now ! Thanks so much for all your time and patience.
I would like to implement a section that sends an email notification as well but I think it's best I post a new thread for that unless you have a code snippet I could use now.

User avatar
Xtra
Posts: 2744
Joined: 02 Oct 2015, 12:15

Re: Game script complains of no matching if with ELSE statement

Post by Xtra » 28 May 2022, 13:58

Good deal, do a forum search for CDO

Post Reply

Return to “Gaming Help (v1)”