Godot 4 how do we..

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

In Godot 3.4.4
1 extends KinematicBody2D
2
3 var velocity = Vector2(0, 0)
4
5 func _physics_process(delta):
6 if Input.is_action_pressed(“ui_right”):
7 velocity.x = 100
8 if Input.is_action_pressed(“ui-letf”):
9 velocity.x = -100
10
11 move_and_slide(velocity)

works push the right arrow and it moves right
push the left arrow it moves left

in Godot 4.0 (any alpha)
it gives us a bunch of errors

Even worst is we do not even have a KinematicBody2D
so when in line 3 var velocity = Vector2(0, 0) it gives me this error
Error at(3,1): Member “velocity” redefined (Original in native class ‘characterBody2D’)
so fine if I type var ve = Vector2(0, 0)
I get rid of that error
but in line 11 move_and_slide(velocity) gives me this error
Error at (11,20): Too many arguments for move_and_slide()" call. Expected at most 0 but received 1

I just tried
var ve = Vector2(0, 0)

func _physics_process(delta):
if Input.is_action_pressed(“ui_right”):
ve.x = 100
if Input.is_action_pressed(“ui_left”):
ve.x = -100



move_and_slide()

it did not give me any errors but nothing happens when i press the right arrow or the left arrow keys. i check Project /Project Settings/Input map
both ui_right and ui_left is listed so what the is trouble

if it works in 3.4.4 why can it not work in 4.0

with Got 4.0 how about meeting us half way by allowing to use code that we took weeks
to create Godot 3.4.4 (or at the very lest try this)

Godot 4.0 create a command that allows to use the new code or the old code
(call the command version (or any thing close to it)
then say
if version = 3.4.4 switch back to the Godot 3.4…4 and run this code then switch to Godot 4.0

:bust_in_silhouette: Reply From: Bartholomew Mortimis

Before anyone mentions it, I am aware how incredibly late I am. However, this is a problem I encountered at Godot 4’s launch and this question is the top google result for the error rn.

In Godot 4, ‘velocity’ has been made a property of CharacterBody/KinematicBody. So to fix the ‘member “velocity” redefined’ error, you just need to remove the line where the error occurs.

As for move_and_slide breaking, the function now automatically applies the velocity variable when it’s called. To fix it, just remove the parameters in move_and_slide (And if you convert from 3, delete the ‘set_velocity(velocity) function too.’ Doing these should make the player move again.

This is what worked for me, at least.

Bro, thank you, very, very much!

What will I remove pls tell me
Am still learning GDscript so pls respect

extends CharacterBody2D

@export () var speed = 200
var ve = Vector2(0,0)

func get_input():
velocity = Vector2()
if Input.is_action_pressed(“ui_right”):
velocity.x += 1
if Input.is_action_pressed(“ui_left”):
velocity.x -= 1
if Input.is_action_pressed(“ui_down”):
velocity.y += 1
if Input.is_action_pressed(“ui_up”):
velocity.y -= 1

velocity = velocity.normalized()

move_and_slide(velocity)

Hi, in Godot 4 move and slide does not take velocity as an argument anymore. You can read about CharacterBody2D here

1 Like