0 votes

I have only one print() function in my game

It prints

4

This is the code where the print function is:

var d = direction
if !d == 4:
    print(d)

The local variable var d is only used in this lines of code

The variable direction is a variable that changes sometimes, but it is always between 0 and 7.

HOW IS IT POSSIBLE THAT IT PRINTS 4 SOMETIMES IF IT ONLY PRINTS WHEN
IT ISNT 4

My head will explod soon ;). Please help me!

Here is the whole script, also when I am not thinking that you will need it. It is the only script:

extends KinematicBody2D

export (int) var speed = 300

var direction = 4
var velocity = Vector2()
var spritedirection = "U"

func get_input():
    velocity = Vector2(0,0)
    var right = Input.is_key_pressed(KEY_D)
    var left = Input.is_key_pressed(KEY_A)
    var up = Input.is_key_pressed(KEY_W)
    var down = Input.is_key_pressed(KEY_S)

    if left:
        velocity.x -= speed
    if right:
        velocity.x += speed
    if up:
        velocity.y -= speed
    if down:
        velocity.y += speed

func get_direction():
    if !velocity == Vector2(0,0):
        direction = rad2deg(velocity.angle()) / 45 + 3


func set_sprite():
    if velocity == Vector2(0,0):
        if $Animation.current_animation == "Laufen1":
            $Animation.play("Stehen1")
        if $Animation.current_animation == "Laufen3":
            $Animation.play("Stehen3")
        if $Animation.current_animation == "Laufen5":
            $Animation.play("Stehen5")
        if $Animation.current_animation == "Laufen7":
            $Animation.play("Stehen7")
    else:
        var d = direction
        if !d == 4:
            print(d)
            $Animation.play("Laufen"+str(d))

func _process(delta):
    get_input()
    get_direction()
    set_sprite()
    velocity = move_and_slide(velocity)
in Engine by (369 points)

Crazy idea, but have you tried writing d != 4, instead of !d == 4?

1 Answer

+1 vote
Best answer

The problem is that direction is a float (as you didnt specify type), and the operation to calculate it may not result in an exact integer, because of rounding errors. So, as direction is always an int, you could either do this when declaring it:

var direction : int = 4

or when comparing:

var d = int(direction)
    if !d == 4:
    print(d)

You see 4 being printed in the console, because numbers like 4.000000001 or 3.999999999 will be printed as 4 because of rounding, but they are not equal. For example, you can test this:

var a = 4.000000001
var b = 3.999999999
var c = 4
print(a)
print(b)
print(a==c)
print(b==c)
by (3,437 points)
selected by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.