Jump to content

Increasing Array subscript in a loop?


Recommended Posts

I'm writing a program to record my clicks and save them to a file, then play them back, with other features, like an offset, speed adjusting, and looping. I've got most of it set, but need some help with the writing and reading from the file. I finished my first program using an .ini file a few days ago, so I know the basics. I'm thinking I should use an array and some kind of loop (for... to... maybe? I've never used that kind before though) to increase the array subscript by one, every time a new value is written. I've read http://www.autoitscript.com/wiki/Arrays a few times, but am still unclear on how to start.

What would be the best way to do this?

The other option is to use a text file and line numbers, but I'd like to learn how to use array's for the future.

I'll show you what I have so for for the capture function. When I press a hotkey, it grabs the current mouse position and writes it to an .ini file. The problem of course, is that every time I press it, it will overwrite the old value, and update it with the new.

Func capture()
    #Region Original *******************************************************************************************************
    Local $pos = MouseGetPos()
    If $file = 1 Then ;$file starts off equal to 1, then gets defined later as "example.ini" in the new, or open dialog. Probably a better way of doing this...
        $msg = "First create a new file to record to, or open an existing one to add to."
        MsgBox(48, "Error", $msg)
    Else
        $x = $pos[0]
        $y = $pos[1]
        IniWrite($file, "Clicks", "$x", $x)
        IniWrite($file, "Clicks", "$y", $y)
        TrayTip("Cursor Position Captured", "Mouse position captured at " & $x & " and " & $y, 10)
    EndIf
    ;http://www.autoitscript.com/wiki/Arrays
    #EndRegion Original ******************************************************************************************************

    ;I'm looking to instead use an array for this.
    ;Local $x[100]
    ;Local $y[100]
    ;maybe put some sort of for... to... loop here?
EndFunc   ;==>capture
Any other constructive criticism is welcomed

Here is the full script:

