-1

I am using Godot 3.2.1 and I am making a turret scene, which rotates and shoots when it's pointing at a player. rotation is a property of Area2D and target_dir is the value of rotation which points at the player. I want the turret to shoot when these values are equal to each other plus-minus 0.1 uncertainty

if target_dir - 0.1 < rotation < target_dir + 0.1:  # this line has error
    print("bang!!!")
    if can_shoot:
        shoot()

The application fails immediately after running it. When I printed position and target_dir they were both floats, though the error states

Invalid operands 'bool' and 'float' in operator '<'.
Andrey Kachow
  • 936
  • 7
  • 22

1 Answers1

1

Godot language has a lot of common with Python, The syntax like a < b < c would be OK for Python, but not for GDScript. Godot language probably "sees" the if-statement like this

 if (target_dir - 0.1 < rotation) < target_dir + 0.1:
    ...

It does comparisons one by one and the result of the first operation has a boolean type. Hence the second operation compares bool with float and error occurs. So instead I used and keyword and two comparisons.

if target_dir - 0.1 < rotation and rotation < target_dir + 0.1:
    print("bang!!!")
    if can_shoot:
        shoot()
Andrey Kachow
  • 936
  • 7
  • 22