Godot Version
4.2.1
Question
I have made this HP system and it is done but for some reason, the heal function is not working at all. I use signals between scripts. the damage works but the heal doesnt.
this is the code for item:
var item=null
func use_item(item):
if item["name"] == "beef":
print("Emitting heal signal for beef")
Globalinventory.emit_signal("heal", 10) # Emit heal signal from the global inventory
item["quantity"] -= 1
if item["quantity"] <= 0:
Globalinventory.inventory.erase(item)
Globalinventory.inventory_updates.emit()
func _use_on_button_pressed():
use_item(item)
this is the global HP script:
var player_health: float = 100.0
# Signal to notify when health changes
signal health_changed(new_health)
func take_damage(amount: float):
player_health -= amount
player_health = clamp(player_health, 0.0, 100.0)
emit_signal("health_changed", player_health)
func heal(amount: float):
print("Healing player by:", amount)
player_health += amount
player_health = clamp(player_health, 0.0, 100.0)
print("New global health:", player_health)
emit_signal("health_changed", player_health)
this is the code for heal and hp inside the player script:
func _ready():
Globalinventory.set_player_ref(self)
HpMofatesh.connect("health_changed", Callable(self, "health_changed"))
if Globalinventory.connect("heal", Callable(self, "heal")) == OK:
print("Heal signal connected successfully")
else:
print("Failed to connect heal signal")
call_deferred("connect_to_monster")
func heal(amount: float):
print("Heal function called with amount:", amount)
HpMofatesh.heal(amount) # Use global health
# health = HpMofatesh.player_health
print("New health:", HpMofatesh.player_health)
emit_signal("health_changed", HpMofatesh.player_health) # Emit signal for health change
await flash_green()
func flash_green():
modulate = Color(0, 1, 0, 0.81) # Green color
await get_tree().create_timer(0.3).timeout
modulate = Color(0.816, 0.82, 0.816, 0.51)
await get_tree().create_timer(0.3).timeout
modulate = Color(0, 1, 0, 1) # Green color
await get_tree().create_timer(0.3).timeout
modulate = Color(0.816, 0.82, 0.816, 0.51)
await get_tree().create_timer(0.3).timeout
modulate = Color(0, 1, 0, 1) # Green color
await get_tree().create_timer(0.3).timeout
modulate = Color(1, 1, 1, 1) # Reset to default color
now this heal function doesnt work, but the dmg code works perfectly. this is this is the code for the monster(enemy) give dmg:
func _on_dmg_body_entered(body):
if body.name == "player" and spawn==true:
print("he is in, signal sent")
emit_signal("player_damaged", 20)
this is the code inside the player taking dmg:
func connect_to_monster():
var monsters = get_tree().get_nodes_in_group("monster")
print("Number of monsters found: ", monsters.size()) # Debugging
for monster in monsters:
if monster.has_signal("player_damaged"):
monster.connect("player_damaged", Callable(self, "take_damage"))
print("Connected to monster's player_damaged signal") # Debugging
# Health-related functions
func take_damage(amount: float):
print("Player received damage signal: ", amount)
HpMofatesh.take_damage(amount) # Use global health
health = HpMofatesh.player_health # Update local health variable
if health <= 0:
die()
await flash_red()
func flash_red():
# Set the player's modulate to red
modulate = Color(1, 0, 0, .81) # Red color
await get_tree().create_timer(0.3).timeout
modulate=Color(0.816,0.82,0.816,0.51)
await get_tree().create_timer(0.3).timeout
modulate = Color(1, 0, 0, 1) # Red color
await get_tree().create_timer(0.3).timeout
modulate=Color(0.816,0.82,0.816,0.51)
await get_tree().create_timer(0.3).timeout
modulate = Color(1, 0, 0, 1) # Red color
await get_tree().create_timer(0.3).timeout
modulate=Color(1,1,1,1)
can someone help? I have no idea whats wrong. the signal emits, but the player does not receive it.