HotKeySet("^{space}", "capture")
;HotKeySet("{Pgup}", "Fast") ; work on this once the main part is up and running
;HotKeySet("{PgDn}", "Slow") ; this too
HotKeySet("{f5}", "Optional_Click")
HotKeySet("{esc}", "Close")
$file = 1 ; this is so I can tell later if a location for the file has been picked. Probably a better way of doing this...

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <SliderConstants.au3>
#include <StaticConstants.au3>
#include <TabConstants.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=d:\documents\farmville bot\recorder gui.kxf
$Form1_1 = GUICreate("Macro Maker", 284, 551, 486, 118)
GUISetFont(11, 400, 0, "Segoe Print")
$Recording = GUICtrlCreateTab(8, 8, 265, 529)
GUICtrlSetFont(-1, 11, 400, 0, "Segoe Print")
GUICtrlSetResizing(-1, $GUI_DOCKWIDTH + $GUI_DOCKHEIGHT)
$TabSheet1 = GUICtrlCreateTabItem("Recording")
$Button5 = GUICtrlCreateButton("Instructions", 20, 53, 241, 41, $WS_GROUP)
GUICtrlSetFont(-1, 11, 400, 0, "Segoe Print")
$Group4 = GUICtrlCreateGroup("File...", 20, 101, 241, 73)
GUICtrlSetFont(-1, 11, 400, 0, "Segoe Print")
$Button7 = GUICtrlCreateButton("Open...", 28, 133, 105, 33, $WS_GROUP)
$Button6 = GUICtrlCreateButton("New...", 140, 133, 113, 33, $WS_GROUP)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Group6 = GUICtrlCreateGroup("Quick Reference", 20, 181, 241, 345)
GUICtrlSetFont(-1, 11, 400, 0, "Segoe Print")
$Label11 = GUICtrlCreateLabel("Ctrl + Spacebar = Record Mouse", 32, 216, 200, 25)
GUICtrlSetFont(-1, 9, 400, 0, "Segoe Print")
$Label12 = GUICtrlCreateLabel("Ctrl + Shift + D = Custom delay", 32, 328, 199, 25)
GUICtrlSetFont(-1, 9, 400, 0, "Segoe Print")
$Label13 = GUICtrlCreateLabel("delay", 104, 304, 35, 25)
GUICtrlSetFont(-1, 9, 400, 0, "Segoe Print")
$Label14 = GUICtrlCreateLabel("Ctrl + D = Insert 500 millisecond ", 32, 288, 213, 25)
GUICtrlSetFont(-1, 9, 400, 0, "Segoe Print")
$Label15 = GUICtrlCreateLabel("(input-box)", 144, 344, 73, 25)
GUICtrlSetFont(-1, 9, 400, 0, "Segoe Print")
$Label16 = GUICtrlCreateLabel("Open or create a new file to begin.", 32, 416, 216, 25)
GUICtrlSetFont(-1, 9, 400, 0, "Segoe Print")
$Label17 = GUICtrlCreateLabel("Simply press Ctrl + Space to record", 32, 432, 222, 25)
GUICtrlSetFont(-1, 9, 400, 0, "Segoe Print")
$Label18 = GUICtrlCreateLabel("a mouse click at the current mouse", 32, 448, 218, 25)
GUICtrlSetFont(-1, 9, 400, 0, "Segoe Print")
$Label19 = GUICtrlCreateLabel("position. The file open/new work", 32, 464, 204, 25)
GUICtrlSetFont(-1, 9, 400, 0, "Segoe Print")
$Label20 = GUICtrlCreateLabel("for both this tab, and the playback", 32, 480, 220, 25)
GUICtrlSetFont(-1, 9, 400, 0, "Segoe Print")
$Label21 = GUICtrlCreateLabel("tab.", 32, 496, 27, 25)
GUICtrlSetFont(-1, 9, 400, 0, "Segoe Print")
$Label22 = GUICtrlCreateLabel("PgUp = Record Mouse double speed", 32, 240, 218, 25)
GUICtrlSetFont(-1, 9, 400, 0, "Segoe Print")
$Label24 = GUICtrlCreateLabel("PgDn = Record Mouse half speed", 32, 264, 203, 25)
GUICtrlSetFont(-1, 9, 400, 0, "Segoe Print")
GUICtrlCreateGroup("", -99, -99, 1, 1)
$TabSheet2 = GUICtrlCreateTabItem("Playback")
GUICtrlSetState(-1, $GUI_SHOW)
$Group2 = GUICtrlCreateGroup("Optional click for playback", 20, 61, 241, 129)
GUICtrlSetFont(-1, 11, 400, 0, "Segoe Print")
$Input1 = GUICtrlCreateInput("0", 28, 93, 65, 34)
$Updown1 = GUICtrlCreateUpdown($Input1)
$Input2 = GUICtrlCreateInput("0", 100, 93, 65, 34)
$Updown2 = GUICtrlCreateUpdown($Input2)
$Label3 = GUICtrlCreateLabel("f5 = set", 172, 93, 52, 25)
GUICtrlSetFont(-1, 9, 400, 0, "Segoe Print")
$Radio1 = GUICtrlCreateRadio("Every other", 28, 149, 113, 17)
$Radio3 = GUICtrlCreateRadio("Other...", 28, 165, 97, 17)
$Input3 = GUICtrlCreateInput("0", 204, 149, 49, 34)
$Updown3 = GUICtrlCreateUpdown($Input3)
$Label4 = GUICtrlCreateLabel("Other", 204, 133, 37, 23)
GUICtrlSetFont(-1, 8, 400, 0, "Segoe Print")
$Radio4 = GUICtrlCreateRadio("None", 28, 133, 73, 17)
$Label5 = GUICtrlCreateLabel("new location", 172, 109, 81, 25)
GUICtrlSetFont(-1, 9, 400, 0, "Segoe Print")
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Group5 = GUICtrlCreateGroup("Optional offset for playback", 20, 197, 241, 129)
GUICtrlSetFont(-1, 11, 400, 0, "Segoe Print")
$Input4 = GUICtrlCreateInput("0", 68, 237, 65, 34)
$Updown4 = GUICtrlCreateUpdown($Input4)
$Input5 = GUICtrlCreateInput("0", 180, 237, 65, 34)
$Updown5 = GUICtrlCreateUpdown($Input5)
$Label7 = GUICtrlCreateLabel("X =", 36, 245, 30, 30)
$Label6 = GUICtrlCreateLabel("Y =", 148, 245, 29, 30)
$Label8 = GUICtrlCreateLabel("Leaving at zero doesn't effect playback", 28, 301, 218, 23)
GUICtrlSetFont(-1, 8, 400, 0, "Segoe Print")
$Button8 = GUICtrlCreateButton("Reset to  X=0  Y=0", 28, 277, 225, 25, $WS_GROUP)
GUICtrlSetFont(-1, 10, 400, 0, "Segoe Print")
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Group1 = GUICtrlCreateGroup("Playback Speed and Delay", 20, 333, 241, 137)
GUICtrlSetFont(-1, 11, 400, 0, "Segoe Print")
$Slider1 = GUICtrlCreateSlider(28, 365, 225, 33)
GUICtrlSetBkColor(-1, 0xFFFFFF) ;                       sets slider background white, to match the tabs
$Label1 = GUICtrlCreateLabel("Fast", 36, 397, 29, 25)
GUICtrlSetFont(-1, 9, 400, 0, "Segoe Print")
$Label2 = GUICtrlCreateLabel("Slow", 212, 397, 33, 25)
GUICtrlSetFont(-1, 9, 400, 0, "Segoe Print")
$Input6 = GUICtrlCreateInput("0", 40, 432, 97, 34)
$Updown6 = GUICtrlCreateUpdown($Input6)
$Label9 = GUICtrlCreateLabel("  Optional Delay", 136, 432, 85, 23)
GUICtrlSetFont(-1, 8, 400, 0, "Segoe Print")
$Label10 = GUICtrlCreateLabel("  (in milliseconds)", 136, 448, 91, 23)
GUICtrlSetFont(-1, 8, 400, 0, "Segoe Print")
$Label26 = GUICtrlCreateLabel(" Speed = 0", 88, 400, 98, 25)
GUICtrlSetFont(-1, 9, 400, 0, "Segoe Print")
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Button1 = GUICtrlCreateButton("Play", 20, 477, 241, 49, $WS_GROUP)
GUICtrlSetFont(-1, 20, 400, 0, "Segoe Print")
GUICtrlCreateTabItem("")
GUICtrlSetTip(-1, "Remember, you can still record with this tab activated")
;GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

;;;;;;;;;;;;;;;;set defaults;;;;;;;;;;;;;;;;;;;;;;;;;
GUICtrlSetLimit($Updown1, 1600, 0)
GUICtrlSetLimit($Updown2, 1080, 0)
GUICtrlSetLimit($Updown3, 10, 2)
GUICtrlSetLimit($Updown4, -1000, 1000)
GUICtrlSetLimit($Updown5, -1000, 1000)
GUICtrlSetLimit($Updown6, 10000, 0)
GUICtrlSetLimit($Slider1, 50, 0)
;these set the radio button and block the appropriate input boxes
GUICtrlSetState($Radio4, $GUI_CHECKED)
GUICtrlSetState($Input1, $GUI_DISABLE)
GUICtrlSetState($Input2, $GUI_DISABLE)
GUICtrlSetState($Input3, $GUI_DISABLE)
$radio = 4 ;this shows the current radio selection, to tell wether the GUI_ENABLE etc.. should be refreshed

GUISetState(@SW_SHOW)


While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $TabSheet1
            $msg = "Use this tab for creating new files for recording, or opening existing files to add to or playback. You can record with either tab activated, or even this whole program minimized. Each time you record a new click, your file is automatically updated and saved."
            TrayTip("Recording", $msg, 15)
        Case $TabSheet2
            $msg = "On this tab you can play back recorded files. Recording still works in this tab, and clicks will be added at the end of the currently active file."
            TrayTip("Playback", $msg, 15)
        Case $Button1
            Start()
        Case $Button5
            Instructions()
        Case $Button6
            $SaveFile = FileSaveDialog("Save As...", @ScriptDir, "All (*.ini)")
            If Not @error Then
                If StringRight($SaveFile, 4) = ".ini" Then
                    $file = $SaveFile
                Else
                    $file = $SaveFile & ".ini"
                EndIf
            EndIf
        Case $Button7
            $file = FileOpenDialog("Open...", @ScriptDir, "All (*.ini)")
        Case $Button8
            GUICtrlSetData($Input4, 0)
            GUICtrlSetData($Input5, 0)
        Case $Slider1
            $val = GUICtrlRead($Slider1)
            GUICtrlSetData($Label26, " Speed = " & $val / 2) ; leave the space in before Speed, to center it better...
        Case $Radio1 ;And BitAND(GUICtrlRead($Radio1), $GUI_CHECKED) = $GUI_CHECKED ;every other
            If $radio <> 1 Then ; these are so the GUICtrlSetState doesn't keep refreshing the GUI and flickering
                GUICtrlSetState($Input1, $GUI_ENABLE)
                GUICtrlSetState($Input2, $GUI_ENABLE)
                GUICtrlSetData($Input3, "", "")
                GUICtrlSetData($Input3, 1, "")
                GUICtrlSetState($Input3, $GUI_DISABLE)
                $radio = 1; these are so the GUICtrlSetState doesn't keep refreshing the GUI and flickering
            EndIf
        Case $Radio3 ;And BitAND(GUICtrlRead($Radio3), $GUI_CHECKED) = $GUI_CHECKED ; other...
            If $radio <> 3 Then; these are so the GUICtrlSetState doesn't keep refreshing the GUI and flickering
                GUICtrlSetState($Input1, $GUI_ENABLE)
                GUICtrlSetState($Input2, $GUI_ENABLE)
                GUICtrlSetData($Input3, "", "")
                GUICtrlSetData($Input3, 2, "")
                GUICtrlSetState($Input3, $GUI_ENABLE)
                $radio = 3; these are so the GUICtrlSetState doesn't keep refreshing the GUI and flickering
            EndIf
        Case $Radio4 ;And BitAND(GUICtrlRead($Radio4), $GUI_CHECKED) = $GUI_CHECKED ; none...
            If $radio <> 4 Then; these are so the GUICtrlSetState doesn't keep refreshing the GUI and flickering
                GUICtrlSetState($Input1, $GUI_DISABLE)
                GUICtrlSetState($Input2, $GUI_DISABLE)
                GUICtrlSetData($Input3, "", "")
                GUICtrlSetData($Input3, 0, "")
                GUICtrlSetState($Input3, $GUI_DISABLE)
                $radio = 4; these are so the GUICtrlSetState doesn't keep refreshing the GUI and flickering
            EndIf
    EndSwitch
