Jump to content

use of const variables


Recommended Posts

First, HUGE thanks to you folks for AutoIt and the AutoIt forums :whistle: I'm at line 767 on my first program and this is only the second time I've gotten stuck. This is the first time something isn't working the way I thought it would, so I need a little guidance. (Be Gentle)

I am getting an error "Can not redeclare a constant", but there are ONLY two instances of each of the two Constants in the file.

Here are the snippets from my code

;declared constant
Global const $BlockH = 11, $BlockW = 16
;fill variables with constant values
$BlkH=$BlockH
$BlkW=$BlockW
;these are the lines I am performing that reference the variables
While 1
    If _IsPressed("01") Then 
        $LastLeftClick=MouseGetPos()
        ToolTip("LeftClick: "&$LastLeftClick[0]&","&$LastLeftClick[1],1100,600)
        ExitLoop
    EndIf
WEnd
MouseMove($LastLeftClick[0],$LastLeftCLick[1]-$BlkH,10)
Send($keyL)
$PieceIsGood= PixelSearch($LastLeftClick[0]-$BlkW,$LastLeftCLick[1]-$BlkH,$LastLeftClick[0]+$BlkW,$LastLeftCLick[1]+$BlkH,$badBridge)
if @error then 
    Beep(1000,500)
    $DeadEnd=1
EndIf
MouseClick("left",$LastLeftClick[0],$LastLeftCLick[1]-$BlkH,10)
Sleep(1000)
;the error kicks me out somewhere in the above code

I think it may be due to my addition of an array with a constant, but not sure how else to do this. I just don't understand why the "redeclare" error is popping up, cause I don't think I'm redeclaring the constant.

Thanks in advance ;)

If I learned from all my mistakes, I would be the smartest man alive!Nothing is fool-proof, because fools are so ingenious.Ingorance it not knowing better. Stupidity is knowing better, but doing it anyway. No way can I be stupid.

Link to comment
Share on other sites

  • Moderators

;declared constant
Global const $BlockH = 11, $BlockW = 16
;fill variables with constant values
$BlkH=$BlockH
$BlkW=$BlockW
Once you make a constant, you can't change it (thus it being a constant)You see here that you try to make the constant = $BlockH and $BlockW (not thinking I like the sounds of those vars too much). Just remove the word Constant.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

  • Moderators

$BlkH / $BlkW arn't constants but, and hes not changing the values of $BlockH or $BlockW.

Oops :"> (totally misread that)

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

;declared constant
Global const $BlockH = 11, $BlockW = 16
;fill variables with constant values
$BlkH=$BlockH
$BlkW=$BlockW
Once you make a constant, you can't change it (thus it being a constant)You see here that you try to make the constant = $BlockH and $BlockW (not thinking I like the sounds of those vars too much). Just remove the word Constant.
Thanks for the quick reply!

I WANT these variables to be Constant.

