From 16fba6caed0bd0fb3b8fd99ee5b60bd13b6d42d6 Mon Sep 17 00:00:00 2001 From: yuki Date: Fri, 14 Nov 2025 21:24:57 -0300 Subject: [PATCH] refactor state class to fix ghost update issue --- scenes/classes/player_state.gd | 10 ++++---- scenes/classes/state.gd | 44 ++++++++++++++++++++++++++++++---- 2 files changed, 44 insertions(+), 10 deletions(-) diff --git a/scenes/classes/player_state.gd b/scenes/classes/player_state.gd index 82a3642..3059f4d 100644 --- a/scenes/classes/player_state.gd +++ b/scenes/classes/player_state.gd @@ -12,21 +12,21 @@ func _ready() -> void: assert(player != null, "The PlayerState state type must be used only in the player scene. It needs the owner to be a Player node.") -func enter(previous_state_path: String, data: Dictionary = {}) -> void: +func _enter(_previous_state_path: String, _data: Dictionary = {}) -> void: pass -func exit() -> void: +func _exit() -> void: pass -func handle_input(_event: InputEvent) -> void: +func _handle_input(_event: InputEvent) -> void: pass -func state_update(_delta: float) -> void: +func _state_update(_delta: float) -> void: pass -func state_physics_update(_delta: float) -> void: +func _state_physics_update(_delta: float) -> void: pass diff --git a/scenes/classes/state.gd b/scenes/classes/state.gd index 4b6ba30..20ba1f1 100644 --- a/scenes/classes/state.gd +++ b/scenes/classes/state.gd @@ -6,19 +6,53 @@ @warning_ignore("unused_signal") signal finished(next_state_path: String, data: Dictionary) + +## For 'ghost frame' update fix +var is_active: bool = false : + set(value): + is_active = value + get: + return is_active + + ## Called by the state machine when receiving unhandled input events. -@abstract func handle_input(_event: InputEvent) -> void +@abstract func _handle_input(event: InputEvent) -> void ## Called by the state machine on the engine's main loop tick. -@abstract func state_update(_delta: float) -> void +@abstract func _state_update(delta: float) -> void ## Called by the state machine on the engine's physics update tick. -@abstract func state_physics_update(_delta: float) -> void +@abstract func _state_physics_update(delta: float) -> void ## Called by the state machine upon changing the active state. The `data` parameter ## is a dictionary with arbitrary data the state can use to initialize itself. -@abstract func enter(previous_state_path: String, data: Dictionary = {}) -> void +@abstract func _enter(previous_state_path: String, data: Dictionary = {}) -> void ## Called by the state machine before changing the active state. Use this function ## to clean up the state. -@abstract func exit() -> void +@abstract func _exit() -> void + + +func handle_input(event: InputEvent) -> void: + if not is_active: return + _handle_input(event) + + +func state_update(delta: float) -> void: + if not is_active: return + _state_update(delta) + + +func state_physics_update(delta: float) -> void: + if not is_active: return + _state_physics_update(delta) + + +func enter(previous_state_path: String, data: Dictionary = {}) -> void: + is_active = true + _enter(previous_state_path, data) + + +func exit() -> void: + is_active = false + _exit()