WEnd

Func capture()
    #Region Original *******************************************************************************************************
    Local $pos = MouseGetPos()
    If $file = 1 Then ;$file starts off equal to 1, then gets defined later as "example.ini" in the new, or open dialog. Probably a better way of doing this...
        $msg = "First create a new file to record to, or open an existing one to add to."
        MsgBox(48, "Error", $msg)
    Else
        $x = $pos[0]
        $y = $pos[1]
        IniWrite($file, "Clicks", "$x", $x)
        IniWrite($file, "Clicks", "$y", $y)
        TrayTip("Cursor Position Captured", "Mouse position captured at " & $x & " and " & $y, 10)
    EndIf
    ;http://www.autoitscript.com/wiki/Arrays
    #EndRegion Original *******************************************************************************************************

    ;I'm looking to instead use an array for this.
    Local $x[100]
    Local $y[100]
    ;maybe put some sort of for... to... loop here?
EndFunc   ;==>capture

Func start()
    #Region Original *******************************************************************************************************
    If $file = 1 Then ;$file starts off as 1, then gets defined as "example.ini" in the new, or open dialog later
        $msg = "First create a new file, or open an existing one."
        MsgBox(48, "Error", $msg)
    Else
        $clicks = 1
        $temp = GUICtrlRead($Slider1) ;slider is 1-50, and I want this 1-25 in .5 increments...
        $speed = $temp / 2 ; so it gets halved here
        $offset_x = GUICtrlRead($Input4) ; offset in x direction
        $offset_y = GUICtrlRead($Input5) ; offset in y direction
        $x = IniRead($file, "Clicks", "$x", "Error: not found")
        $y = IniRead($file, "Clicks", "$y", "Error: not found")
        If $x And $y <> "Error: not found" Then
            MouseClick("Primary", $x + $offset_x, $y + $offset_y, $clicks, $speed)
        EndIf
        $msg = "Clicked at " & $x + $offset_x & ", " & $y + $offset_y & " at a speed of " & $speed
        TrayTip("", "", 0)
        TrayTip("Click", $msg, 10)
    EndIf
    #EndRegion Original *******************************************************************************************************
    ; I need to read the array from the .ini file here.

