Jump to content
Sign in to follow this  
marsEvolve

Need Help Regarding MouseGetPos() and _ArrayAdd()

Recommended Posts

marsEvolve

Hey guys, I'm a new member here but I've known this forum since 2009. 

Sorry for being blunt but I need your help and I'd really appreciate it if you could enlighten me so I can understand my problem by the tiniest bit of detail (tiny details cannot be missed! :D)

So here's what I wanted to do:

  1. To get mouse-coordinates
  2. Store them in an array using _ArrayAdd() e.g. $mousePosArray
  3. Use MouseGetPos() to click "left" in the coordinates inside the array: $mousePosArray
  4. Since it's a for loop I wanted it clicking at both mouse-coordinates at each instance (+increment)

My Script:

#include <misc.au3>
#include <array.au3>
Dim $posArray[2]

MsgBox(64, "???", "Getting mouse coordinates for unit 1..")
Sleep(500)
$unitPos = MouseGetPos()
_ArrayAdd($posArray, $unitPos[0] & ", " & $unitPos[1])
MsgBox(64, "???", "Getting mouse coordinates for unit 2...")
Sleep(500)
$unitPos = MouseGetPos()
_ArrayAdd($posArray, $unitPos[0] & ", " & $unitPos[1])

MouseClick("left", $posArray[0], $posArray[1],1 ,10) 
Sleep(500)
MouseClick("left", $posArray[0], $posArray[1],1, 10)

_ArrayDisplay($posArray)

My Problems:

  1. I've tried using MouseClick() with the following coordinates: $mousePosArray[0], and $mousePosArray[1]. e.g. mouseclick("left", $mousePosArray[0], $mousePosArray[1],1,5)
  2. Instead of the mouse moving to the desired coordinates, it moved way farther than the coordinates inside _ArrayDisplay (well, it's not irrelevant isn't it? I used this to make sure that the coordinates were inside the array.)
  3. However, it did click... but I'm obviously not happy since it wasn't the kind of result I wanted. 
  4. Also, could you guys tell me why that, when the 2D interface of _ArrayDisplay() shows up, the coordinates are in [2] and [3], not in both [0] and [1]. I have declared $mousePosArray as: Dim $mousePosArray[2], juuuuuusst below the #includes.

BTW, it is my first time using this function so I hope you be understanding :)

IF POSSIBLE, I ASK THAT AN EXPERT HELP ME OUT. BUT EVERYONE CAN START THE BALL ROLLIN'! :D

Edited by marsEvolve

Share this post


Link to post
Share on other sites
computergroove

If you've already paved the way with some code then we certainly appreciate it when you share it. It takes a lot of time to make it from scratch and it is often times easier to see where there is a mistake in your code and fix it. 


Get Scite to add a popup when you use a 3rd party UDF -> http://www.autoitscript.com/autoit3/scite/docs/SciTE4AutoIt3/user-calltip-manager.html

Share this post


Link to post
Share on other sites
marsEvolve

If you've already paved the way with some code then we certainly appreciate it when you share it. It takes a lot of time to make it from scratch and it is often times easier to see where there is a mistake in your code and fix it. 

Included my script ^

Share this post


Link to post
Share on other sites
computergroove

You aren't making a 2d array and I cannot remember the syntax. This is what I have made so far. I was reading how to do this earlier today and I cannot find it again. I'm working on it.

#include <array.au3>
#include <Misc.au3>

Global $aArray[0][2];Array to store the mouse click positions in

Local $hDLL = DllOpen("user32.dll"); This is part of the _IsPressed Function
Global $nClicks=0 ;Number of clicks you want to limit the array to accept from MouseGetPos()(optional)

While 1
$pos = MouseGetPos();Gets x and y coordinates of where the mouse is
If _IsPressed("01", $hDLL) Then
    _ArrayAdd($aArray,$pos);first click writes to array line 0, second click writes to array line 1 etc...
    $nClicks = $nClicks + 1
    Clicks()
    While _IsPressed("01",$hDLL)
        Sleep(250)
    WEnd
    Sleep(100)
