CharacterBody2D physics access

Godot Version

4.1.3 stable


I really don’t like how I need to set CB2D velocity, only by accessing the node directly each time:

@export var player_body: CharacterBody2D

player_body.velocity = player_body.velocity.move_toward(_player_vector() * player.SPEED, player.ACCELERATION)

ChatGPT tells me that I can make @onready var player_body = $CharacterBody2D and then declare in _process() var player_veclocity = player_body.velocity but it still throws error Invalid get index 'velocity' like when I tried:

@onready var player_body = $"../.." #I don't know wth is this, I just dragged and dropped CharacterBody2D here, maybe my mistake
var player_velocity = player_body.velocity

How can I make CharacterBody2D.velocity as one object, is it possible? I also tried
access velocity by using Player class:

extends CharacterBody2D
class_name Player

That was changing velocity, but player-object stood still in the scene.
Help please, I didn’t find much information on this.

Are you trying to access the player velocity from another node? If so, then the first option is your best choice. I don’t understand why you don’t like it.

Using class_name Player.velocity would not work since you’re not referencing any instance of the player node.

Nah, I just don’t like how first option looks like, visually. I understand that is the best way.
And yes, I access it from other node. But for my peace of mind I want to create single object of CharacterBody2D.velocity.

Noted that class call not works properly basically. I thought that I can access methods from it, like Player.vector() that returns input vectors. But it just don’t works. Unlike nodes’ own methods. I’m very upset about this because I don’t understand what’s wrong.