Question

I've created this script to auto login into a game. It works fine, but there are multiple accounts, (^!2:: %acc2% %pw2%.. and so on). Rather than copying the code separately under each hotkey, I would like combine them but I can't figure out how.

;Autologin #1.
^!1::
ImageSearch,ExistUser_X,ExistUser_Y,0,0,A_ScreenWidth,A_ScreenHeight,%A_TEMP%\ExistUser.bmp
If !ErrorLevel
{
    Click,%ExistUser_X%,%ExistUser_Y%
    ImageSearch,Login_X,Login_Y,0,0,A_ScreenWidth,A_ScreenHeight,%A_TEMP%\Login.bmp
    Click,%Login_X%,%Login_Y%
    If !ErrorLevel
        Send,%acc1%{Tab}%pw1%{Enter}
    Else
        MsgBox,The login page is not visible.
    Return
}
Else If ErrorLevel=1
{
    ImageSearch,Login_X,Login_Y,0,0,A_ScreenWidth,A_ScreenHeight,%A_TEMP%\Login.bmp
    Click,%Login_X%,%Login_Y%
    If !ErrorLevel
        Send,%acc1%{Tab}%pw1%{Enter}
    Else
    MsgBox,The login page is not visible.
    Return
}
Else
MsgBox,The login page is not visible.
Return

A little run down on what it does; when you hit the hotkey, it'll search for a button (ExistUser.bmp) and click it if it's there. It will then get to the real login page (and make sure it's the actual page) where it will send the account details (%acc1% and %pw1%). If the user has manually clicked the ExistUser button, it will check for the login page then send the details. If it can't find either page it spits out an error MsgBox.

Appreciate any help, thanks!

Was it helpful?

Solution

Why not store the various accounts and passwords in a 2 dimensional array and run your code in the loop.

Something like this:

Loop, read, %A_ScriptDir%\Login.txt ; in username{Tab}Password{NewLine} format
{
    RecordNumber:=A_Index
    Loop, parse, A_LoopReadLine, %A_Tab% ; uses tab to separate items
    {
        Array_%RecordNumber%_%A_Index% := A_LoopField
    }
}
MaxRecords:=RecordNumber

Loop, %MaxRecords% 
{
        acc1:=Array_%A_Index%_1
        pw1:=Array_%A_Index%_2
        Run your code here
}
Return

Adding more {Tab} separated items e.g. unique button locations to the text file are automatically stored in the array as Array_%A_Index%3, Array%A_Index%_4, etc. and can be used in the loop code.

Or if you want to launch it with Ctrl+Alt+1 , Ctrl+Alt+2 , etc. define the acc1 and pw1 per hotkey and run the code.

;Autologin #1.
Return

^!1::
acc1=UserName
pw1=passwd
GoSub, MyScript
Return

^!2::
acc1=UserName2
pw1=passwd2
GoSub, MyScript
Return


MyScript:
ImageSearch,ExistUser_X,ExistUser_Y,0,0,A_ScreenWidth,A_ScreenHeight,%A_TEMP%\ExistUser.bmp
If !ErrorLevel
{
    Click,%ExistUser_X%,%ExistUser_Y%
    ImageSearch,Login_X,Login_Y,0,0,A_ScreenWidth,A_ScreenHeight,%A_TEMP%\Login.bmp
    Click,%Login_X%,%Login_Y%
    If !ErrorLevel
        Send,%acc1%{Tab}%pw1%{Enter}
    Else
        MsgBox,The login page is not visible.
    Return
}
Else If ErrorLevel=1
{
    ImageSearch,Login_X,Login_Y,0,0,A_ScreenWidth,A_ScreenHeight,%A_TEMP%\Login.bmp
    Click,%Login_X%,%Login_Y%
    If !ErrorLevel
        Send,%acc1%{Tab}%pw1%{Enter}
    Else
    MsgBox,The login page is not visible.
    Return
}
Else
MsgBox,The login page is not visible.
Return
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top