Help me with this error: Invalid operands 'int' and 'Object' in operator '=='.

Godot Version

4.7.stable

Question

Hello, thanks for reading.

this is my code:

12. var sprite = 0
13. func turn():
14.	    if velocity.x == 0 and velocity.y == 0:
15.		    #idol
16.		    sprite = 0
17.		    pass
18.	    if velocity.x > 1 and sprite == 0:
19.		    #right
20.		    sprite = load("res://assets/playerright.png")
21.		    $Sprite2D.texture = sprite
22.		    pass
23.	    if velocity.x < -1 and sprite == 0:
24.		    #left
25.		    sprite = load("res://assets/playerleft.png")
26.		    $Sprite2D.texture = sprite
27.		    pass
28.	    if velocity.y > 1 and sprite == 0:
29.		    #down
30.		    sprite = load("res://assets/playerdown.png")
31.		    $Sprite2D.texture = sprite
32.		    pass
33.	    if velocity.y < -1 and sprite == 0:
34.		    #up
35.		    sprite = load("res://assets/playerup.png")
36.		    $Sprite2D.texture = sprite
37.		    pass

my objective here was to make the player change sprites when walking to different directions and to keep the last sprite if the player walks diagonally.
the error is coming up in lines: 18, 23, 28, 33, presumably on the sprite == 0 part.
Hope that’s enough info, thanks!

You can use ai like Gemini , Claude , ChatGPT for problems like this it can help u learn and find the mistakes u did easily

BUT DONE USE IT TO DO EVERYTIHNG FOR U K?

The error is happening because the type of sprite changes during your code.

At the beginning:

var sprite = 0

So sprite is an int.

Later you do:

sprite = load("res://assets/playerright.png")

Now sprite is a Texture2D object.

Then on the next frame you compare:

sprite == 0

You’re comparing a Texture2D with an int, which gives:

Invalid operands ‘int’ and ‘Object’ in operator ‘==’


Another problem

Even if the error didn’t happen, this logic wouldn’t work.

When you first move right:

sprite = load("playerright.png")

Now sprite is no longer 0.

So these conditions will never be true again:

if velocity.x > 1 and sprite == 0:
if velocity.x < -1 and sprite == 0:
if velocity.y > 1 and sprite == 0:
if velocity.y < -1 and sprite == 0:

because sprite is now a texture, not 0.


What you probably wanted

You don’t actually need sprite == 0.

Just change the texture whenever the player moves in a direction.

func turn():
    if velocity.x > 1:
        $Sprite2D.texture = load("res://assets/playerright.png")

    elif velocity.x < -1:
        $Sprite2D.texture = load("res://assets/playerleft.png")

    elif velocity.y > 1:
        $Sprite2D.texture = load("res://assets/playerdown.png")

    elif velocity.y < -1:
        $Sprite2D.texture = load("res://assets/playerup.png")

Notice the elifs. Only one direction is chosen each frame.

If the player stops moving, nothing happens, so the last direction stays visible automatically.


If your goal is diagonal movement

You said:

keep the last sprite if the player walks diagonally

For example:

  • Move right → face right.
  • Hold Up while still holding Right → continue facing right.
  • Release Right and keep holding Up → now face up.

You can do that by giving horizontal movement priority:

func turn():
    if velocity.x > 1:
        $Sprite2D.texture = right_texture
    elif velocity.x < -1:
        $Sprite2D.texture = left_texture
    elif velocity.y > 1:
        $Sprite2D.texture = down_texture
    elif velocity.y < -1:
        $Sprite2D.texture = up_texture

Or give vertical movement priority by checking velocity.y first.


One more improvement

You’re calling load() every frame, which is inefficient. Load the textures once:

@onready var right_texture = preload("res://assets/playerright.png")
@onready var left_texture  = preload("res://assets/playerleft.png")
@onready var up_texture    = preload("res://assets/playerup.png")
@onready var down_texture  = preload("res://assets/playerdown.png")

Then just assign them:

$Sprite2D.texture = right_texture

This is much faster because the images are loaded only once.

Tysm for helping!
I didn’t realize that changing the variable type would break the if statement
though i would prefer to have the priority be the last sprite loaded instead of an horizontal or vertical priority

next i’m gonna try to use only a variable to check if the game should change sprites or just stay with the last one

also when you said that the sprite would never go back to 0, that wasn’t entirely true because on the first if statement (#idol) it sets sprite back to 0.

and i kinda refuse to use AI… like ever… no judgement for using it though.
and thanks also for the preload hint, that’ll come in handy.

yup this worked, just had to change ‘and’ to ‘or’ on the 14th line,

Thanks!