File loop cant read variable?

Ask gaming related questions
bautai
Posts: 17
Joined: 29 Nov 2020, 02:54

File loop cant read variable?

Post by bautai » 10 Jan 2021, 01:55

Hello, before that, I want you to see the contents of my code.

Code: Select all

#SingleInstance Ignore
#NoEnv
#KeyHistory 0
#Include FindClick.ahk
#Include D:\CL\FindText.ahk
#Include *i Gdip_All.ahk
#Include Gdip_ImageSearch.ahk
ListVars On
$a:: ; Auto Skill
	{
		CoordMode, Mouse, Window
		WinGetPos, sX, sY, sW, sH, ahk_exe Game.exe
		if (sW >= 1366) {
			Ycp := 30
		} else if (sW < 1366) {
			Ycp := 56
		}
		ToolTip, Auto Skill Active, 155, 5, 1
		FindClick(A_ScriptDir "\CP.png", "Silent1 Stay1 n0 CEnter0 r x14 y-4", Xfb, Yfb)
		Xfb -= sX
		Yfb -= sY
		loop {
			if FindClick("Enemy HP Bar.png","Silent1 Stay1 n0 r a0," Ycp ",,30",Xhp) {
				if (sW >= 1366) {
					Xhp := Xhp+112
				} else if (sW < 1366) {
					Xhp := 54+(sW//2)
				}
				if FindClick("*0xEA39A6","FuncGivePain Silent1 Stay1 e1 r a" Xhp "," Ycp ",10,30") {
				} else if FindClick("*0x320F24","FuncFindSkill Silent1 Stay1 e1 r a" Xhp "," Ycp ",10,30") {
				}
			}
		}
	}
return
GivePain() {
	send, 1
}
AddConf() {
	oText := ""
	if FileExist("SkillList.txt") {
		file := FileOpen("SkillList.txt", "r")
		LoadedText := file.Read()
		file.Close()
		sort, LoadedText, u
		; string := RegExReplace(LoadedText,"`am)(.*)\R+$","$1")
		FileDelete, % A_ScriptDir "\SkillList.txt"
		file := FileOpen("SkillList.txt", "w")
		file.Write(LoadedText)
		file.Close()
		; Split on new line
		oText := StrSplit(LoadedText, "`n")
		oText.Delete(oText.MaxIndex())
		return oText
	} else {
		Def := A_ScriptDir "\Skill\*.png"
		return Def
	}
}
FindSkill() {
	SaveConf := AddConf()
	if FileExist("SkillList.txt") {
		Loop % SaveConf.MaxIndex() {
			DirFile := SaveConf[A_Index]
			ExecSkill(DirFile)
			; MsgBox, % A_ScriptDir "\Skill\" SaveConf[A_Index]
		}
	} else {
		DirFile := SaveConf
		ExecSkill(DirFile)
	}
}
ExecSkill(DirFile) {
	; Xfb = 391, Yfb = 579
	global Xfb , Yfb , tempskill , SkillList
	; CoordMode, Mouse, Window
	tes := ""
	WinGet hwnd, ID, ahk_exe Game.exe
	Xcps := Xfb ; 391
	Ycps := Yfb ; 579
	Wcps := (Xcps+390) ;391+390 = 781
	Hcps := (Yfb+45) ; 579+45 = 624
	slot_width := 39
	global pToken := Gdip_Startup()
	global pBitmapGameWin := Gdip_BitmapFromScreen("hwnd:" hwnd)
	; Random, KeyD, 0, 300
	Random, PressD, 50, 200
	SetKeyDelay, , % PressD
		; MsgBox, % A_ScriptDir "\Skill\" DirFile
	loop % A_ScriptDir "\Skill\" DirFile , 1 , 1
	{
		MsgBox, % A_LoopFileFullPath
		global pBitmapSkill := Gdip_CreateBitmapFromFile(A_LoopFileFullPath)
		RET := Gdip_ImageSearch(pBitmapGameWin,pBitmapSkill,OutputList,Xcps,Ycps,Wcps,Hcps,,,,0)
		if (RET > 0) {
			; MsgBox, % "Sukses: " Xcps "," Ycps "," Wcps "," Hcps
			;********************************************************
			tes .= A_LoopFileName . "`n"
			;********************************************************
			temppos := StrSplit(OutputList, ",")
			Xs := temppos[1]
			Ys := temppos[2]
			temppos := ""
			column := Ceil((Xs-Xcps)/slot_width)
		}
		Gdip_DisposeImage(pBitmapSkill)
		if !(column <= 0)
			SendEvent, % column
		column := ""
		; Sleep 1000
	}
	Gdip_DisposeImage(pBitmapGameWin)
	Gdip_Shutdown(pToken)
	; MsgBox, Done, Please Re-Open Bot CL
	; ExitApp
	if !FileExist("SkillList.txt") {
		file := FileOpen("SkillList.txt", "a `r`n")
		file.Write(tes)
		tes := ""
		file.Close()
		MsgBox, Done, Please Re-Open Bot CL
		ExitApp
	}
}

#If
$esc::
	Gdip_DisposeImage(pBitmapSkill)
	Gdip_DisposeImage(pBitmapGameWin)
	Gdip_Shutdown(pToken)
	ExitApp
I was trying to find an image and it was aplenty, I used the Gdip_imagesearch script to speed up the search and it worked really well.
Please pay attention to file looping(DirFile Variable). I thought that I could still speed up the image search process by saving the file name into a text file and then when the loop was done it would not search for all image files, but would only repeat from the file name that was saved in the text file.

In short, I want the code that I generated when I first run it will do a search for images of all the images and then save them in a text file, and if the text file containing the names of the image files already exists there is no need to do an image search of the entire file. But just search it from the names included in the text file.

I've checked my code many times and nothing seems wrong. But what I wanted didn't materialize, the file loop doesn't read the DirFile variable whose value is the filename of the text file I stored in the array.

Please help me, best regards and thank you.
Last edited by BoBo on 10 Jan 2021, 05:24, edited 1 time in total.
Reason: Moved to Gaming section.
User avatar
lmstearn
Posts: 392
Joined: 11 Aug 2016, 02:32
GitHub: lmstearn
Contact:

Re: File loop cant read variable?

Post by lmstearn » 10 Jan 2021, 09:02

Do you have the AddConf function returning a string? It looks like DirFile is just one character.
An alternative approach might be to to make SaveConf into an array of string filenames by

Code: Select all

SaveConf.Push(AddConf())
:arrow: itros "ylbbub eht tuO kaerB" a ni kcuts m'I pleH
bautai
Posts: 17
Joined: 29 Nov 2020, 02:54

Re: File loop cant read variable?

Post by bautai » 22 Jan 2021, 05:56

lmstearn wrote:
10 Jan 2021, 09:02
Do you have the AddConf function returning a string? It looks like DirFile is just one character.
An alternative approach might be to to make SaveConf into an array of string filenames by

Code: Select all

SaveConf.Push(AddConf())
Yes, AddConf function returning a string. And please focus on a Loop % SaveConf.MaxIndex(), it make SaveConf into an array of string filenames. I test it with MsgBox. Correct me if I'm wrong.

Code: Select all

FindSkill() {
	SaveConf := AddConf()
	if FileExist("SkillList.txt") {
		Loop % SaveConf.MaxIndex() {
			DirFile := SaveConf[A_Index]
			ExecSkill(DirFile)
			; MsgBox, % A_ScriptDir "\Skill\" SaveConf[A_Index]
		}
	} else {
		DirFile := SaveConf
		ExecSkill(DirFile)
	}
}
User avatar
lmstearn
Posts: 392
Joined: 11 Aug 2016, 02:32
GitHub: lmstearn
Contact:

Re: File loop cant read variable?

Post by lmstearn » 23 Jan 2021, 01:06

This one might be an issue:

Code: Select all

loop % A_ScriptDir "\Skill\" DirFile , 1 , 1
Not familiar with the syntax. Do you want something like (untested)

Code: Select all

For Key in DirFile
testFile := % A_ScriptDir .  "\Skill\" .  DirFile[A_Index]
:arrow: itros "ylbbub eht tuO kaerB" a ni kcuts m'I pleH
Post Reply

Return to “Gaming”