Using variables multiple times in one loop Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Beast-l-Lucky
Posts: 7
Joined: 23 Jan 2022, 07:03

Using variables multiple times in one loop  Topic is solved

Post by Beast-l-Lucky » 23 Jan 2022, 08:11

Hi, Im currently working on compacting a script thats almost 7000 lines long and it isnt going as good as i was expecting. I made an animation using "gui, add, text," and "guicontrol, hide," and it worked perfectly but i wanted to compact it down a bit. Thats when i tought of the idea that using another variable i can put it inside a loop and replace the repeating lines with a loop and the coordinates (only those change along the entire script) can be replaced by a variable and I can subtract or add 3 so it changes position. But Im stuck with an error message that says "Error: The same variable cannot be used for more than one control." and Im perplexed on what should I do cause i tried everything. Here is the compacted version of the code.

Code: Select all

#SingleInstance, force
gui, new, , Animmation
gui, font, cFFFFFF s14.4999 w800, Consolas
gui, color, black
gui, -resize,
gui, add, button, y1 x999999999 default w1 h1, .
gui, show, maximize
sleep, 150
var1 = 1357
loop,
{
    If (var1 < 7)
    {
        Break
    }
    Else
    {
        guicontrol, hide, 1text0006
        sleep, 1
        gui, add, text, +disabled y182 x%var1% v1text0000, 07de7c99c853215f85b946041ace5308
        sleep, 125
        var1 -= 3
        sleep, 125
        gui, add, text, +disabled y182 x%var1% v1text0001, 07de7c99c853215f85b946041ace5308
        sleep, 1
        var1 -= 3
        guicontrol, hide, 1text0000
        sleep, 1
        gui, add, text, +disabled y182 x%var1% v1text0002, 07de7c99c853215f85b946041ace5308
        sleep, 1
        var1 -= 3
        guicontrol, hide, 1text0001
        sleep, 1
        gui, add, text, +disabled y182 x%var1% v1text0003, 07de7c99c853215f85b946041ace5308
        sleep, 1
        var1 -= 3
        guicontrol, hide, 1text0002
        sleep, 1
        gui, add, text, +disabled y182 x%var1% v1text0004, 07de7c99c853215f85b946041ace5308
        sleep, 1
        var1 -= 3
        guicontrol, hide, 1text0003
        sleep, 1
        gui, add, text, +disabled y182 x%var1% v1text0005, 07de7c99c853215f85b946041ace5308
        sleep, 1
        var1 -= 3
        guicontrol, hide, 1text0004
        sleep, 1
        gui, add, text, +disabled y182 x%var1% v1text0006, 07de7c99c853215f85b946041ace5308
        sleep, 1
        var1 -= 3
        guicontrol, hide, 1text0005
        sleep, 1
    }
}
return,

button.:
{
    ExitApp, animmation.ahk
}

guiclose:
ExitApp,
I have put a peice of theoriginal code I am trying to compact linked to this post. (The text variables have problem (And it doesnt matter that i have used 1text0000 in the compact version and 2text0000 in the extended version but the main is to make it working (And the hash-es have nothing to do but its just a placeholder)))
Thank you everybody that helped in advance!
Attachments
animationtestchamber.ahk
The extended verrsion Im trying to compact
(46.15 KiB) Downloaded 22 times

User avatar
mikeyww
Posts: 26601
Joined: 09 Sep 2014, 18:38

Re: Using variables multiple times in one loop

Post by mikeyww » 23 Jan 2022, 08:46

Since you run your loop infinitely, it keeps trying to use the same variables. Instead, run your loop a finite number of times. Each variable name in the GUI (control name) must be unique.

Code: Select all

Loop, 4
 Gui, Add, Text, vv%A_Index%, %A_Index%
Gui, Show

Beast-l-Lucky
Posts: 7
Joined: 23 Jan 2022, 07:03

Re: Using variables multiple times in one loop

Post by Beast-l-Lucky » 23 Jan 2022, 10:26

mikeyww wrote:
23 Jan 2022, 08:46
Since you run your loop infinitely, it keeps trying to use the same variables. Instead, run your loop a finite number of times. Each variable name in the GUI (control name) must be unique.

Code: Select all

