Jump to content

Double check understanding of program code


OldManJack
 Share

Recommended Posts

Ok. Let me run through the commands and functions to make sure I understand their purpose.

Opt("OnExitFunc", "endscript"); So that the file gest closed on exit

Opt("OnExitFunc" , "endscript") Is a autosetoption that performs a function as the program in closing, in this case, one that closes a file by the name of $file, previosuly defined as C:\test.text.

AdLibEnable("_GetPos", 30); Run the _GetPos() function ever 30 ms

I'm a little fuzzy on the AdLibEnable() function. I'm guessing it works a bit like a self contained 'always do this' statement, yes? It executes the function _getPos eveyr 30 ms. (The "_" is a personal method of organizing Functionnames or a mostly universal thing that I'll see often enough that I should know it? I also noticed that most of the forum chatter talking about AdLibEnable() and AdLibDisable() are doing so in relationship to error messages, which I don't quite underdtand yet either. Why would you need a script running continiously to check for errors and call a msgbox? People are also using phrases like "saving the CPU," which sounds like something I should know about.

$file = "c:\test.txt"; Specify the text file for writing co-ords to
$file = FileOpen($file, 9); Open it, and test to see that it exists
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

The next few lines assign the variable $file to c:\test.txt, tests its existance, and if it doesn't exist, then creates an error message and exits the script (The 'Exit' function, right?) What I don't know about is the "-1" part of "If $file = -1 Then" I'd halfway expect to see a "0" (zero) for negative return. Why -1 specifically? Or possibly an easier question for you to answer, what should I have read?

While 1
    Sleep(10); Hang around to keep things running
WEnd

Without the While-WEnd loop, the script would pause or exit?

Func _GetPos(); Function to get the mouses position, and write it to the text file
$pos = MouseGetPos()
FileWriteLine($file, $pos[0] & "-" & $pos[1])
EndFunc;==> _GetPos()

Begins the _GetPos() function, assigns the $pos variable to MouseGetPos(), then uses the FileWriteLine command to write into $file, previously defined as c:\test.txt in the format of ###-###.

The same function still run if you used $cheese = MouseGetPos() and FileWriteLine($Cheese[0] & "-" & $Cheese[1]) ?

Hopefully, this will set me straight to finish the thing up. Thanks much.

Link to comment
Share on other sites

First off, let me just say that you are currently my favorite noobie. :) You seem like someone who genuinely wants to learn the language and how it works, which is something that I'm sure every helpful person on these forums appreciates. We never like to see people come in and just whine at us to write their scripts for them, so a post like this is refreshing. At least for me.

Ok. Let me run through the commands and functions to make sure I understand their purpose.

Opt("OnExitFunc", "endscript"); So that the file gest closed on exit

Opt("OnExitFunc" , "endscript") Is a autosetoption that performs a function as the program in closing, in this case, one that closes a file by the name of $file, previosuly defined as C:\test.text.

I don't see the "endscript" function defined in the code you supplied, so I'm going to have to take your word for it on what it does, but you have the idea right. Basically, this option changes the function that will run when the script exits, by default it looks for a function called OnAutoItExit. If you had a function named "Goodnight" that you wanted to run when the script exited, you could put that instead of "endscript".

Working example:

Opt('OnExitFunc', 'FunctionThatDoesntExist')
Opt('OnExitFunc', 'Goodnight')
; You can also try deleting either of the lines above and running the script to see what happens

Func Goodnight()
    MsgBox(0, 'Goodnight function', 'The script has finished and is now exiting, this is the Goodnight function.')
EndFunc

Func OnAutoItExit()
    MsgBox(0, 'Default exit function', 'The script has finished and is now exiting, this is the default function that runs when a script exits.')
EndFunc

AdLibEnable("_GetPos", 30); Run the _GetPos() function ever 30 ms

I'm a little fuzzy on the AdLibEnable() function. I'm guessing it works a bit like a self contained 'always do this' statement, yes? It executes the function _getPos eveyr 30 ms.

Basically the AdlibEnable function is a way to constantly execute a function, outside of the normal program loop, at a set interval. Like the comment in this example says, it will run that function every 30 milliseconds. The limitation of this function is that only one "Adlib" can be running at a time. If you use AdlibEnable again, it will start to run the newly defined function at the set interval.

Working example:

AdlibEnable('One', 1000)
Sleep(5000); Do nothing for 5 seconds, the adlib functions are not affected by this and will continue to run

AdlibEnable('Two', 1500); Changing the adlib function
Sleep(5000)

AdlibDisable(); This will stop any more adlib functions from running

Sleep(2000)

Func One()
    ToolTip('This is function ONE, it will run every second. Random number: ' & Random(1, 10, 1))
EndFunc

