Godot Adding Dash Ability To Player As An Independent Scene

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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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
</code>
<code>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 </code>
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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật