.txt parse as a trigger

Ask gaming related questions (AHK v1.1 and older)
NewbCoder
Posts: 13
Joined: 08 Dec 2019, 21:07

.txt parse as a trigger

08 Dec 2019, 21:12

I'm trying to write a script that will always be running. It will read a log file, a .txt file that basically changes every second. I want it to basically check the file every second and look at only the last line and then trigger from there. So far this is what I've started but I have absolutely no idea what I'm doing. It cannot look at the entire file cuz there are 10k+ lines of code and then it will trigger every second as it will see what its looking for over and over and over.

I want it to check the log file for a string of text and then Send 1 every time it sees it.

Code: Select all

	;AutoMage script

	Loop, read, C:\P2k\Logs\eqlog_Rex.txt
	last_line := A_LoopReadLine
	Loop, 
	{
	FileRead, A_LoopReadLine, C:\P2k\Logs\eqlog_Rex.txt
	if A_LoopReadLine contains Tex tells the group, 'Assist me
	{
		Send {1}
		return
	}    
	}
User avatar
Xtra
Posts: 2750
Joined: 02 Oct 2015, 12:15

Re: .txt parse as a trigger

08 Dec 2019, 23:54

Code: Select all

#NoEnv
SetBatchLines, -1
Loop
    eqLog("Tex tells the group, 'Assist me", "1")

eqLog(str, key) {
    FileRead, eqlog, C:\P2k\Logs\eqlog_Rex.txt
    if InStr(SubStr(eqlog, InStr(eqlog, "`n",, 0) + 1), str)
        Send % key
    Sleep 750    ; Note: change to adjust speed of script
}
NewbCoder
Posts: 13
Joined: 08 Dec 2019, 21:07

Re: .txt parse as a trigger

09 Dec 2019, 11:25

Thank you Xtra. Can you add a Random Sleep between 500 and 1000 before pressing 1?
NewbCoder
Posts: 13
Joined: 08 Dec 2019, 21:07

Re: .txt parse as a trigger

09 Dec 2019, 11:28

Also I tested this and it's not working. Not entirely sure whats going wrong its just not sending the 1 key.
User avatar
Xtra
Posts: 2750
Joined: 02 Oct 2015, 12:15

Re: .txt parse as a trigger

09 Dec 2019, 12:12

Make sure the text you are comparing to log matches.
Also check what the log reads with a message box:

Code: Select all

#NoEnv
SetBatchLines, -1
Loop
    eqLog("Tex tells the group, 'Assist me", "1")

eqLog(str, key) {
    FileRead, eqlog, C:\P2k\Logs\eqlog_Rex.txt
    
    MsgBox, 4096, log lastline, % SubStr(eqlog, InStr(eqlog, "`n",, 0) + 1), 3    ; for testing only
    
    if InStr(SubStr(eqlog, InStr(eqlog, "`n",, 0) + 1), str) {
        rndSleep(500,1000)
        Send % key
    }
    Sleep 750    ; Note: change to adjust speed of script
}

rndSleep(min,max) {
    Random, sleeptime, min, max
    Sleep, sleeptime
}
NewbCoder
Posts: 13
Joined: 08 Dec 2019, 21:07

Re: .txt parse as a trigger

09 Dec 2019, 12:38

It just keeps popping up an empty text box called log lastline. nothing in the box. So I dont believe it's actually reading the log.
User avatar
Xtra
Posts: 2750
Joined: 02 Oct 2015, 12:15

Re: .txt parse as a trigger

09 Dec 2019, 12:51

Run this:

Code: Select all

MsgBox,, FileExist(), % FileExist("C:\P2k\Logs\eqlog_Rex.txt") ? "true" : "false"
NewbCoder
Posts: 13
Joined: 08 Dec 2019, 21:07

Re: .txt parse as a trigger

09 Dec 2019, 13:45

Comes up true. So its finding the log. Just not reading it.
User avatar
Xtra
Posts: 2750
Joined: 02 Oct 2015, 12:15

Re: .txt parse as a trigger

09 Dec 2019, 13:48

