見出し画像

Godot: Parse Error: Member "velocity" redefined (original in native class 'CharacterBody2D')を解決する

 このチュートリアルを取り組んでいましたところ、上記のエラーを観測しました。エラーの発生箇所は、次のとおりです。

extends CharacterBody2D

const GRAVITY_POWER := 1000
const JUMP_POWER := -400

	velocity.y += GRAVITY_POWER <- ここでエラーが発生

func _process(delta):
	velocity.y += GRAVITY_POWER
	if Input.is_action_just_pressed("ui_accept"):
		velocity.y = JUMP_POWER

(Player.gd)

 検索しますと、次のフォーラムが見つかりました。

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

Godot 4 how do we..

 どうやら、同じところで悩んでいるようです。この質問に対する回答は、次のとおりです。

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.

Godot 4 how do we..

 「velocity」はプロパティになっているらしく、コードに記述する必要は無いようです。

extends CharacterBody2D

const GRAVITY_POWER := 1000
const JUMP_POWER := -400

# velocity.y += GRAVITY_POWER

func _process(delta):
	velocity.y += GRAVITY_POWER
	if Input.is_action_just_pressed("ui_accept"):
		velocity.y = JUMP_POWER

 コメントアウトすると、問題なく動作しました。

この記事が気に入ったらサポートをしてみませんか?