if statement just isn't having any of it

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
bakewell66
Posts: 2
Joined: 08 May 2018, 19:17

if statement just isn't having any of it

08 May 2018, 19:29

Hi guys,
i have been trying to get this simple if statement to work and ive google'd for hours for something im doing wrong and i just can't figure it out,
all i want it to do is compare a variable to an nth of an array in a loop

my code in it's most simple form is:

Code: Select all

i := 1
loop 5 
{
	ii := Array[i]
	if (ii > %current_xp%) {
		rank = %i%
		i ++
	}
}

any thoughts would be greatly appreciated
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: if statement just isn't having any of it

08 May 2018, 19:46

I think the i ++ should be without a space, so i++. However, without having access to your Array nor what current_xp is, it's hard to diagnose an issue. And what is it you expect each variables value to be at the end of your process?
bakewell66
Posts: 2
Joined: 08 May 2018, 19:17

Re: if statement just isn't having any of it

08 May 2018, 19:58

thank you for the reply

this is the array and the variable.

Code: Select all

Array := [1, 9337, 365056, 2577829, 9863433, 18568070]
current_xp = 50

i := 1
loop 5 
{
	ii := Array[i]
	if (current_xp > %ii%) {
		rank = %i%
		i++
	}
}

i want the script to check the current_xp against requirements to do certain levels ( the numbers in the array ), if the current_xp is greater then up the rank, at the end of the script the current_xp gets increased
gregster
Posts: 9012
Joined: 30 Sep 2013, 06:48

Re: if statement just isn't having any of it

08 May 2018, 21:05

You are using if-expression-syntax with parentheses here, which is recommended :thumbup:. But unfortunately, you are using it wrong :shifty: Indeed, this is rather a mixup of traditional and expression if syntax.

That is why if (current_xp > %ii%) will always be true here. Each run of the loop, if will test, if (50 > 0).

You should rather use if (current_xp > ii) - that means, no %s in the expression - to compare the contents of the two variables.

If you write %ii% here, you are doing a double de-ref: that means, even if the actual content of the variable ii is the number 9337, for example, if will not compare it to 50.
Instead, AHK will look for a variable named 9337 and take its contents - unfortunately the rather lax AHK syntax allows variable names that only consist of digits - and this variable will probably be blank (you didn't define any variable named 9337 in your script - I cannot blame you for that :D ). A blank variable will be interpreted as 0 in this comparison.

Try this to see what I mean:

Code: Select all

if (1 > %9337%)				; AHK will take the contents of the variable named 9337, which is blank --> if (1 > 0) returns true
	msgbox 9337 is blank: %9337% 
9337 := "test"					; see that a number really could be a variable name
msgbox %9337%
Btw, you are using two different styles to assign variables. You should go with the expression syntax again here (the other one will be removed in AHK v2): Use rank := i instead of rank = %i% for consistency.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: slowwd and 195 guests