Open your log file and post some of the lines that also include the text you are looking for.
User avatar
Xtra
Posts: 2750
Joined: 02 Oct 2015, 12:15

Re: .txt parse as a trigger

09 Dec 2019, 16:15

Give this a try i think your logs last line is blank.

Code: Select all

#NoEnv
SetBatchLines, -1
Loop
    eqLog("Tex tells the group, 'Assist me", "1")

eqLog(str, key) {
    FileRead, eqlog, C:\P2k\Logs\eqlog_Rex.txt
    
    MsgBox, 4096, log lastline, % SubStr(eqlog, InStr(eqlog, "`n",, 0, 2) + 1), 3    ; for testing only
    
    if InStr(SubStr(eqlog, InStr(eqlog, "`n",, 0, 2) + 1), str)
    {
        rndSleep(500,1000)
        Send % key
    }
    Sleep 750    ; Note: change to adjust speed of script
}

rndSleep(min,max)
{
    Random, sleeptime, min, max
    Sleep, sleeptime
}
InStr(eqlog, "`n",, 0, 2) + 1 will give the position of the newline (`n) before the blank last line.
NewbCoder
Posts: 13
Joined: 08 Dec 2019, 21:07

Re: .txt parse as a trigger

09 Dec 2019, 23:17

You are correct I double checked the log and it leaves the last line blank for some reason. So the line right above that is the one that needs to be read.
NewbCoder
Posts: 13
Joined: 08 Dec 2019, 21:07

Re: .txt parse as a trigger

09 Dec 2019, 23:22

It is working! Now I don't quite understand the random sleep, what numbers do i change so its random between 500 and 1500? Also I changed the Sleep 750 to Sleep 1000.
NewbCoder
Posts: 13
Joined: 08 Dec 2019, 21:07

Re: .txt parse as a trigger

09 Dec 2019, 23:31

I also want to add another trigger. Would you be able to show me how to do that?

Tex tells the group, 'Follow me

Random sleep 500,1000
Send 2
User avatar
Xtra
Posts: 2750
Joined: 02 Oct 2015, 12:15

Re: .txt parse as a trigger

10 Dec 2019, 01:23

With the current script you would add more function calls into the loop.
Example:

Code: Select all

#NoEnv
SetBatchLines, -1
Loop
{
    eqLog("Tex tells the group, 'Assist me", "1", 500, 1500)
    eqLog("Tex tells the group, 'Follow me", "2", 500, 1000)
}

eqLog(str, key, min, max) {
    FileRead, eqlog, C:\P2k\Logs\eqlog_Rex.txt
    if InStr(SubStr(eqlog, InStr(eqlog, "`n",, 0, 2) + 1), str)
    {
        rndSleep(min,max)
        Send % key
    }
    Sleep 1000    ; Note: change to adjust speed of script
}

rndSleep(min,max)
{
    Random, sleeptime, min, max
    Sleep, sleeptime
}
The only bad part now would be the log file is read each time you want to check a trigger.
(This is why its important to state what the intentions of the script is before you start, not a problem just saying)

Now that we know we want any number of triggers we can change the script to handle an array of objects which will make things easier.
Example:

Code: Select all

#NoEnv
SetBatchLines, -1
triggers := []
triggers.push( {"str": "Tex tells the group, 'Assist me", "key": 1, "min":500, "max": 1500, "delay": 1000} )
triggers.push( {"str": "Tex tells the group, 'Follow me", "key": 2, "min":500, "max": 1000, "delay": 1000} )
; Add more triggers using above format (I could put all in one line but this should be easier for you to follow)

Loop
    eqLog(triggers)

eqLog(obj) {
    FileRead, eqlog, C:\P2k\Logs\eqlog_Rex.txt
    for each, trigger in obj
        if InStr(SubStr(eqlog, InStr(eqlog, "`n",, 0, 2) + 1), trigger.str)
        {
            Random, sleeptime, trigger.min, trigger.max
            Sleep, sleeptime
            Send % trigger.key
        }
    Sleep trigger.delay
}
NewbCoder
Posts: 13
Joined: 08 Dec 2019, 21:07