EndIf
WEnd

Func Clicks()
    If $nClicks > 5 Then;number of clicks you want x and y coordinates you want to be written to the array
        _ArrayDisplay($aArray);Shows all of the mouse coordinates on the screen
    EndIf
EndFunc

Get Scite to add a popup when you use a 3rd party UDF -> http://www.autoitscript.com/autoit3/scite/docs/SciTE4AutoIt3/user-calltip-manager.html

Share this post


Link to post
Share on other sites
marsEvolve

 

You aren't making a 2d array and I cannot remember the syntax. This is what I have made so far. I was reading how to do this earlier today and I cannot find it again. I'm working on it.

#include <array.au3>
#include <Misc.au3>

Global $aArray[0][2];Array to store the mouse click positions in

Local $hDLL = DllOpen("user32.dll"); This is part of the _IsPressed Function
Global $nClicks=0 ;Number of clicks you want to limit the array to accept from MouseGetPos()(optional)

While 1
$pos = MouseGetPos();Gets x and y coordinates of where the mouse is
If _IsPressed("01", $hDLL) Then
    _ArrayAdd($aArray,$pos);first click writes to array line 0, second click writes to array line 1 etc...
    $nClicks = $nClicks + 1
    Clicks()
    While _IsPressed("01",$hDLL)
        Sleep(250)
    WEnd
    Sleep(100)
EndIf
WEnd

Func Clicks()
    If $nClicks > 5 Then;number of clicks you want x and y coordinates you want to be written to the array
        _ArrayDisplay($aArray);Shows all of the mouse coordinates on the screen
    EndIf
EndFunc

Mouse X-Y coordinates are not recorded inside the _ArrayDisplay. My guess is that you forgot to add $pos[0] and $pos[1].

Well, anyways it's not adding the coordinates inside :(

Edited by marsEvolve

Share this post


Link to post
Share on other sites
computergroove

What is the correct syntax to add the x and y coords to the array with _arrayAdd? I cannot find any examples on the net or forums. MouseGetPos() returns the coords in a 2d array. Why isnt this working?

Edited by computergroove

Get Scite to add a popup when you use a 3rd party UDF -> http://www.autoitscript.com/autoit3/scite/docs/SciTE4AutoIt3/user-calltip-manager.html

Share this post


Link to post
Share on other sites
marsEvolve

What is the correct syntax to add the x and y coords to the array with _arrayAdd? I cannot find any examples on the net or forums. MouseGetPos() returns the coords in a 2d array. Why isnt this working?

#include <misc.au3>
#include <array.au3>
Dim $posArray[3]

MsgBox(64, "???", "Getting mouse coordinates for unit 1..")
Sleep(500)
$unitPos = MouseGetPos()
_ArrayInsert($posArray, 0, $unitPos[0])
_ArrayInsert($posArray, 1, $unitPos[1])
MsgBox(64, "???", "Getting mouse coordinates for unit 2...")
$unitPos = MouseGetPos()
_ArrayInsert($posArray, 2, $unitPos[0])
_ArrayInsert($posArray, 3, $unitPos[1])
Sleep(500)
MouseClick("left", $posArray[0], $posArray[1],1 ,10)
Sleep(500)
MouseClick("left", $posArray[2], $posArray[3],1, 10)

_ArrayDisplay($posArray)

Although it's redundant, it works... I guess I should be happy?

Share this post


Link to post
Share on other sites
Geir1983

Try this. Press "1" to replay your clicks.

#include <Misc.au3>
#include <Array.au3>


HotKeySet("1", "_Replay")

Global Enum $X, $Y
Global $aArray[0][2];Array to store the mouse click positions in

Local $hDLL = DllOpen("user32.dll"); This is part of the _IsPressed Function