EndFunc   ;==>start

Func slow()
    ;add this once I have the main part up and running
    ;same as capture but with $speed/2
EndFunc   ;==>slow

Func fast()
    ;add this once I have the main part up and running
    ;same as capture but with $speed*2
EndFunc   ;==>fast

Func Instructions()
    $msg = "This program records, and plays your mouse clicks back to you. " & _
            "Optionally at a faster (or slower) speed, with another click inserted in between at a user-set interval, or offset in both the x and or y direction. " & _
            "Once your file is opened, you can add to it at any time. Ctrl + Spacebar records a mouseclick at the current cursor location. " & _
            "Pressing PgUp or PgDn will record the following mouseclick with a doubled, or halfed, speed value. " & _
            "The speed value comes into play when you play the recording back. When the mousclicks are recorded at a higher speed value, " & _
            "the mouse will move to that spot much quicker. The speed at max is limited only by the speed of your computer. Note that the if the speed " & _
            "slider is already maxed out on playback, it will not go any faster, regardless of the recorded value." & @CRLF & _
            "The recordings are saved as standard .ini configuration files. These can be fully edited by using notepad or the like." & _
            "You can also copy and paste the code into AutoIt SciTe for further editing ."
    MsgBox(64, "Instructions", $msg)
EndFunc   ;==>Instructions

Func Optional_Click()
    $pos = MouseGetPos()
    If IsArray($pos) Then
        GUICtrlSetData($Input1, $pos[0])
        GUICtrlSetData($Input2, $pos[1])
        TrayTip("Optional Click", "Optional click value updated", 5)
    Else
        TrayTip("Error", "Cannot grap mouse position", 5)
    EndIf
EndFunc   ;==>Optional_Click

Func Close()
    $exit = True
    Exit
EndFunc   ;==>Close
Lastly, is there any significance to the variable $i ? I see it used all the time in arrays. What does it stand for? Edited by danielmohr91
Link to comment
Share on other sites

Add a new key to your ini section so it will look like:

[MousePos]

count=2

pos1=12,150

pos2=18,155

You will have the 2 coordinates separated by a comma (pos1=$x,$y); count will be the last number in your list (pos2

Everytime you get the mouse position, read the "counter" value, add 1 to that value, write the new value for "counter", and write a new ini key whose name will be pos&counter.

Ex:

- Grab mouse coordinates

- counter = 2

- counter +1 = 3

- write new value (3) in the ini file

- write new key pos3 = coordinates

...

No need to use any arrays here since you are writing to an ini file.

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Link to comment
Share on other sites

Add a new key to your ini section so it will look like:

[MousePos]

count=2

pos1=12,150

pos2=18,155

You will have the 2 coordinates separated by a comma (pos1=$x,$y); count will be the last number in your list (pos2

Everytime you get the mouse position, read the "counter" value, add 1 to that value, write the new value for "counter", and write a new ini key whose name will be pos&counter.

Ex:

- Grab mouse coordinates

- counter = 2

- counter +1 = 3

- write new value (3) in the ini file

- write new key pos3 = coordinates

...

No need to use any arrays here since you are writing to an ini file.

Brilliant! Thank you both for you help!
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...