Keep enemies dead after scene changes

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

Hello everyone I hope everyone is well! I’m having a bit of a problem when it comes to keeping my enemies dead after the scene changes from A to B and then back from B to A. I’ve tried using a singleton to store when the enemies die, and while it does register that the enemies have taken a dirt nap, It doesn’t keep them dead when the scene changes back and forth. So my question is how do I keep them dead when I leave the scene and come back? I have some code if it’s needed.

:bust_in_silhouette: Reply From: Giiltham

It will work with singleton.
something you could do is in the ready() function of your mob, make it check if it can find itself in the singleton, if so, trigger the function to kill him.

Thank you for answering :slight_smile: but like I previously stated " I’ve tried using a singleton to store when the enemies die, and while it does register that the enemies have taken a dirt nap, It doesn’t keep them dead when the scene changes back and forth. " I’m just not sure how to call it properly to keep them dead ¯_(ツ)_/¯ That’s my problem here. Regardless I’m pressing on. Perhaps when I learn more code I’ll figure it out until then. c’est la vie!

RakuNana | 2020-08-14 16:33

:bust_in_silhouette: Reply From: RakuNana

Figured it out. Create a singleton and name it Global and give it a var. Name the var enemy__life = 1. Then in your enemy script create a _process function and write:

if Global.enemy_life <= 0:
	self.queue__free

Now all you’ll need to do is create another function that will subtract from Global.enemy__life. And that’s it you are done :slight_smile:

for those that just wanna see the code here you go!

Enemy:

extends Node2D

export var dialog = ["This is test dialog" , "this is new dialog"]


 var character_name : String = "Enemy"
 var lvl : int = 2
 var dialog_index = 0
 var dialog_fin = false
 var can_speak = false


 func _process(_delta):
    if can_speak == true:
	load_dialog()
     if Global.enemy_life <= 0:
	    dead()


 func load_dialog():
pass

func dead():
 Global.enemy_life -= 1
  self.queue_free()


  func _on_Area2D_area_entered(_area: Area2D) -> void:
     can_speak = true

  func _on_Area2D_area_exited(_area: Area2D) -> void:
      can_speak = false

  func _on_Enemy_battle_enemy_died() -> void:
     dead()

Just a note but, this code will effect every single enemy that uses this Global value. Regardless if you killed them or not. So you may want to either use this for special occasions or make an export var so every enemy has their own life values.

Happy coding everyone and Farewell!