Godot Version
4.6.2
Question
I recently followed this tutorial to learn Godot composition. I’m working on a new project now and using the system implemented for basic movement, however no movement is registering. I feel extremely lost as to why it won’t work as expected when InputComponent does indeed get updated properly each frame (at least looking at the debug message I added.) There are 3 component scripts: Player (where the other two components get initiated), Input, and Movement.
class_name Player extends CharacterBody3D
@onready var input_component: InputComponent = %InputComponent
@onready var movement_component: MovementComponent = %MovementComponent
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _physics_process(delta: float) -> void:
input_component.update()
movement_component.direction = input_component.move_dir
movement_component.jump_counter = input_component.jump_pressed
movement_component.tick(delta)
class_name MovementComponent extends Node
@onready var body : CharacterBody3D
#variables for movement and physics
const RUN_SPEED : float = 20.0
const SPEED : float = 10.0
@export var current_speed : float = SPEED
@export var jump_velocity : float = 12.0
@export var gravity_multiplier : float = 3.0
@onready var input_component: InputComponent = %InputComponent
var direction: Vector2 = Vector2.ZERO
var jump_counter := false
func tick(delta: float) -> void:
if body == null:
return
#Move
body.velocity.x = direction.x*current_speed
body.velocity.z = direction.y*current_speed
#Gravity
if not body.is_on_floor():
body.velocity += body.get_gravity() * delta * gravity_multiplier
#Jump
if jump_counter and body.is_on_floor():
body.velocity.y = jump_velocity
jump_counter = false
body.move_and_slide()
class_name InputComponent extends Node
var move_dir : Vector2 = Vector2.ZERO
var jump_pressed : bool = false
func update() -> void:
move_dir = Input.get_vector("left", "right", "up", "down")
jump_pressed = Input.is_action_just_pressed("jump")
print(move_dir)
print(jump_pressed)
As you can see, both are referred to in the Player script, however when running the game, nothing in tick() is applied; I’m unable to use WASD to move and gravity is not applied. Also to note, the first project I implemented this in, it did work. I tried to see if I missed anything but I couldn’t find any differences in node organization or anything as such, I could be missing something though. I just really don’t know what it is exactly after hours of re-writing and analyzing. I also intend to add more components, so I’d like to be secure in using this method further without things not getting updated. I can send node trees etc. if this is unclear/unhelpful.
