Im making 2d platformer game using godot. My player is made with node based state machine and health/damage is controlled by hitbox/hurtbox class. by using hitbox/hurtbox class, player will take damage as soon as enemy hit player and func player_in_hurt_state(amount: int):
is called by hurtbox class. This is fine if I want to make player to take damage instantly when hit by enemy or enemy projectile. However, im facing problem when I want to adjust the timing of when the player to take damage. for example, i want to make player to take damage not instantly but after transitioning to certain state, when hit by certain enemies. I tried to implement this, by calling player_in_hurt_state()
inside node state script. However, i get an error such as amount not declared or too few arguments, since state node have no idea what damage value is. For now, I made variable var player_take_damage = false
and decided to call true inside enemy script when I want to make player to take damage. But I feel this is little detour and dumb way. Is there any way to call player_in_hurt_state(amount: int)
inside state node script, or is there any other smarter way to do this?
in Hurt class script:
func _on_area_entered(hitbox: HitBox) -> void:
if owner.has_method("take_damage") and hitbox.is_in_group("bullet"):
owner.take_damage(hitbox.bullet_damage()) #Enemy Take Damage By Bullet
elif owner.has_method("grab_player") and hitbox.is_in_group("player"):
owner.grab_player() #Enemy Grab Player If Hit By Player
elif owner.has_method("grab_by_enemies") and hitbox.is_in_group("enemies"):
owner.grab_by_enemies() #Player Enter Grab State If Hit By Enemies
elif owner.has_method("player_in_hurt_state") and hitbox.is_in_group("enemy_projectile"):
owner.player_in_hurt_state(hitbox.enemy_damage()) #Player Enter Hurt State If Hit By Enemy Projectile
in player script:
func player_in_hurt_state(amount: int):
state_in_hurt = true
print("player take damage")
health = max(0, health - amount)
if health == 0:
state_in_death = true
For now, in state node script: (another problem with this is that damage value cannot be changed based on enemy type)
if player.player_take_damage:
player.health -= 10
print("OUCH")
player.player_take_damage = false