func give_health(_health, max_health):
_health += (max_health / 4)
func _on_body_entered(body):
if body is Player_base:
if body.health <= body.health_max - (body.health_max / 4):
give_health(body.health, body.health_max)
queue_free()
Question
My health pick up won’t give health to my player. I know it somewhat works because it deletes itself after being touched but it won’t give the player health.
give_health is a function inside of the health pickup. It doesn’t seem to actually do anything to the player’s health. The function has two parameters: _health and max_health. When called, parameter _health is incremented by some value, but _health is then not actually used for anything.
What you probably want to do is add some sort of health variable to your player script, then increment that. Example:
class_name Player extends CharacterBody2D
@export var health : int = 0
class_name HealthPickup extends Area2D
func _on_body_entered(body)->void:
if body is Player:
body.health += 10 # Or whatever logic you please.
I’d suggest @TokyoFunkScene 's solution, and have _on_body_entered() in the health pickup call a function on the player.
class_name Player extends CharacterBody2D
@export var max_health: int = 100
@export var health: int = max_health
func hurt(amount: int, debug: String) -> void:
health = maxi(health - amount, 0)
print("Got hurt by " + debug)
if health == 0:
oh_no_am_dead()
func heal(amount: int, debug: String) -> void:
health = mini(health + amount, max_health)
print("Got healed by " + debug)
class_name HealthPickup extends Area2D
func _on_body_entered(body) -> void:
if body is Player:
body.heal(10, "Health Pickup " + str(self))
Making it a function means that you can isolate things like clamping health to max health inside the player code rather than needing to duplicate that anywhere that touches the player. It also means you can thread debug tracking through stuff. A transcript of what happened can be helpful when you’re trying to fix obscure bugs.
There seems to be something weird going on. The pick up does give health but only states the new health in the print; not the health bar. Also say if the player has 150 hp and then uses the health pick up to get to 200 hp; the next time the player takes damage it goes to 140 hp (the attack does 10 damage.)
Basically the current value of body.health is passed to your function give_health_medium, meaning _health has the same value but is a different variable. Any changes to _health aren’t reflected back to the health variable on body.
This is why the print statement prints the correct value as _health has been updated, but the health bar doesn’t as body.health is still the original value.
Hopefully that makes sense? To fix this I’d follow the advice from @hexgrid
Edit: Sorry read your updated code and see you’ve already followed their advice!
You just need to remove the parameters passed to the give_health_medium function and use the variables from the Player directly.
func give_health_medium() -> void:
health += (max_health / 4)
if health > max_health:
health = max_health
print("Player " + str(player_id) + ": " + str(health))