Re: .txt parse as a trigger

10 Dec 2019, 09:16

Sorry about that. I thought I would be able to see what you did and break it down and add more myself but what you've provided me is so far beyond my understanding currently that I couldn't figure it out. Thank you again so much for all your help, this is fantastic. I have learned a lot here.
User avatar
Xtra
Posts: 2750
Joined: 02 Oct 2015, 12:15

Re: .txt parse as a trigger

10 Dec 2019, 12:55

Added a Break to the for loop. This will optimize the script so when a trigger is found it will not check for any other triggers when it has completed.

Code: Select all

#NoEnv
SetBatchLines, -1
triggers := []
triggers.push( {"str": "Tex tells the group, 'Assist me", "key": 1, "min": 500, "max": 1500, "delay": 1000} )
triggers.push( {"str": "Tex tells the group, 'Follow me", "key": 2, "min": 500, "max": 1000, "delay": 1000} )
; Add more triggers using above format (I could put all in one line but this should be easier for you to follow)

Loop
    eqLog(triggers)

eqLog(obj) {
    FileRead, eqlog, C:\P2k\Logs\eqlog_Rex.txt
    for each, trigger in obj
        if InStr(SubStr(eqlog, InStr(eqlog, "`n",, 0, 2) + 1), trigger.str)
        {
            Random, sleeptime, trigger.min, trigger.max
            Sleep, sleeptime
            Send % trigger.key
            break
        }
    Sleep trigger.delay
}
NewbCoder
Posts: 13
Joined: 08 Dec 2019, 21:07

Re: .txt parse as a trigger

11 Dec 2019, 13:14

For some reason this last version isn't working.
User avatar
Xtra
Posts: 2750
Joined: 02 Oct 2015, 12:15

Re: .txt parse as a trigger

11 Dec 2019, 14:07

Should work fine you can check it with this test script here:

Code: Select all

SetBatchLines, -1
triggers := []
triggers.push( {"str": "Tex tells the group, 'Assist me", "key": 1, "min": 500, "max": 1500, "delay": 1000} )
triggers.push( {"str": "Tex tells the group, 'Follow me", "key": 2, "min": 500, "max": 1000, "delay": 1000} )
; Add more triggers using above format (I could put all in one line but this should be easier for you to follow)

Loop
    eqLog(triggers)

eqLog(obj) {
	eqlog := "
	(
	Tex tells the group, 'Assist me
	Tex tells the group, 'Follow me
	
	)"
    ;~ FileRead, eqlog, C:\P2k\Logs\eqlog_Rex.txt
    for each, trigger in obj
        if InStr(SubStr(eqlog, InStr(eqlog, "`n",, 0, 2) + 1), trigger.str)
        {
            Random, sleeptime, trigger.min, trigger.max
            Sleep, sleeptime
			MsgBox,, sleeptime, % "Sleep, " sleeptime, 1
            MsgBox,, trigger.key, % "Send, " trigger.key, 1
            break
        }
	MsgBox,, trigger.delay, % "Sleep, " trigger.delay, 1
}
NewbCoder
Posts: 13
Joined: 08 Dec 2019, 21:07

Re: .txt parse as a trigger

12 Dec 2019, 21:25

This pops up 3 message boxes. The random sleep time, Send, 2 and Sleep 1000.

It shouldn't be sending 2 as the line isnt being seen currently.
User avatar
Xtra
Posts: 2750
Joined: 02 Oct 2015, 12:15

Re: .txt parse as a trigger

12 Dec 2019, 23:50

Yes and the line its reading from the variable is the line you want to send 2?
The whole point of the test script is to show what the script is seeing. Whether from the temp var (log) or the obj / arrays.

This is the same as what is seen from the log:

Code: Select all

	eqlog := "
	(
	Tex tells the group, 'Assist me
	Tex tells the group, 'Follow me
	
	)"
What exactly is not working in the script. If you are having an issue explain it better than its not working.

Return to “Gaming Help (v1)”

Who is online

Users browsing this forum: No registered users and 66 guests