Loop, 4
 Gui, Add, Text, vv%A_Index%, %A_Index%
Gui, Show
Thank you I made a lot of fixes thanks to this and this is the semi-final product:

Code: Select all

#SingleInstance, force
gui, new, , Animmation
gui, font, cFFFFFF s14.4999 w800, Consolas
gui, color, black
gui, -resize,
gui, add, button, y1 x999999999 default w1 h1, .
gui, show, maximize
sleep, 150
var1 = 1357
var2 = 100000000
var3 = 1
loop,
{
    If (var1 < 7)
    {
        Break
    }
    Else
    {
        gui, add, text, +disabled y182 x%var1% vv%var3%, 07de7c99c853215f85b946041ace5308 
        sleep, 1
        var1 -= 3
        guicontrol, hide, %var2%
        var2 -= 5
        sleep, 1
        gui, add, text, +disabled y182 x%var1% vv%var2%, 07de7c99c853215f85b946041ace5308
        sleep, 1
        var1 -= 3
        guicontrol, hide, %var3%
        var3 += 5
        sleep, 1
        gui, add, text, +disabled y182 x%var1% vv%var3%, 07de7c99c853215f85b946041ace5308 
        sleep, 1
        var1 -= 3
        guicontrol, hide, %var2%
        var2 -= 5
        sleep, 1
        gui, add, text, +disabled y182 x%var1% vv%var2%, 07de7c99c853215f85b946041ace5308
        sleep, 1
        var1 -= 3
        guicontrol, hide, %var3%
        var3 += 5
        sleep, 1
        gui, add, text, +disabled y182 x%var1% vv%var3%, 07de7c99c853215f85b946041ace5308 
        sleep, 1
        var1 -= 3
        guicontrol, hide, %var2%
        var2 -= 5
        sleep, 1
        gui, add, text, +disabled y182 x%var1% vv%var2%, 07de7c99c853215f85b946041ace5308
        sleep, 1
        var1 -= 3
        guicontrol, hide, %var3%
        var3 += 5
        sleep, 1
        gui, add, text, +disabled y182 x%var1% vv%var3%, 07de7c99c853215f85b946041ace5308
        sleep, 1
        var1 -= 3
        guicontrol, hide, %var2%
        var2 -= 5
        sleep, 1
        gui, add, text, +disabled y182 x%var1% vv%var2%, 07de7c99c853215f85b946041ace5308
        sleep, 1
        var1 -= 3
        guicontrol, hide, %var3%
        var3 += 5
        sleep, 1
    }
}
return,

button.:
{
    ExitApp, animmation.ahk
}

guiclose:
ExitApp,
But now this has one major problem. For some wierd reason it leaves behind a glitching tail and I cant fid out why. If you could help me I would appreciate it a lot.
Thank you
Last edited by Beast-l-Lucky on 23 Jan 2022, 10:34, edited 1 time in total.

User avatar
mikeyww
Posts: 26601
Joined: 09 Sep 2014, 18:38

Re: Using variables multiple times in one loop

Post by mikeyww » 23 Jan 2022, 10:42

In your script, variable names start with "v", but that is not how you are using GuiControl.

Try this one.

Code: Select all

str  = 07de7c99c853215f85b946041ace5308
x    = 1300
y    = 182
incr = 10
Gui, Font, cFFFFFF s14 w800, Consolas
Gui, Color, Black
Gui, Add, Text, x%x% y%y% vtext1, %str%
Gui, Show, Maximize
Loop, % x / incr {
 GuiControl, Move, text1, % "y" y " x" x - (A_Index * incr)
 Sleep, 50
}

Beast-l-Lucky
Posts: 7
Joined: 23 Jan 2022, 07:03

Re: Using variables multiple times in one loop

Post by Beast-l-Lucky » 23 Jan 2022, 13:53

mikeyww wrote:
23 Jan 2022, 10:42
In your script, variable names start with "v", but that is not how you are using GuiControl.

Try this one.

Code: Select all

