Jump to content
Sign in to follow this  
Jason786

Can someone explain what this does?

Recommended Posts

Jason786

Global $Active = 0
While 1
    If $Active = 1 Then

Hey, so I know global declares a variable, I found this tutorial script on the internet so $Active must be the variable right

So how does the script get from $Active = 0 to $Active = 1 , what does the 1 mean in this case?

Also does it have to be While 1 or can it be just While?

Thanks :mellow:

Share this post


Link to post
Share on other sites
AlmarM

The $Active variable isn't changed in your script. It will stay 0 forever. Until you change it using (for example) $Active = 1.

The While loop keeps repeating itself as a statement is true. In this case While 1, 1 will always be true, so the loop wont stop until you exit the script or use an ExitLoop.

Example 1

HotKeySet("{HOME}", "SetActive") ; Press 'home' to set $Active to 1

Global $Active = 0

While 1
    If $Active = 1 Then
        ConsoleWrite("Something is now active." & @CRLF)
    EndIf
    
    If $Active = 0 Then
        ConsoleWrite("Something isn't active." & @CRLF)
    EndIf
    
    Sleep(1000)
WEnd

Func SetActive()
    $Active = 1
EndFunc

Example 2

HotKeySet("{HOME}", "StopLoop") ; Press 'home' to stop the loop.

Global $Statement = 1

While $Statement = 1
    ConsoleWrite("While loop is active." & @CRLF)
    
    Sleep(1000)
WEnd

ConsoleWrite("While loop stopped." & @CRLF)

Func StopLoop()
    $Statement = 0
EndFunc

Does this help you?

AlmarM :mellow:


Minesweeper

A minesweeper game created in autoit, source available.

_Mouse_UDF

An UDF for registering functions to mouse events, made in pure autoit.

2D Hitbox Editor

A 2D hitbox editor for quick creation of 2D sphere and rectangle hitboxes.

Share this post


Link to post
Share on other sites
rudi

Hi.

Global $Active = 0
While 1
    If $Active = 1 Then
; ...

Hey, so I know global declares a variable, I found this tutorial script on the internet so $Active must be the variable right

So how does the script get from $Active = 0 to $Active = 1 , what does the 1 mean in this case?

Also does it have to be While 1 or can it be just While?

Thanks :mellow:

You could have read the autoit help file, topic "while". :party:

While <condition>

...

WEnd

is looping again and again, as long as "<condition>" is true. Here <condition> is "1", so the loop will loop for ever.

Usually a statement like "If $Active = <something> then ExitLoop" will break out of this infinite looping.

that's all :P

Regards, Rudi.


Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Share this post


Link to post
Share on other sites
Jason786

Thanks :mellow:

So if I do

If $Active = 1 Then
        $SquarePosition = PixelSearch(0, 0, 1696, 1036, 0x941616, 3)

When does Active turn to 1? Because if Active is 1 right, so how does it change to 1? Hope you guys understand what I'm asking :S

edit: oh I see, so you gotta make active = something. thanks :P

HotKeySet("{a}", "_Start")
Func _Start()
    $Active = 1
EndFunc   ;==>_Start

Some like that would be right then?

Edited by Jason786

Share this post


Link to post
Share on other sites
Melba23

Jason786,

Please take this advice in the friendly sense I am giving it! :mellow:

It is obvious that you have very little idea about coding and program structure. Nothing wrong with that, we all started there! :party: Could I suggest that before going any further, you read the Help file (at least the first few sections - Using AutoIt, Tutorials and the first couple of References) - this will help you enormously. You should also look at the excellent tutorials that you will find here and here - you will find other tutorials in the Wiki (the link is at the top of the page). There are even video tutorials on YouTube if you prefer watching to reading. :P

In the particular case you mention above, $Active will only turn to 1 (or 0 or any other value) when your script tells it to - like here: :party:

; Set our variable
$Active = 1

; Set a counter to zero
$iCount = 0

; Start a loop which continues until $Active is no longer 1
While $Active = 1

    ; Increase the count
    $iCount += 1

    ; Check if the count has reached a certaib value.  if it has then set $Active to 0
    If $iCount = 10 Then $Active = 0

; Keep going round the loop if $Active = 1
WEnd

; To get here, $Active must = 0 - and we changed it when $iCount = 10
; So let us see if we are right
MsgBox(0, "End", "$iCount = " & $iCount)

I hope you can follow what is happening in the script and that it answers your question.

I really do recommend those tutorials - getting a good base knowledge of coding will serve you well in future. :party:

M23

Edit: Your edit shows you are on the right lines already! :party:

Edited by Melba23

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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
niubbone

Since i'm pretty new to it i can perfectly understand what you are asking.

Global $Active = 0

While 1

If $Active = 1 Then

As said, $active will stay 0 forever. Now I'll explain your piece of code line by line very clearly.

Global $Active = 0

We state that the Global variable $Active is 0.

While 1

We set an infinite loop. This will keep running infinitely the code below, since there isn't a condition that makes it exit. That is because 1 its like True boolean operator, so it's like While True.

If $Active = 1 Then

This is where you got stuck. We just check continuosly IF THE VARIABLE $Active IS EQUAL TO 1, we DON'T STATE that is equal to 1. That $Active is a condition of the If statement.

To make a more humanlike example:

You want to exit from house, but door it's locked with a key.

Global $Active = Door it's locked

While 1; I keep continuosly pulling the handle to see if door is open

If $Active = Door isn't locked anymore Then; the door it's open and i can go out.

Exit and go out with friends

As you can see, the door won't magically unlock by itself. It needs you to pick the key and open it. So if you put it like this

Global $Active = 0 Door it's locked

$Active = 1; I pick the key and open the door

While 1; Keeping pulling the handle to see if door is open

If $Active = 1 Then If door it's open

Exit and go out with friends

That's it. Sounds ridiculous, i know, but i think it gave the idea :mellow:

Share this post


Link to post
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
Sign in to follow this  

×