So, to answer your earlier question: Yes, you can put it in a custom function. You can then call that function from physics_process(), and at least your code will be a little easier to read. Your code is currently what we would call spaghetti code. It took me a while to parse and I have a number of suggestions for readability as well as speed and memory usage.
We will start with your if statement that calls dodge:
if Input.is_action_just_pressed("Dodge")
and canDodge == true
and waitDodge == false
and isDodge == false
and direction
and not debug
or Input.is_action_just_pressed("Dodge")
and canDodge == true
and waitDodge == false
and isDodge == false
and directionyaxis
and not debug:
This seems to basically come down to the fact that you can only dash if you are moving left or right - or up or down. If velocity is (0,0), dash does nothing.
if Input.is_action_just_pressed("Dodge")
and canDodge == true
and waitDodge == false
and isDodge == false
and velocity != Vector2.ZERO
and not debug:
if Input.is_action_just_pressed("Dodge") and canDodge == true and waitDodge == false and isDodge == false and velocity != Vector2.ZERO and not debug:
execute_dash()
(The second one is the one to use in GDScript.)
dashrandom = round(rng.randf_range(1,6))
There is no reason to declare this variable at the top of the file. You don’t use it anywhere else in your code. You’ve scoped this variable at a higher level than necessary, so it’s taking up space in RAM even when the character isn’t dashing.
aaaaaa = false
I have no idea what this does. Don’t care. You might consider a more descriptive name for future you. Still good you added all the code because we can see it’s immaterial to the issue.
if didbounce == true:
out = true
Same as above.
rng.randomize()
This doesn’t do anything. You’ve already gotten your number up above. You don’t need to call randomize() every time.
if dashrandom == 1:
$DashSound.pitch_scale = 1
$DashSound.play()
elif dashrandom == 2:
$DashSound.pitch_scale = 0.94
$DashSound.play()
elif dashrandom == 3:
$DashSound.pitch_scale = 0.90
$DashSound.play()
elif dashrandom == 4:
$DashSound.pitch_scale = 1.02
$DashSound.play()
elif dashrandom == 5:
$DashSound.pitch_scale = 1.08
$DashSound.play()
elif dashrandom == 6:
$DashSound.pitch_scale = 1.10
$DashSound.play()
There are cleaner ways of doing this.
const dash_pitch: Array[float] = [0.90, 0.94, 1.0, 1.02, 1.08, 1.10] #Top of the file
dash_random_pitch = round(rng.randi_range(0,5))
$DashSound.pitch_scale = dash_pitch[dash_random_pitch]
$DashSound.play()
Alternately, in the $DashSound object you could change the Stream to AudioStreamRandomizer, add your dash sound stream into it, and set Random Pitch to 1.1 (which would cover 0.90 to 1.10).
$WallGoneTimer.stop()
leftwallgone()
rightwallgone()
$OnionSkin.emitting = true
$constant.start()
constant = true
set_collision_mask_value(2, false)
$AnimationPlayer.play("Dodge")
isDodge = true
canDodge = false
waitDodge = true
$DodgeTimer.start()
$isDodgeTimer.start()
Skipping this.
dodgepower = dodge
The variable dodge always equals 1600.0 according to your code. I recommend making dodge a constant.
if tween:
tween.stop()
tween = create_tween()
tween.tween_property(self, "dodgepower", 0, 0.18).set_ease(Tween.EASE_OUT)
The best thought I have, and @gertkeno may have a better one, is that you should probably be lerping this, not tweening it.
dodgepower = lerp(dodgepower, 0.0, 0.18)
Also this code should probably be here:
elif wallclimbmode == false and constant == true and bounce == false and stopdashvelo == false:
dodgepower = lerp(dodgepower, 0.0, 0.18)
velocity.x = direction * (SPEED + dodgepower) * 1.4
velocity.y = directionyaxis * (dodgepower) * 1.6
And the dash code ends up being:
const dash_pitch: Array[float] = [0.90, 0.94, 1.0, 1.02, 1.08, 1.10] #Top of the file
func execute_dash() -> void:
dash_random_pitch = round(rng.randi_range(0,5))
$DashSound.pitch_scale = dash_pitch[dash_random_pitch]
$DashSound.play()
aaaaaa = false
if didbounce == true:
out = true
$WallGoneTimer.stop()
leftwallgone()
rightwallgone()
$OnionSkin.emitting = true
$constant.start()
constant = true
set_collision_mask_value(2, false)
$AnimationPlayer.play("Dodge")
isDodge = true
canDodge = false
waitDodge = true
$DodgeTimer.start()
$isDodgeTimer.start()
dodgepower = dodge