[SOLVED] Error message with If statement

Started by KamikazeHighland, Tue 17/01/2012 01:19:17

Previous topic - Next topic

KamikazeHighland

This works fine:

float float1 = 3.75;
float float2 = 0.25;

Even FloatToInt(float1, eRoundNearest)


This, however:

if (3.0 >= float1 >= 2.0 && 3.0 >= float2 >= 2.0){
}

Type mismatch: cannot convert 'int' to 'float'

What am I not seeing?

Gilbert

Unlike real maths, you cannot use two comparison operators (==, >, <, >=, <=) at the same time. You need to use && or ||, etc. to achieve the purpose.

So, the expression 3.0 >= float1 >= 2.0 is not valid. It has to be changed to:
3.0 >= float1 && float1 >= 2.0

Try to change your codes to (brackets are added for more clarity):
Code: ags
if ((3.0 >= float1 && float1 >= 2.0) && (3.0 >= float2 && float2 >= 2.0)){
}

Khris

The error message is a bit misleading here.
The problem is that 3.0 >= float1 >= 2.0 first gets turned into 0/1 >= 2.0 since AGS starts by evaluating 3.0 >= float1 and depending on float1, that's true or false, which is handled as an int internally.

The solution is to process both conditions separately:

Code: ags
  if ((2.0 <= float1 && float1 <= 3.0) && (2.0 <= float2 && float2 <= 3.0)) ...


SMF spam blocked by CleanTalk