Invalid operands "Vector2" and "float" for assignment operator - GDQUEST TUTORIAL

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

I am watching a GDQuest tutorial from Oct 2, 2019. Not sure what version this is…
https://www.youtube.com/watch?v=Mc13Z2gboEk&list=PL-mK09n3GWSVzWd3UwAtts8Wi11WgOsJB&index=19 #1:18:34

Using stable version 4 and GDScript, most of these issues I have been able to sort out myself by rather omitting code or googling answers, however here I’m at a loss… :frowning:

The script I’m stuck on is extended from the Actor.gd that has only

extends CharacterBody2D
class_name Actor

@export var speed: Vector2 = Vector2(300.0, 1000.0)
@export var gravity: float = 3000.0

My problem is in the enemy script, the goal is to make sure it has gravity.

Error at (7, 5) Invalid operands “Vector2” and “float” for assignment operator

extends "res://src/Actors/Actor.gd"

func _ready() -> void:
velocity.x = -speed.x

func _physics_process(delta: float) -> void:
velocity += gravity * delta
if is_on_wall():
	velocity.x *= -1.0
move_and_slide()

This is my first time asking a question I hope the format is acceptable!

:bust_in_silhouette: Reply From: jgodfrey

I assume the problem is here:

velocity += gravity * delta

velocity is a Vector2 and you’re trying to add a float value to it - which isn’t possible.

I’m honestly just following the tutorial word per word, I understand where the issue is coming from, I’m just not sure how to solve this problem. I need this object to fall downwards using the gravity variable, I appreciate your response!

DragoXing | 2023-05-17 02:59

That code is not valid, so I assume that’s not what’s in the tutorial you’re watching…

Just looked at the video reference. The code in the video is:

velocity.y += gravity * delta

Notice, that’s the y component of the vector - which is itself a float, so that’s perfectly fine. You’re code is missing the .y

jgodfrey | 2023-05-17 03:03

Holy crap, thank you I’m sorry for taking up your time sir.

DragoXing | 2023-05-17 03:07

Sometimes, it just takes another set of eyes to see the mistake… Happy to help. :slight_smile:

jgodfrey | 2023-05-17 03:10