str  = 07de7c99c853215f85b946041ace5308
x    = 1300
y    = 182
incr = 10
Gui, Font, cFFFFFF s14 w800, Consolas
Gui, Color, Black
Gui, Add, Text, x%x% y%y% vtext1, %str%
Gui, Show, Maximize
Loop, % x / incr {
 GuiControl, Move, text1, % "y" y " x" x - (A_Index * incr)
 Sleep, 50
}
Thank you but I already solved it and progressed further using this solution:

Code: Select all

#SingleInstance, force
gui, new, , Animmation
gui, font, cFFFFFF s14.4999 w800, Consolas
gui, color, black
gui, -resize,
gui, add, button, y1 x999999999 default w1 h1, .
gui, show, maximize
sleep, 150
;Variable settings set to match Line 1
var1h = 36
var2h = 1000000000
var3h = 1
var4h = 182
var5text = 07de7c99c853215f85b946041ace5308
;Horizontal movement subroutine (re-useable)
Horizontal:
{
    loop,
    {
        If (var1h < 16)
        {
            Break
        }
        Else
        {
            gui, add, text, +disabled y%var4h% x%var1h% v%var3h%, %var5text%
            sleep, 1
            var1h -= 3
            guicontrol, hide, %var2h%
            var2h -= 5
            sleep, 1
            gui, add, text, +disabled y%var4h% x%var1h% v%var2h%, %var5text%
            sleep, 1
            var1h -= 3
            guicontrol, hide, %var3h%
            var3h += 5
            sleep, 1
        }
    }
}
;Variable settings set to match Line 2
var1h = 36
var2h = 2000000000
var3h = 4
var4h = 212
var5text = 754117d49a68dcbda9f4587308081898
Gosub, Horizontal

button.:
{
    ExitApp, animmation.ahk
}

guiclose:
ExitApp,
But I would like to ask you to explain to me how it works because it seems rather smart way of compacting it. Write me a private message if you have the time.

User avatar
mikeyww
Posts: 26601
Joined: 09 Sep 2014, 18:38

Re: Using variables multiple times in one loop

Post by mikeyww » 23 Jan 2022, 13:56

My script works by displaying a single text control, and then moving the control every 50 ms.

Beast-l-Lucky
Posts: 7
Joined: 23 Jan 2022, 07:03

Re: Using variables multiple times in one loop

Post by Beast-l-Lucky » 23 Jan 2022, 14:00

mikeyww wrote:
23 Jan 2022, 13:56
My script works by displaying a single text control, and then moving the control every 50 ms.
I understand the first part but i would like to know more indepth about how this moving works because Im not really good at using ahk (Still learning)

Code: Select all

Loop, % x / incr {
 GuiControl, Move, text1, % "y" y " x" x - (A_Index * incr)
 Sleep, 50
}


User avatar
mikeyww
Posts: 26601
Joined: 09 Sep 2014, 18:38

Re: Using variables multiple times in one loop

Post by mikeyww » 23 Jan 2022, 14:03

The script defines the incr as 10. This is the increment, or number of pixels by which the text control will move with each iteration. Since you have defined the starting x position, the number of iterations is known to be x / incr. During each iteration, the control will be moved to the left by the incr, so the x-value will be x - ([iteration number] * incr). A_Index is the iteration number. After the control is moved, the script waits for 50 ms before proceeding to the next iteration. The y-value is constant.

Beast-l-Lucky
Posts: 7
Joined: 23 Jan 2022, 07:03

Re: Using variables multiple times in one loop

Post by Beast-l-Lucky » 23 Jan 2022, 14:07

mikeyww wrote:
23 Jan 2022, 14:03
The script defines the incr as 10. This is the increment, or number of pixels by which the text control will move with each iteration. Since you have defined the starting x position, the number of iterations is known to be x / incr. During each iteration, the control will be moved to the left by the incr, so the x-value will be x - ([iteration number] * incr). A_Index is the iteration number. After the control is moved, the script waits for 50 ms before proceeding to the next iteration.
Thank you
Is there a way for me to reuse this with like a gosub function or somewhat like that?


Edit:
And another question
The code i shared previously where I used gosub command to reuse a piece of code is giving me the error of reusing variables but I already fixed the same issue before and it theoretically should work

User avatar
mikeyww
Posts: 26601
Joined: 09 Sep 2014, 18:38

Re: Using variables multiple times in one loop

Post by mikeyww » 23 Jan 2022, 16:30

