I'm currently working on my first game and I'm having a hard time implementing a dash to the player. I tried following a godot 3 tutorial and changing some of the godot 3 language to got 4 but no matter how hard I try it never works.
here is my platformer code.

extends CharacterBody2D

@export var movement_data : PlayerMovementData

var air_jump = false
var just_wall_jumped = false
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
var was_wall_normal = Vector2.ZERO

@onready var animated_sprite_2d = $AnimatedSprite2D
@onready var coyote_jump_timer = $CoyoteJumpTimer
@onready var starting_position = global_position
@onready var wall_jump_timer = $WallJumpTimer
@onready var dash = $Dash

func _physics_process(delta):
	apply_gravity(delta)
	if Input.is_action_just_pressed("Dash"):
		dash.start_dash(movement_data.dash_length)
	var speed = movement_data.dash_speed if dash.is_dashing() else movement_data.normalspeed
	handle_wall_jump()
	handle_jump()
	var input_axis = Input.get_axis("move_left", "move_right")
	handle_acceleration(input_axis, delta)
	handle_air_acceleration(input_axis, delta)
	apply_friction(input_axis, delta)
	apply_air_resistance(input_axis, delta)
	var was_on_floor = is_on_floor()
	var was_on_wall = is_on_wall_only()
	if was_on_wall:
		was_wall_normal = get_wall_normal()
	move_and_slide()
	var just_left_ledge = was_on_floor and not is_on_floor() and velocity.y >= 0
	if just_left_ledge:
		coyote_jump_timer.start()
	just_wall_jumped = false
	var just_left_wall = was_on_wall and not is_on_wall()
	if just_left_wall:
		wall_jump_timer.start()
	update_animations(input_axis)

func apply_gravity(delta):
	if not is_on_floor():
		velocity.y += gravity * movement_data.gravity_scale * delta

func handle_wall_jump():
	if not is_on_wall_only() and wall_jump_timer.time_left <= 0.0: return
	var wall_normal = get_wall_normal() 
	if wall_jump_timer.time_left > 0.0:
		wall_normal = was_wall_normal
	if Input.is_action_just_pressed("jump"):
		velocity.x = wall_normal.x * movement_data.normalspeed
		velocity.y = movement_data.jump_velocity
		just_wall_jumped = true

func handle_jump():
	if is_on_floor(): air_jump = true
	
	if is_on_floor() or coyote_jump_timer.time_left > 0.0:
		if Input.is_action_pressed("jump"):
			velocity.y = movement_data.jump_velocity
			coyote_jump_timer.stop()
	elif not is_on_floor():
		if Input.is_action_just_released("jump") and velocity.y < movement_data.jump_velocity / 2:
			velocity.y = movement_data.jump_velocity / 2
		
		if Input.is_action_just_pressed("jump") and air_jump and not just_wall_jumped:
			velocity.y = movement_data.jump_velocity * 0.8
			air_jump = false

func handle_acceleration(input_axis, delta):
	if not is_on_floor(): return
	if input_axis != 0:
		velocity.x = move_toward(velocity.x, movement_data.normalspeed * input_axis, movement_data.acceleration * delta)

func handle_air_acceleration(input_axis, delta):
	if is_on_floor(): return
	if input_axis != 0:
		velocity.x = move_toward(velocity.x, movement_data.normalspeed * input_axis, movement_data.air_acceleration * delta)

func apply_friction(input_axis, delta):
	if input_axis == 0 and is_on_floor():
		velocity.x = move_toward(velocity.x, 0, movement_data.friction * delta)

func apply_air_resistance(input_axis, delta):
	if input_axis == 0 and not is_on_floor():
		velocity.x = move_toward(velocity.x, 0, movement_data.air_resistance * delta)

func update_animations(input_axis):
	if input_axis != 0:
		animated_sprite_2d.flip_h = (input_axis < 0)
		animated_sprite_2d.play("run")
	else:
		animated_sprite_2d.play("idle")
	
	if not is_on_floor():
		animated_sprite_2d.play("jump")

func _on_hazard_detector_area_entered(area):
	global_position = starting_position

func handle_dash():
	if Input.is_action_just_pressed("Dash") and is_on_floor():
		movement_data.normalspeed = movement_data.dash_speed

and here is the code I currently have for the dash timer
`extends Node2D

@onready var timer = $dashtimer

func start_dash(dur):
	timer.wait_time = dur
	timer.start()

func is_dashing():
	return !timer.is_stopped()

if you might know whats wrong please help.

  • spaceyjase and xyz replied to this.
  • Klown movement_data.dash_speed is only referenced in 2 places:

    • in handle_dash() which is never called
    • assigned to speed in _pyhsics_process() but speed is never used

    So your code never changes the active speed to dash_speed.

    This has nothing to do with Godot api, meaning that engine version switch is not the cause of this problem. It's just a regular bug/oversight in the game logic.

    It's very hard to read your code like that. Try putting ~~~ before and after the code snippet.

      Klown var speed = movement_data.dash_speed if dash.is_dashing() else movement_data.normalspeed

      Where do you use this speed variable?

        Klown I tried following a godot 3 tutorial and changing some of the godot 3 language to got 4 but no matter how hard I try it never works.

        Don't do that. If you're currently at the level of learning from beginner-grade tutorials, you can't realistically expect that you'll just simultaneously port stuff. Porting requires solid amount of knowledge of both versions and is way harder than following a tutorial. Why make things harder than they need to be?

        So if you have a 3.x tutorial, download the 3.x and do the tutorial there. The learning value will be the same as all of the core concepts are exactly the same in both versions. At least bring the tutorial to completion in the intended version and THEN try adapting it to another version.

          spaceyjase
          I put it in an export script or what ever it's called to be able to easily change the variables

          xyz
          thanks and i don't mean to be rude but when i say i'm new to game dev i mean godot 4 I had been using godot 3 for the last 2 years

          • xyz replied to this.
            • Edited
            • Best Answerset by Klown

            Klown movement_data.dash_speed is only referenced in 2 places:

            • in handle_dash() which is never called
            • assigned to speed in _pyhsics_process() but speed is never used

            So your code never changes the active speed to dash_speed.

            This has nothing to do with Godot api, meaning that engine version switch is not the cause of this problem. It's just a regular bug/oversight in the game logic.

              xyz
              Thanks that helped a lot. didn't see that