invalid get index 'y' (on base: float) how do i resolve that?

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

I want to learn godot as a game engine and i starded follow gdquest tutorial and i get this thing i don’t know how to call it bc isn’t a error pls can anyoane help me?

code:extends “res://src/Actors/Actor.gd”

func _ready() → void:
_velocity.x = -speed.x

func _physics_process(_delta: float) → void:
_velocity.y += gravity * _delta
if is_on_wall():
_velocity.x *= -1.0
_velocity = move_and_slide(_velocity, FLOOR_NORMAL).y

sry i wanted to add a picture but idk how pls help!

:bust_in_silhouette: Reply From: Ertain

The error is saying that the assignment of the value can’t be done because the thing on the left-side of the assignment doesn’t have the desired property. That is, some variable doesn’t have the “.y” property. The problem could be with this line:

_velocity.y += gravity * _delta

The _velocity variable (in this case) appears to be a floating point number rather than a vector (a vector, BTW, does have the “.y” property).

If that’s not the error, then this line may be at fault:

_velocity = move_and_slide(velocity, FLOOR_NORMAL).y

The final .y may have to be deleted from the line.

Thank you very much that helped a lot !!

Ackro | 2021-11-26 16:18

:bust_in_silhouette: Reply From: timothybrentwood

Change:

_velocity = move_and_slide(velocity, FLOOR_NORMAL).y

to

_velocity = move_and_slide(velocity, FLOOR_NORMAL)

move_and_slide() returns a Vector2, your _velocity variable should always be a Vector2. In your version of code you were assigning your _velocity variable to the y component (just a float or floating point number) of the Vector2 returned by the move_and_slide() function. Then on the next _physics_process() call

_velocity.y += gravity * _delta

failed because a float doesn’t have a y component, it’s just a number, thus giving you the error.

thank you very much!

Ackro | 2021-11-26 16:19