[Resolved] Variable types in Comparisons

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Pierogiboi

Hello, I am having trouble with a seemingly simple thing. I ran into the error in my main game and then made a testing script in an otherwise empty project to test it. I’m really at a loss for what I’m doing wrong.

Basically, I just want to check if my variable, which is a float number, is within a certain interval. Here is a picture of the code I wrote for the test script.

test script

The error log tells me: "Invalid operands ‘bool’ and ‘float’ in operator ‘<’.

But the numbers are float numbers on either side of the expression, and “test” is a float variable, is it not? Where is this boolean coming from?

I am running Godot 3.4.4-stable_win64 and using GDScript

:bust_in_silhouette: Reply From: jgodfrey

That syntax isn’t valid. You want this:

var test = 0
if test >= 0 && test < 100:
	print("yay")
else:
	print("no")

I assume the error comes from how the statement is resolved. Just guessing, but probably something like this:

if 0.0 <= test < 100.0

probably resolves to an intermediate state of:

if true < 100.0

Since the 0.0 <= test part is true.

Then, in the next round of resolution, you’d have a bool compared to a float, resulting in the mentioned error…

Thank you, this makes sense. It was so ingrained in me from my math background to write intervals in the way I did so I never considered it would be unreadable in GDScript or other languages. I never would have considered this, thanks so much.

Pierogiboi | 2022-07-23 18:29