• General Chat
  • Help on making enemies stay dead after switching scene.

Hi, i'm new in Godot and strated following some tutorials and trying to make some experiments on my own.

I've been trying to solve this problem for 2 days straight now. I have mapA and mapB, a player and some enemies.
Then when the player kills an enemy, lets say on mapA, it dies as it should. Then the player goes on to mapB. But if he comes back to mapA from mapB, the enemies on mapA are back to life.

Help would much be appreciated. Thanks in advance.

The issue is that you are likely unloading and reloading each scene every time, instead you could remove scene A from SceneTree but not unload/queue_free it so it stays in memory the state it was left in.

Better yet, create a save file for your maps per player profile/account and serialize the state of the relevant nodes in each map the player has been to. Save it before you free the scene.

Then when the player revisits the map, you don't just load the map but initialize the states of relevant nodes according to the serialized data in the relevant save file.

This is a more advanced topic however so you need to be a bit more familiar with programming and game development before you jump into this I would say.

    Megalomaniak Thank you for your response. I was guessing that using a save file would be the best option but was unsure.
    Will look more into this. Thank you once again for the insight.

      diogo One way I have found to create permanence is to create an array of values with a different one for each enemy instance.

      For instance, put this in a global autoload named Global:

      var _enemyPermanence = []

      This gets you an empty array. This is what you check to see if your enemy spawns. By exporting, you can make it unique for each enemy in the Inspector.
      Put this in your enemy object:

      export var _permanenceValue : String ## you could also do int, less memory that way

      Then it's a simple check in the enemy. When you defeat them, place their value in the global array. If it has the value, you queue_free them at the _ready() function.
      Like this:

      if Global._enemyPermanence.has(_permanenceValue):
           self.queue_free()
      else:
           pass

      Only caveat to this is that arrays can get big over time and that can affect loading. May want to make separate arrays for areas, objects, blah blah, to keep memory from churning, although I think you have to get pretty big before that happens. Hope that helps. Oh! And with this method, make sure to put a key in before testing or the enemy will remove itself (because NULL is a value the computer recognizes, even in arrays)

        SnapCracklins thank you so much for your detailed explanation, it makes a lot of sense.
        I really like the idea of making multiple arrays for different areas or enemies.
        Thanks a lot once again 😃

        I thought I'd mention this because I've been thinking about this type of problem myself, someone more optimisation minded might cringe at what I'm suggesting but I really don't see why it can't work with modern engines. The thing is, yes, you 'could' separate your maps into zones and have them load up the old fashioned way but how big are your maps? Are they really that large that loading everything in at once would be a big deal?

        The reason I'm bringing this up is because you could potentially fake entering and exiting the maps or not at all but if you wanted to you could just put up a splash screen or a fade in/out black screen effect. This would deal with the problem of persistence pretty easy. People don't realise how powerful even average office PCs are now and it might be worth an experiment.

        I've been prototyping Half Life esque and old school Fallout 3/New Vegas in my head you see and I thought I'd post it up as an idea.

          Lethn I'd be wary of running into floating point precision issues with that one.