Why isn't this dead function working ?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Scavex

So I have made an enemy that shoots bullets. The dead function gets called every time the bullets of type Area2D hit my player. His health is decreasing but when his health is 0 his Collision Shape isn’t getting disabled. The Particles get emitted when his health is 0 which means the collision shape should get disabled and his motion should also be zero. But I can control him even when his health bar is all red. This is my dead function :

func dead(): 
	playerhp-=1
	if playerhp<=0:
		print("Player with 0 health")
		is_dead==true
		if emit == false:
			$Particles2D.emitting = true
			emit = true
		$Sprite.play("PlayerDead")
		motion=Vector2(0,0)
		$CollisionShape2D.disabled = true

And this is the script for the Bullets that enemy is firing :

extends Area2D

const SPEED =300
var velocity = Vector2()
var direction=1

func _ready():
	pass

func set_bullet_direction(dir):
	direction = dir
	if dir==-1:
		$Sprite.flip_h=true
		
func _physics_process(delta):
	velocity.x=SPEED*delta*direction
	translate(velocity)

func _on_VisibilityNotifier2D_screen_exited() -> void:
	queue_free()

func _on_Bullet_Advance_body_entered(body: Node) -> void:
		if "Player" in body.name:
			body.dead()
		queue_free()

I can’t see any mistake. Maybe something different is impeding?

MaaaxiKing | 2020-06-18 14:38

Yes it’s frustrating because the function is actually working since "Player with 0 health " is getting printed once my player health reaches zero but strangely enough the playerhp is going below 0 to negative values which is confusing

Scavex | 2020-06-18 14:43

:bust_in_silhouette: Reply From: Sinowa-Programming

Have you tried .kill instead of disabled?

Yes that gives me an error. I just wanna know why the values are going in negative when I am giving an if statement. Very frustrating

Scavex | 2020-06-18 15:09

You need to get rid of one equal sign from is_dead==true to is_dead=true

Sinowa-Programming | 2020-06-18 15:22

I did this and now my player’s death sprite is working but he gets stuck instead of falling down. My player dies when his velocity along y axis gets greater that 1000. So it’s necessary for him to fall

Scavex | 2020-06-18 15:27

I don’t understand. Do you want the character to fall through the map into the void?

Sinowa-Programming | 2020-06-18 16:40

I just can’t tell you what happened bro! I set is_dead==true and and the player now dies from the bullets. But after solving this my respawn system which worked earlier is broken now as well as I want the effect that the player fell from screen after getting killed. But wherever he dies the Dead sprite stays there for a second since I am using Timer but I want the sprite to go down in the screen like he fell or something.

Scavex | 2020-06-18 16:50

You should open another question for that issue

Sinowa-Programming | 2020-06-18 17:54