Invalid set index 'knockback_vector' (on base: 'Vector2') with value of type 'Vector2'.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Gael
onready var swordHitbox = $HitboxPivot/SwordHitbox
func _ready():
 swordHitbox = Vector2.ZERO
swordHitbox.knockback_vector = input_vector

So what are you trying to do with this code? We know it involves knocking back something. But what are the details about the nodes involved, such as the SwordHitbox node?

Ertain | 2022-09-28 12:25

:bust_in_silhouette: Reply From: godot_dev_

The error code is telling you that you are incorrectly trying to access a member knockback_vector of a type Vector2. Indeed this is the case.

First, you assign swordHitbox with what appears to be a hitbox in the scene tree with the line of code:

onready var swordHitbox = $HitboxPivot/SwordHitbox

This seems correct. However, in your ready function, you override/erase the reference to a hitbox with an empty Vector2

swordHitbox = Vector2.ZERO

swordHitbox no longer references a hitbox object, it’s now a Vector2. You proceed to access a member knockback_vector of this empty vector

swordHitbox.knockback_vector = input_vector

Either remove swordHitbox = Vector2.ZERO or rethink your logic, because you are using the swordHitbox variable to store different types of variables (a hitbox and Vector2), which is bad design and can quickly get messy

Thank you very much!

Gael | 2022-10-01 07:08