Invalid operands 'float' and 'Vector2' in operator '>'.

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

I am trying to implement a knockback feature, everything seems right, but an error message pops up for the third line of this code from

func knockback(var enemypos):
velocity.y = min_jump_velocity

if position.x > enemypos:
	velocity = 800
elif  position.x < enemypos:
	velocity = -800

Over in I have:

func _on_Hitbox_body_entered(body):
if == "Player":

Not really sure why I’m getting an invalid operand call here. Still fairly new to Godot and programming in general.

I tried using enemypos.x, but that presents an “Invalid get index ‘x’ (on base: ‘float’)” error.

Any help is appreciated! Thank you.
(haven’t been able to mess around with this code to see how 800 works…lol)

:bust_in_silhouette: Reply From: Godont

This is only a guess, but is your velocity variable defined as a Vector2 (i.e. does it contain an x and y component)? If so, you are trying to set a vector of two values equal to a single number (float).

Perhaps try

if position.x > enemypos:
    velocity.x = 800
elif  position.x < enemypos:
    velocity.x = -800

(This will only provide horizontal knockback).

velocity is defined as

var velocity = Vector2()

The full code can be viewed here via Pastebin

edit: the knockback is different as I was trying some various methods

Sideswipe0009 | 2021-03-16 01:08

Right, so that is the problem. velocity is a Vector2 but you are trying to make it a float by making it equal to 800. Did the code change work for you?

Godont | 2021-03-16 14:58

I got it to be error free with this in

func knockback(var enemypos):
velocity.y = min_jump_height

if position.x > enemypos.x:
	velocity.x = 2 * Globals.UNIT_SIZE
elif position.x < enemypos.x:
	velocity.x = 2 * Globals.UNIT_SIZE

on the

func _on_Hitbox_body_entered(body):
if == "Player":

This seems to work for the x axis, but not the y axis, whereas before the y axis was working but not the x axis.

Sideswipe0009 | 2021-03-16 15:14