An example is below. You can post any revisions for more feedback.

Code: Select all

str  = 07de7c99c853215f85b946041ace5308
x1   = 1000
x2   = 200
y    = 182
incr = 20
wait = 20
Gui, Font, cFFFFFF s14 w800, Consolas
Gui, Color, Black
Gui, Add, Text, x%x1% y%y% vtext1, %str%
Gui, Show, Maximize
move("text1", x1, x2, incr, wait)

move(control, x1, x2, incr, wait) {
 sign := (dif := x2 - x1) / adif := Abs(dif)
 Loop, % adif / incr {
  GuiControl, Move, %control%, % "x" x1 + sign * A_Index * incr
  Sleep, wait
 }
}
Or:

Code: Select all

str  = 07de7c99c853215f85b946041ace5308
x1   = 1000
x2   = 200
y1   = 182
y2   = 400
incr = 20
wait = 20
Gui, Font, cFFFFFF s14 w800, Consolas
Gui, Color, Black
Gui, Add, Text, x%x1% y%y1% vtext1, %str%
Gui, Show, Maximize
move("text1", x1, x2, y1, y2, incr, wait)

move(control, x1, x2, y1, y2, incr, wait) {
 signx := (difx := x2 - x1) / adifx := Abs(difx)
 signy := (dify := y2 - y1) / adify := Abs(dify)
 Loop, % adifx / incr {
  x := x1 + signx * A_Index * incr
  y := (ratio := adify / adifx) ? y1 + signy * A_Index * incr * ratio : y1
  GuiControl, Move, %control%, x%x% y%y%
  Sleep, wait
 }
}
The second one needs some adjustments, but it's a general idea.

Beast-l-Lucky
Posts: 7
Joined: 23 Jan 2022, 07:03

Re: Using variables multiple times in one loop

Post by Beast-l-Lucky » 24 Jan 2022, 10:40

mikeyww wrote:
23 Jan 2022, 16:30
An example is below. You can post any revisions for more feedback.

Code: Select all

str  = 07de7c99c853215f85b946041ace5308
x1   = 1000
x2   = 200
y    = 182
incr = 20
wait = 20
Gui, Font, cFFFFFF s14 w800, Consolas
Gui, Color, Black
Gui, Add, Text, x%x1% y%y% vtext1, %str%
Gui, Show, Maximize
move("text1", x1, x2, incr, wait)

move(control, x1, x2, incr, wait) {
 sign := (dif := x2 - x1) / adif := Abs(dif)
 Loop, % adif / incr {
  GuiControl, Move, %control%, % "x" x1 + sign * A_Index * incr
  Sleep, wait
 }
}
Or:

Code: Select all

str  = 07de7c99c853215f85b946041ace5308
x1   = 1000
x2   = 200
y1   = 182
y2   = 400
incr = 20
wait = 20
Gui, Font, cFFFFFF s14 w800, Consolas
Gui, Color, Black
Gui, Add, Text, x%x1% y%y1% vtext1, %str%
Gui, Show, Maximize
move("text1", x1, x2, y1, y2, incr, wait)

move(control, x1, x2, y1, y2, incr, wait) {
 signx := (difx := x2 - x1) / adifx := Abs(difx)
 signy := (dify := y2 - y1) / adify := Abs(dify)
 Loop, % adifx / incr {
  x := x1 + signx * A_Index * incr
  y := (ratio := adify / adifx) ? y1 + signy * A_Index * incr * ratio : y1
  GuiControl, Move, %control%, x%x% y%y%
  Sleep, wait
 }
}
The second one needs some adjustments, but it's a general idea.
Its nice from you to help me but Im not that good or not that advanced in Autohotkey to understand this code and how to use it properly. I would like to ask you to explain it on Discord. You can find me there with the tag of LuckyGamer85#3397. Thank you

User avatar
mikeyww
Posts: 26601
Joined: 09 Sep 2014, 18:38

Re: Using variables multiple times in one loop

Post by mikeyww » 24 Jan 2022, 10:58

I added comments below.

Code: Select all

