How to make health system for player

my bad. yes that works. but player.player_in_hurt_state(hitbox.damage) still does not work inside HURT state node script.

extends PlayerState

@export var _animation_player: NodePath
@onready var animation_player:AnimationPlayer = get_node(_animation_player)


func enter(_msg := {}) -> void:
	print("state hurt")
	animation_player.play("PlayerHurt")
	player.state_in_hurt = false
	player.player_cannot_move = true
	player.visible = true
	player.can_shoot = false
	Autoload.camera.shake(0.5, 2.0)
	player.velocity.y -= 500
	if player.mark2D.scale.x ==1:
		player.velocity.x = 250 * -1
	else:
		player.velocity.x = 250 * 1
	player.player_in_hurt_state(hitbox.damage)

Why would you expect that to work? Your player’s hurt state does not have access to the hitbox! Also, why do you want that? The hurtbox should already have called that function and applied the correct amount of damage. What’s the point of calling the function here again?

1 Like

well point is, I want to make player to take damage after GRAB state for certain enemies. So i wanted to call that function whenever player enter HURT state. But now i know that wont work. Then how can I make player to take damage after certain state instead of immediately.

If your bullets hold the info about how much damage they deal, but you don’t want your player to take damage immediately, you have to save that damage value somewhere where the player state can still access it later. Adding a new variable to the player for that purpose seems sensible here, let’s call it scheduled_damage.

Then in your hurtbox you can set that variable:

owner.scheduled_damage += hitbox.damage

and later in your HURT state you can use it like this:

player.player_in_hurt_state(player.scheduled_damage)
player.scheduled_damage = 0
1 Like

"Invalid type in function “player_in_hurt_state’ in base ‘Characterbody2D(player)’. Cannot convert argument 1 from Nill to int.”

Then you haven’t added the variable to your player script yet:

var scheduled_damage := 0
1 Like

it worked perfectly. Thank you very much njamster. was extremely helpful. THANK YOU!!!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.