From 49ffa397e7ea3a1565e52317afa4e356b387a011 Mon Sep 17 00:00:00 2001 From: Squinty Date: Fri, 12 Apr 2024 16:14:45 +0000 Subject: [PATCH] Fix damage/health client/server desynchnorization --- components/health_component.gd | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/components/health_component.gd b/components/health_component.gd index 0b4adb2..925198d 100644 --- a/components/health_component.gd +++ b/components/health_component.gd @@ -11,20 +11,28 @@ signal health_zeroed signal health_changed(value : int) func _ready(): - health = max_health + heal_full() area_entered.connect(_on_area_entered) -func damage(amount : int): +@rpc("call_local") +func _damage(amount : int) -> void: health = clampf(health - amount, 0.0, max_health) if health == 0.0: health_zeroed.emit() -func heal_full(): - health = max_health - -func heal(amount : int): +@rpc("call_local") +func _heal(amount : int): health = clampf(health + amount, 0.0, max_health) +func heal_full(): + if not is_multiplayer_authority(): + return + + _heal.rpc(max_health) + func _on_area_entered(area : Area3D): + if not is_multiplayer_authority(): + return + if area is AreaDamageComponent: - damage(area.damage) + _damage.rpc(area.damage)