Not sure if I do this correctly, please help me understand.

Let's say I have an enemy_class.gd and 2 enemy scripts extending from it.

enemy_class.gd
enemyA - enemyA.gd #extents enemy_class.gd
enemyB - enemyB.gd #extents enemy_class.gd

enemy_class.gd has a death function.

if health <= 0:
queue_free()

What if when dead I also want only enemyB to print("B is dead")? Can I do this from enemy_class.gd? or do I have to move the death function to enemyB.gd and do it from there.

Thanks

  • This is only a guess, since I don't know how your project is organized.

    You could add a property to enemy_class.gd:
    bool announce_my_death

    Set the property true for enemies for which the print() will execute, and false for other enemies.

    Then in enemy_class.gd:

    func death():
        if announce_my_death:
            print("%s is dead" % name) # Replace name by actual enemy name.
            queue_free()

oops never mind. I can just use an actual death function to enemy_class.gd then add print() to it in enemyB.gd

enemy_class.gd

if health <= 0:
death()

func death():
queue_free()

enemyB.gd

func death():
print("B is dead")

    Gowydot how does it even work?

    O mena if you re-define the func death() doesnt it overwrite the class definition of the method?

    And if not, then how does it execute? Does it execute the class definitions first then your re-defined print statement or the other way around?

      kuligs2
      Sorry I looked at my code again and it's like this:

      enemy_class.gd

      func death():
      queue_free()

      enemyB.gd

      func death():
      queue_free()
      print("B is dead")

      It works for what I want but probably not the way to do it. I found another method using group but have not yet tested on my project. I think that's the better way.

      This is only a guess, since I don't know how your project is organized.

      You could add a property to enemy_class.gd:
      bool announce_my_death

      Set the property true for enemies for which the print() will execute, and false for other enemies.

      Then in enemy_class.gd:

      func death():
          if announce_my_death:
              print("%s is dead" % name) # Replace name by actual enemy name.
              queue_free()

        DaveTheCoder

        Thank you! I'm using it to signal camera back to player's position.

        enemy_class.gd

        signal b_is_dead
        var test : bool
        
        func enemy_death():
        	queue_free()
        	if test:
        		emit_signal("b_is_dead")
        		print("TRUE")
        	else:
        		print("FALSE")

        enemyB.gd

        func _ready() -> void:
        	test = true
        • xyz replied to this.

          Gowydot

          class_name Enemy
          
          func death():
          	queue_free()
          class_name EnemyB extends Enemy
          
          func death():
          	print("B is dead")
          	super()

            xyz
            Ohhh that works too, interesting! Thanks!

            • xyz replied to this.

              Gowydot If you just can't live without class inheritance - that's the "proper" way to do this.