Func Two()
    ToolTip('Function TWO, this will run every 1.5 seconds. Random number: ' & Random(1, 10, 1))
EndFunc

(The "_" is a personal method of organizing Functionnames or a mostly universal thing that I'll see often enough that I should know it?

It's mostly a personal thing, we like to prefix our own functions with "_" to differentiate from built in AutoIt functions. It's also a bit of backwards compatibility I guess you could say, in that if you had a function named "MouseGetPos" before that function had been added to the language, then your script would error if you tried to run it with a newer version that had added that function. Changing the name to "_MouseGetPos" means you shouldn't ever have to worry about that problem.

I also noticed that most of the forum chatter talking about AdLibEnable() and AdLibDisable() are doing so in relationship to error messages, which I don't quite underdtand yet either. Why would you need a script running continiously to check for errors and call a msgbox?

I'm not quite sure about that either.. I don't think I've seen people mention that, could you point to a post where you've seen this? Cus I can't understand that logic either.

People are also using phrases like "saving the CPU," which sounds like something I should know about.

This is probably in reference to the fact that a function that is fairly CPU intensive (makes the computer work harder than normal) being called continuously in a tight loop (like using AdlibEnable('functionName', 1), it runs every 1 millisecond, so very often, very fast) will make your computer run slowly. I'm sure you can understand that if your computer has to generate a random number, read the colour of a pixel, get the coordinates of your mouse, plus make you coffee every millisecond, it's going to get a little overworked (the coffee part was a joke, I need to spice up all this text somehow.. even if it IS with bad jokes).

$file = "c:\test.txt"; Specify the text file for writing co-ords to
$file = FileOpen($file, 9); Open it, and test to see that it exists
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

The next few lines assign the variable $file to c:\test.txt, tests its existance, and if it doesn't exist, then creates an error message and exits the script (The 'Exit' function, right?) What I don't know about is the "-1" part of "If $file = -1 Then" I'd halfway expect to see a "0" (zero) for negative return. Why -1 specifically?

You know what? I'm not sure why it's -1 either. 0 for a negative return would have been a better idea, especially since 0 evaluates to false and everything else (including "-1") evaluates to true, which means you could just write code like "If Not $file Then" or "If $file Then". But it's been -1 for a long time and changing it now would just break a lot of scripts that expect this behaviour, so it's probably going to stay that way for a long time.

While 1
    Sleep(10); Hang around to keep things running
WEnd

Without the While-WEnd loop, the script would pause or exit?

It would exit. If there's nothing to stop a script from continuing, it just hits the end of the file and closes. This loop is an infinite loop. While X is not false: do loop. X = 1; 1 is not false (0 is false, empty string ("") is false, and False (the special keyword) is false). X will always be 1, so X will always be true, so always loop. The Sleep(10) is in the same vein as what I discussed above about not overworking the CPU. For example, even though there's nothing in this loop:

While 1
; Do nothing
WEnd

It will make the CPU run hard, because it's going through that loop as fast as it possibly can, trying to finish it, which it will never do because it's infinite. 1 will never change, so the loop will never end, but the script doesn't know that when it starts so it's just working it's little buns off trying to get it's job done. Adding the Sleep(10) in there will make the script pause in the middle of this loop for 10 milliseconds, this gives the CPU plenty of time to catch a breath before it continues doing.. well, nothing, but it's still exhausting. Ideally in a loop like that you could probably get away with having a very high sleep time, this would make the script even easier on the CPU.

Func _GetPos(); Function to get the mouses position, and write it to the text file
$pos = MouseGetPos()
FileWriteLine($file, $pos[0] & "-" & $pos[1])
EndFunc;==> _GetPos()

Begins the _GetPos() function, assigns the $pos variable to MouseGetPos(), then uses the FileWriteLine command to write into $file, previously defined as c:\test.txt in the format of ###-###.

The same function still run if you used $cheese = MouseGetPos() and FileWriteLine($Cheese[0] & "-" & $Cheese[1]) ?

Hehe, cheese. Yes, you are right. $pos is a variable, and can be named anything so long as it starts with $ and is followed by letters, numbers or the underscore (_). So anything like $fred, $bed3, $_important, and even $123 will work.

Hopefully, this will set me straight to finish the thing up. Thanks much.

I hope that I have provided some straightening and haven't curled you up more than you were before... okay, that analogy kinda ran away from me but I'm sure you get the idea. :)
Link to comment
Share on other sites

Jack (me) - Hopefully, this will set me straight to finish the thing up. Thanks much.

Saundwers - I hope that I have provided some straightening and haven't curled you up more than you were before... okay, that analogy kinda ran away from me but I'm sure you get the idea.

It did. You haven't. And thank you.

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...