Jump to content

Making custom #include's


 Share

Recommended Posts

Hey guys.

So im trying to learn some AutoIT Scripting, and i thought i'd try to make my own include file.

I've created a simple timer, saved it in a class, and imported that on my main script. However, it does not work.

If anyone would care to help me out, i'd be really happy :mellow:

TimerClass.au3

$Hour = @HOUR
$Min = @MIN
$Sec = @SEC

Func StartTimer($WHour = 0, $WMin = 0, $WSec = 0)
    While 1
        If (@HOUR - $Hour >= $WHour) Then
            If (@MIN - $Min >= $WMin) Then
                If (@SEC - $Sec >= $WSec) Then
                    Return True
                Else
                    Return False
                EndIf
            EndIf
        EndIf
    WEnd
EndFunc

Script to run it:

#include "TimerClass.au3"

$StartA = StartTimer(0,0,10)
While 1
    If $StartA = True Then
        MsgBox(0,"True","True")
    EndIf
WEnd
Link to comment
Share on other sites

Is 0 greater or equal to 10? No - that's why it doesn't work, because you have coded your function badly.

$Sec = @SEC

then

If (@SEC - $Sec >= $WSec) Then

won't this make @SEC - $Sec = 0 ??

You will need to correct your function.

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

@SEC = current second

@Sec = second when it started.

So @SEC - $Sec = seconds it has run.

Also, how would i fix the script?

I've looked at it, but im blanking out.

And i know it's probably poorly coded, but i'm new at this and i need to learn at some point :mellow:

Link to comment
Share on other sites

Hey guys.

So im trying to learn some AutoIT Scripting, and i thought i'd try to make my own include file.

I've created a simple timer, saved it in a class, and imported that on my main script. However, it does not work.

If anyone would care to help me out, i'd be really happy :mellow:

TimerClass.au3

$Hour = @HOUR
$Min = @MIN
$Sec = @SEC

Func StartTimer($WHour = 0, $WMin = 0, $WSec = 0)
    While 1
        If (@HOUR - $Hour >= $WHour) Then
            If (@MIN - $Min >= $WMin) Then
                If (@SEC - $Sec >= $WSec) Then
                    Return True
                Else
                    Return False
                EndIf
            EndIf
        EndIf
    WEnd
EndFunc

Script to run it:

#include "TimerClass.au3"

$StartA = StartTimer(0,0,10)
While 1
    If $StartA = True Then
        MsgBox(0,"True","True")
    EndIf
WEnd

Hi Lancer,

Welcome to the forum! Here i don't understand what you are trying to do but i will tell you what i understand....

your UDF will return False for your current example. So try the below 2 examples you will get the correct answer.

#include "TimerClass.au3"

$StartA = StartTimer(0,0,10)
    If $StartA = False Then
        MsgBox(0,"True","True")
    EndIf

or Try like this:

#include "TimerClass.au3"


$StartA = StartTimer(0,0,0)
    If $StartA = True Then
        MsgBox(0,"True","True")
    EndIf
Edited by Syed23

Thank you,Regards,[font="Garamond"][size="4"]K.Syed Ibrahim.[/size][/font]

Link to comment
Share on other sites

Hi Syed32, and thanks.

I tried to set it to True and False.

Having Timer at 10 and checking to False = OK

Having Timer at 10 and checking to True = Fail

Having Timer at 0 and checking to False = Fail

Having Timer at 0 and checking to True = OK

I also tried setting the values to a negative number.

I've tried using the actual function in a script by itself, where it worked perfectly.

So im not sure if it's the way it's getting handled in the include is messing it up or not.

Also, what i'm trying to do is to:

1. Learn how to create and include my own custom function libraries.

2. Make a timer function that will count until X is reached and then output True.

Link to comment
Share on other sites

You're right - the error is elsewhere. You were starting your function only once, it returned "False" and remained stuck in your script loop.

This will work:

$Hour = @HOUR
$Min = @MIN
$Sec = @SEC

While 1
    $StartA = StartTimer(0,0,5)
    If $StartA = True Then
        MsgBox(0,"True","True")
        Exit
    EndIf
WEnd

Func StartTimer($WHour = 0, $WMin = 0, $WSec = 0)
    If (@HOUR - $Hour >= $WHour) Then
        If (@MIN - $Min >= $WMin) Then
            If (@SEC - $Sec >= $WSec) Then
                Return True
            Else
                Return False
            EndIf
        EndIf
    EndIf
EndFunc

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

You're right - the error is elsewhere. You were starting your function only once, it returned "False" and remained stuck in your script loop.

This will work:

$Hour = @HOUR
$Min = @MIN
$Sec = @SEC

While 1
    $StartA = StartTimer(0,0,5)
    If $StartA = True Then
        MsgBox(0,"True","True")
        Exit
    EndIf
WEnd

Func StartTimer($WHour = 0, $WMin = 0, $WSec = 0)
    If (@HOUR - $Hour >= $WHour) Then
        If (@MIN - $Min >= $WMin) Then
            If (@SEC - $Sec >= $WSec) Then
                Return True
            Else
                Return False
            EndIf
        EndIf
    EndIf
EndFunc

That actually does work. But im clueless why it works.

In my logic sense it shouldn't work, as it keeps setting the timer to 5? And then it would keep recounting from 5.

But apparently not.

Thanks a lot for your assistance :mellow:

Link to comment
Share on other sites

I have removed the While loop from your function so the way it works changed totally - that is why you can't see how it can work.

Also, setting the timer to 5, I did that because to test it I had to wait 10 seconds ... change it to whatever value you want.

This way of doing it is not the most efficient one - have a look at "TimerDiff" function in AutoIt help file, also _Timer_SetTimer in the same file.

Plenty of other ways - no need to reinvent the wheel. Well, if this is only an exercise, then, go ahead and do it your way, but if you need it in a real script - use the ones already made.

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

Maybe im mistaken about how While loops work. I thought they would keep on repeating themselves while being active. But apparently not.

Thanks a lot for your help :mellow:

I actually searched for functions similar to what i just made, but couldn't find any. Closest i got were something with Ticks.

Link to comment
Share on other sites

Have a look at this:

$begin = TimerInit()

While 1
    If TimerDiff($begin) > 5000 Then
        TimerStart()
    EndIf
    Sleep(100)
WEnd


Func TimerStart()
    MsgBox(0, "", "Timer Start NOW")
    Exit
EndFunc

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

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