"Unexpected Identifier" in class body - My first "tutorial" project (Pin Pon)

Godot Version

Godot 4.3 Stable

Question

I’m making my first tutorial project to learn the basics of making games with Godot with a Pin Pon game. My project is going decently but when making the ball’s code which is attached to its CharacterBody2D it gives me this error: “Error at (3, 16): Unexpected identifier” in class body. The code is the following:
extends CharacterBody2D

var speed = 100
velocity = Vector2.ZERO

func _ready() → void:
velocity.x = [-1,1][randi() % 2]
velocity.y = [-0.8,0.8][randi() % 2]

func _physics_process(delta: float) → void:
move_and_collide(velocity * speed * delta)
-------------------------------------------------------------------- (spacer, not in my code) ---------------------------
Also, I don’t know if this matters but I have a ColorRect for the shape and color of the ball and a CollisionShape2D for the physics, both as the child of the CharacterBody2D.


Because you’re trying to manipulate the property velocity outside a function, you even need to call it because velocity already starts as zero, but if for some reason you want to change it at the start you need to do it insde a function (like in the _ready function)

1 Like

You are missing var keyword in the velocity variable declaration, hence the “Unexpected identifier” error.

var velocity = Vector2.ZERO

The script inherits from CharacterBody2D, so already has the velocity property

Ah yes, but then you can’t have this piece of code floating around outside of the function, it needs to be moved inside the function.
velocity = Vector2.ZERO

1 Like

Thank you so much! Yes, it was exactly that, I moved the velocity property inside the start function, and it worked as expected. Once again, thank you so much! :tada:

1 Like