0 votes

Hello guys so what I want to ask is that I made a scene called Enemy Scene which consists of my basic enemy for my game. Earlier I used CTRL + D to make copies of my enemy and then used them in my Main Scene but recently I added a life bar on top of my enemy. Now when I use simple copying, killing one enemy kills all other enemies too. How can I fix this ?

in Engine by (416 points)

1 Answer

+1 vote

That shouldn't happen. Where is the health variable stored? If it is stored in a global script, then the variable is linked to all the enemies. If it it stored in each individual one (stored in a script attached to the enemy) then they should die individually. Perhaps you have a problem with collision detection. The question needs more context. How do you kill the enemies? Where is the Health variable stored?

by (83 points)

This is my enemy script :

var direction=1

var is_dead=false
export(int) var hp=10


func _ready() -> void:
    pass # Replace with function body.

func dead():
    hp-=1
    if hp<=0:
        is_dead=true
        velocity=Vector2(0,0)
        $AnimatedSprite.play("Dead")
        $CollisionShape2D.disabled=true
        $Timer.start()


func _physics_process(delta):
    if is_dead==false:
        velocity.x=SPEED*direction
        if direction == 1:
            $AnimatedSprite.flip_h=false 
        else:
            $AnimatedSprite.flip_h=true
        $AnimatedSprite.play("Corona")
        velocity.y+=GRAVITY
        velocity=move_and_slide(velocity, FLOOR)

    if is_on_wall():
        direction=direction*-1
        $RayCast2D.position.x*=-1

    if $RayCast2D.is_colliding()==false:
        direction=direction*-1
        $RayCast2D.position.x*=-1

    if get_slide_count()>0:
            for i in range(get_slide_count()):
                if "Player" in get_slide_collision(i).collider.name:
                    get_slide_collision(i).collider.dead()


func _on_Timer_timeout() -> void:
    queue_free()

and this is my script for Progress Bar which appears on top of my Enemy :

extends ProgressBar

func _ready():
    pass

func _physics_process(delta):
    value=get_tree().root.get_node("World").get_node("Enemy").hp

All this is fine but I want to make multiple copies of my same enemy for my game stage ! Simply copying over the Enemy node isn't giving me desired results as the enemies do appear and can be destroyed too but their life bar doesn't change and stays full except the one enemy from which all others were copied. That's probably because the copies have numbers added to their names like enemy1,enemy2 and so on and I must change the name in progress bar script. But If I change the name of node there than still the issue prevails because now let's say that enemy1 is working as desired but enemy is showing full life bar. Is there any way I can make copies of enemy and make the life bar interact with each of them separately and not simultaneously

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.