I’m working on a project using the Godot game engine
the project includes player movement and dashing ability
I have a script for the CharacterBody2D that handles player movement and dashing
but I want to make dashing ability independent
can I make another scene with Node node and add a script to it that handles the dashing ability?
if yes, how do I implement it?
do I access the player through dash script or what?
I’m going to share with you the code on CharacterBody2D
if you know how can I split the code into two files, one for player movement and the other for dashing ability please let me know
extends CharacterBody2D
@onready var animated_sprite = $AnimatedSprite2D
@onready var timer = $Timer
@export_category("Properties")
@export var player_speed: int = 500
@export var gravity: int = 1200
@export var jump_strength: int = -400
@export var max_jump_times: int = 2
@export var dash_speed: int = 1500
@export var dash_duration: float = 0.2
var dashing: bool = false
var dash_timer: float = 0.0
var can_dash: bool = true
var jump_count: int = 0
var was_on_floor: bool = false
var play_fall_shake: bool = false
func _process(delta):
handle_player_movement(delta)
func handle_player_movement(delta):
var direction = get_movement_axis()
# Handle Gravity
if not is_on_floor():
velocity.y += gravity * delta
play_fall_shake = true
else:
can_dash = true
if not was_on_floor:
handle_jump_and_camera_shake()
# Handle Dashing
if dashing:
velocity.y = 0
velocity.x = dash_speed * (1 if animated_sprite.flip_h == false else -1)
dash_timer -= delta
if dash_timer <= 0:
dashing = false
else:
velocity.x = direction.x * player_speed
was_on_floor = is_on_floor()
manage_animations(direction)
move_and_slide()
func manage_animations(direction):
# Handle Sprite Face Direction
if direction.x == -1:
animated_sprite.flip_h = true
if direction.x == 1:
animated_sprite.flip_h = false
# Handle
if direction.x != 0:
animated_sprite.play("walk")
else:
animated_sprite.play("idle")
# Handle Jump Animation
if velocity.y and not dashing:
animated_sprite.play("jump_start")
if dashing:
animated_sprite.play("dash")
func handle_jump_and_camera_shake():
jump_count = 0
if play_fall_shake:
var camera = get_tree().get_first_node_in_group("camera") as Camera2D
if camera:
camera.apply_jump_fall_shaking()
play_fall_shake = false
func get_movement_axis():
var x_movement = (Input.get_action_strength("move_right") - Input.get_action_strength("move_left") if not dashing else 0)
return Vector2(x_movement, 0)
func _input(event):
if event.is_action_pressed("jump"):
if can_jump():
velocity.y = jump_strength
jump_count += 1
if event.is_action_pressed("dash") and not dashing and can_dash:
start_dash()
func can_jump():
return is_on_floor() or jump_count < max_jump_times
func start_dash():
can_dash = false
dashing = true
dash_timer = dash_duration