While 1
    If _IsPressed("01", $hDLL) Then
        While _IsPressed("01", $hDLL)
            Sleep(10)
        WEnd
        $pos = MouseGetPos();Gets x and y coordinates of where the mouse is
        ReDim $aArray[UBound($aArray)+1][UBound($aArray, 2)]
        $aArray[UBound($aArray)-1][$X] = $pos[0]
        $aArray[UBound($aArray)-1][$Y] = $pos[1]
    EndIf
    Sleep(10)
WEnd

Func _Replay()
    For $idx = 0 To Ubound($aArray)-1
        MouseClick("Left", $aArray[$idx][$X], $aArray[$idx][$Y])
    Next
EndFunc
Edited by Geir1983

Share this post


Link to post
Share on other sites
marsEvolve

 

Try this. Press "1" to replay your clicks.

#include <Misc.au3>
#include <Array.au3>


HotKeySet("1", "_Replay")

Global Enum $X, $Y
Global $aArray[0][2];Array to store the mouse click positions in

Local $hDLL = DllOpen("user32.dll"); This is part of the _IsPressed Function


While 1
    If _IsPressed("01", $hDLL) Then
        While _IsPressed("01", $hDLL)
            Sleep(10)
        WEnd
        $pos = MouseGetPos();Gets x and y coordinates of where the mouse is
        ReDim $aArray[UBound($aArray)+1][UBound($aArray, 2)]
        $aArray[UBound($aArray)-1][$X] = $pos[0]
        $aArray[UBound($aArray)-1][$Y] = $pos[1]
    EndIf
    Sleep(10)
WEnd

Func _Replay()
    For $idx = 0 To Ubound($aArray)-1
        MouseClick("Left", $aArray[$idx][$X], $aArray[$idx][$Y])
    Next
EndFunc

Thanks, now how you do this if I wanted it to repeat 5 times while clicking at different xy mouse coordinates for 5 times as well?

E.G. my game has 5 rounds and I have 5 units available for commence, I want to make it so that the loop would run 5 times whilst clicking all 5 units in each round.

my idea: using the data stored into the array using _ArrayInsert to obtain all 5 unit coordinates (I have a problem with this actually)

For each round to 5 rounds step -1

for each unit to 5 units step -1

mouseclick("left", $unitPosArray[$unit], $unitPosArray[++$unit])  ; x = $unitPosArray[0], y = $unitPosArray[1], from the mousegetpos()  

I thought it would work well but as it turned out, the mouse moved somewhere I had not set the xy coordinates to. 

I tried getting the data from the $unitPosArray via the method _ArrayDisplay($unitPosArray) but the ones that were there were abit different.

i.e. _ArrayInsert($unitPosArray, 0, $unitPos[0]) x-coordinates work fine but when it comes to the y-coordinate values it becomes similar or has the same value as the x-coordinates stored in the array.

I do know that when x = [0] and y = [1] the values inside the array increase as you're putting in new rows. i.e. $unitPos[3], this array originally has 4 rows (of course, from 0 to 4), however, when I looked into it I noticed that, and correct me if I'm wrong, the number of rows increased because of the y-coordinate conditions in the for-loop has already met the value of $unitCount (which is 5 times) so I guess that $unitCount only serves as an iterator (repeat-er). 

TL;DR I'm having trouble inserting the right x-y values inside my array, however, I'm still trying out various methods (and not to forget that I am new to this scripting language). I hope you can help me ASAP.

Share this post


Link to post
Share on other sites
Geir1983

Sorry, this forum has a strict no game automation policy.

Share this post


Link to post
Share on other sites
Melba23

marsEvolve,

my game has 5 rounds and I have 5 units available for commence

Geir1983 is quite right - please read the Forum rules (the link is also at bottom right of each page) - particularly the bit about not discussing game automation - before you post again. Thread locked. :naughty:

M23


Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.
Sign in to follow this  

×