I have a couple of level scenes and each scene has a bunch of enemies. These enemies can be killed and I want to keep
check if an enemy is dead or not and use that when the level scene is loaded. Ideally, when an enemy is defeated I want to store the enemy position and display the corpse there when the level is loaded.

I know that I need to use a singleton to store data globally so my idea has been to create a global script with a list, I called that dead_monsters and whenever one has been killed I add the instance to it. The code for the logic is this:

global
var dead_monsters = []

enemy

func death_logic():
	Global.dead_monsters.append(self)

level

func _ready():
	for enemy in $Main/Enemies.get_children():
		if enemy in Global.dead_monsters:
			enemy.dead = true 

This doesn't work, I think because the enemy instances are freed whenever the scene is changed. What is the best way to get around that?

  • xyz replied to this.

    izNoob Store the dead enemy positions. When the scene is reloaded, instantiate dead enemies at stored positions.

      xyz I thought about that but the issue is that I will then create a living enemy in that position along with the corpse of the fallen one.

      • xyz replied to this.

        izNoob Well, don't generate a living enemy. Generate only a corpse.

          xyz Okay, so, I have an enemy scene that I want to drag to the starting position. And this enemy scene should be stored globally and from there I want to set it to dead or alive. That seems like the most efficient way to organize a larger project and just dragging marker2Ds for the position seems a bit tedious.

          Thanks for the help, by the way 🙂

            izNoob Make a separate scene for the enemy corpse and instantiate that. In global data simply keep an array of vectors that represent positions at which to instantiate the corpses. If you have may scenes, keep a dictionary of arrays, with scene names as dictionary keys.