I added the $BlkH and $BlkW in an attempt to protect the constants. (Apparently it didn't work)

Where is the line(s) that is(are) trying to change them?

BTW, the Vars are refering to pixel blocks ... 11 pixels high by 16 pixels wide, not sure why you don't like the sound. :whistle: But I am declaring them early, so I can adjust them later if my calculations are wrong. The code lines where this is crashing is going to be used in a Select-Case-EndSelect in each of the Cases (27 so far).

Thanks in advance.

If I learned from all my mistakes, I would be the smartest man alive!Nothing is fool-proof, because fools are so ingenious.Ingorance it not knowing better. Stupidity is knowing better, but doing it anyway. No way can I be stupid.

Link to comment
Share on other sites

:whistle:, what line number does it indicate the error is on SimpleMindedFool?

Line 105, which reads:

Global const $BlockH = 11, $BlockW = 16

This is the only declaration for these, too.

If I learned from all my mistakes, I would be the smartest man alive!Nothing is fool-proof, because fools are so ingenious.Ingorance it not knowing better. Stupidity is knowing better, but doing it anyway. No way can I be stupid.

Link to comment
Share on other sites

Thanks for the quick reply!

I WANT these variables to be Constant.

But I am declaring them early, so I can adjust them later if my calculations are wrong.

If your going to change them later why make them constants @@? Do you need to "protect" the original 16 and 11 values? If your not going to change the variables (meaning your calculations are correct), and not have them constant, just don't use them.

Edit:

Can you declare 2 variables on the same line in AutoIt? (I forget :whistle:).

Perhaps...

Global Const $BlockH = 11
Global Const $BlockW = 16
Edited by Burrup

qq

Link to comment
Share on other sites

If your going to change them later why make them constants @@? Do you need to "protect" the original 16 and 11 values? If your not going to change the variables (meaning your calculations are correct), and not have them constant, just don't use them.

Edit:

Can you declare 2 variables on the same line in AutoIt? (I forget :whistle:).

Perhaps...

Global Const $BlockH = 11
Global Const $BlockW = 16
Sorry, must clarify...

I want $BlockW and $BlockH to be constant. These may need adjustment, but not during program operation (sort of a tweaking point at the code level).

$BlkH and $BlkW were introduced because I was getting the redefine error. The idea was to isolate the Constants by using a substitute variable instead.

If I learned from all my mistakes, I would be the smartest man alive!Nothing is fool-proof, because fools are so ingenious.Ingorance it not knowing better. Stupidity is knowing better, but doing it anyway. No way can I be stupid.

Link to comment
Share on other sites

Can you declare 2 variables on the same line in AutoIt? (I forget :whistle:).

Perhaps...

Global Const $BlockH = 11
Global Const $BlockW = 16
According to help, yes. But tried it on seperate lines and it kicks out on first line:

Global const $BlockH = 11
Global Const $BlockW = 16

If I learned from all my mistakes, I would be the smartest man alive!Nothing is fool-proof, because fools are so ingenious.Ingorance it not knowing better. Stupidity is knowing better, but doing it anyway. No way can I be stupid.

Link to comment
Share on other sites

And your SURE your not declaring $BlockH anywhere else?

I search the code several times, only two instances of each of the Constants listed. I even used the

I also checked the "#include" files for the variable name, just in case.

By trial and error method (comment out line by line), I have found the following section is the guilty offender:

$PieceIsGood= PixelSearch($LastLeftClick[0]-$BlkW, $LastLeftCLick[1]-$BlkH, $LastLeftClick[0]+$BlkW, $LastLeftCLick[1]+$BlkH, $badBridge)
    if @error then 
        Beep(1000,500)
        $DeadEnd=1
    EndIf

When I change the "if @error" to "if NOT @error", I don't get the redefine error. However, I also don't get the correct response from the argument. I don't think PixelSearch likes my "mixed" array and variable additions. The coords are correct (triple checked) and the color is correct. The purpose is to check around the current mouse position for a specific color and if found, set a flag ($DeadEnd) so it will drop out of the loop.

Is there an easy way to store one piece of an array into a single variable?

For example, how would make the following code work?

$LastLeftClick=MouseGetPos()
$LastX=$LastLeftClick[0]
$LastY=$LastLeftClick[1]

If I learned from all my mistakes, I would be the smartest man alive!Nothing is fool-proof, because fools are so ingenious.Ingorance it not knowing better. Stupidity is knowing better, but doing it anyway. No way can I be stupid.

Link to comment
Share on other sites

That is the easiest way, and that SHOULD work. Is $DeadEnd a constant?

No, $DeadEnd is a local variable, not constant.

In fact the only Constants in the whole code are the two mentioned before. And FYI, when the two Global Const are reversed, the other one gets redefine error. Also moving "Global Const" above all other "Global" (above all declaration line in fact) had not effect. All goes back to my screwball PixelSearch with the if @error, instead of if NOT @error

... easiest way, and SHOULD work? ... hmmm ... where's the dunce cap smilie for me? :whistle:

I'm going to wrestle around with this some more, but I think I might be going in the right direction.

I really appreciate the help. ;)

Edit:

I was wrong, sort of ... $DeadEnd is a local variable ... but in my use it became a Constant:

Do
;snip - snip
$DeadEnd=1
;snip - snip
while $DeadEnd=0

I still can't figure out why it was kicking me back to the first "Global Const" definition line. That confused me ... which isn't too hard to do :P

Edited by SimpleMindedFool

If I learned from all my mistakes, I would be the smartest man alive!Nothing is fool-proof, because fools are so ingenious.Ingorance it not knowing better. Stupidity is knowing better, but doing it anyway. No way can I be stupid.

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