str  = 07de7c99c853215f85b946041ace5308 ; Your string
x1   = 1000                             ; Starting x-value
x2   = 200                              ; Ending x-value
y    = 182                              ; Y-value
incr = 20                               ; Number of pixels to be moved with each iteration
wait = 20                               ; Number of milliseconds to wait between moves
Gui, Font, cFFFFFF s14 w800, Consolas
Gui, Color, Black
Gui, Add, Text, x%x1% y%y% vtext1, %str%
Gui, Show, Maximize
move("text1", x1, x2, incr, wait)       ; Move the named control from x1 to x2 over incr steps

move(control, x1, x2, incr, wait) {
 sign := (dif := x2 - x1) / adif := Abs(dif)
 Loop, % adif / incr {                  ; Number of iterations is x difference / per-move increment
  GuiControl, Move, %control%, % "x" x1 + sign * A_Index * incr ; Move the control to a new x position
  Sleep, wait                                                   ; Wait a bit between moves
 }
}

Beast-l-Lucky
Posts: 7
Joined: 23 Jan 2022, 07:03

Re: Using variables multiple times in one loop

Post by Beast-l-Lucky » 24 Jan 2022, 11:10

mikeyww wrote:
24 Jan 2022, 10:58
I added comments below.

Code: Select all

str  = 07de7c99c853215f85b946041ace5308 ; Your string
x1   = 1000                             ; Starting x-value
x2   = 200                              ; Ending x-value
y    = 182                              ; Y-value
incr = 20                               ; Number of pixels to be moved with each iteration
wait = 20                               ; Number of milliseconds to wait between moves
Gui, Font, cFFFFFF s14 w800, Consolas
Gui, Color, Black
Gui, Add, Text, x%x1% y%y% vtext1, %str%
Gui, Show, Maximize
move("text1", x1, x2, incr, wait)       ; Move the named control from x1 to x2 over incr steps

move(control, x1, x2, incr, wait) {
 sign := (dif := x2 - x1) / adif := Abs(dif)
 Loop, % adif / incr {                  ; Number of iterations is x difference / per-move increment
  GuiControl, Move, %control%, % "x" x1 + sign * A_Index * incr ; Move the control to a new x position
  Sleep, wait                                                   ; Wait a bit between moves
 }
}

Code: Select all

#SingleInstance, force

gui, new, , Animmation
gui, font, cFFFFFF s14.4999 w800, Consolas
gui, color, black
gui, -resize,
gui, add, button, y1 x999999999 default w1 h1, .
gui, show, maximize
sleep, 150
;Variable settings set to match Line 1
var1h = 36
var2h = 1000000000
var3h = 1
var4h = 182
var5text = 07de7c99c853215f85b946041ace5308
;Horizontal movement subroutine (re-useable)
Horizontal:
{
    loop,
    {
        If (var1h < 16)
        {
            Break
        }
        Else
        {
            gui, add, text, +disabled y%var4h% x%var1h% v%var3h%, %var5text%
            sleep, 1
            var1h -= 3
            guicontrol, hide, %var2h%
            var2h -= 5
            sleep, 1
            gui, add, text, +disabled y%var4h% x%var1h% v%var2h%, %var5text%
            sleep, 1
            var1h -= 3
            guicontrol, hide, %var3h%
            var3h += 5
            sleep, 1
        }
    }
}
;Variable settings set to match Line 2
var1h = 36
var2h = 2000000000
var3h = 4
var4h = 212
var5text = 754117d49a68dcbda9f4587308081898
Gosub, Horizontal


button.:
{
    ExitApp, animmation.ahk
}

guiclose:
ExitApp,
I would like you to kind of fix this code because it always gives me the same error wich is that i cant use the same variable twice but it was made to fix this but it still gives me in the part where its Gosub, Horizontal there it goes back to the Horizontal section again but with the variable's values changed to match the second line's alignment. If you can fix this version I would be really thankful so I can reuse it in the remaining of the script. Thank you in advance

User avatar
mikeyww
Posts: 26601
Joined: 09 Sep 2014, 18:38

Re: Using variables multiple times in one loop

Post by mikeyww » 24 Jan 2022, 11:24

If you do not actually believe that you are reusing variable names, you can add a MsgBox line that displays the variable name. You will then see the bug in your script.

Post Reply

Return to “Ask for Help (v1)”