Search code examples
godotgdscript

Getting "error: ":" expected at the end of the line" when it is clearly present. (GDScript)


I have tried restarting the engine and rewriting the code but i still get an error asking for a colon on the end of the line.

while angle not in range(-360, 360):

Relevant code if it helps:

func _physics_process(delta):
    angle = self.rotation_degrees
    while angle not in range(-360, 360):
        if angle > 360:
            angle -= 360
        elif angle < -360:
            angle += 360

Edit: Fixed indentation in the code example.


Solution

  • The issue is that not in is not valid GDScript syntax.

    I believe this is what you want:

    while not angle in range(-360, 360):
    

    With that said, you seem to be going on a convoluted way to wrap the angle. You can do this for the same effect:

    angle = wrapf(angle, -360, 360)
    

    Using wrapf is very useful to normalize angle. For example, wrapf(angle, -180, 180) or wrapf(angle, 0, 360) for degrees or wrapf(angle, -PI, PI) or wrapf(angle, 0, TAU) for radians.


    What does wrapf do? It wraps a float value around an interval. If the value is gone past one end of the interval, pretend it entered by the opposite end. For example wrapf(14, 0, 10) is 4, and wrapf(-4, 0, 10) is 6.

    If I wanted to make a wrap function replacement in GDScript it would look like this:

    func wrap(value:float, from:float, to:float) -> float:
        var diff := to - from
        return value - (diff * floor((value - from) / diff))
    

    You don't have to. This is just for reference.