Skip to content

Instantly share code, notes, and snippets.

@bfloch
Created May 30, 2022 08:28
Show Gist options
  • Save bfloch/ed5ed04502b63024aa8d6d94d0e502f0 to your computer and use it in GitHub Desktop.
Save bfloch/ed5ed04502b63024aa8d6d94d0e502f0 to your computer and use it in GitHub Desktop.
Framestepping in Godot
extends Node
"""
Assign to a single Node in the scene.
Define the following actions as keys:
- debug_switch_pause_mode
Switches pause from either _physics_process or _process (current mode
outputs in console)
- debug_step_frame
Pauses/Steps one frame (either physics or regular frame depending on mode)
- debug_resume
Resume from stepping/pausing
2022 by StateOff for Godot 3.x
"""
# v2
# ##############################################################################
# ## Downstream Dependencies
# ##############################################################################
# ##############################################################################
# ## State
# ##############################################################################
var pause_next_frame: bool = false
var pause_in_physics: bool = true
var pause_switch_handled: bool = false
# ##############################################################################
# ## Exports
# ##############################################################################
# ##############################################################################
# ## Signals
# ##############################################################################
# ##############################################################################
# ## Signal Handlers
# ##############################################################################
# ##############################################################################
# ## Overrides
# ##############################################################################
func _ready() -> void:
pause_mode = PAUSE_MODE_PROCESS
func _physics_process(delta: float) -> void:
_handle_pausing(true)
func _process(delta: float) -> void:
_handle_pausing(false)
# ##############################################################################
# ## Public API
# ##############################################################################
# ##############################################################################
# ## Private API
# ##############################################################################
func _handle_pausing(is_physics: bool) -> void:
if Input.is_action_just_pressed("debug_switch_pause_mode") and not pause_switch_handled:
pause_in_physics = not pause_in_physics
print("DEBUG: Pause in ", "_physics_process()" if pause_in_physics else "_process()")
pause_switch_handled = true
else:
pause_switch_handled = false
if is_physics and not pause_in_physics:
return
if pause_next_frame:
get_tree().paused = true
pause_next_frame = false
if Input.is_action_just_pressed("debug_step_frame"):
var is_paused: bool = get_tree().paused
if is_paused:
get_tree().paused = false
pause_next_frame = true
else:
get_tree().paused = true
if Input.is_action_just_pressed("debug_resume"):
get_tree().paused = false
pause_next_frame = false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment