Small snippet to clear the memory of dead creeps.



  • This code has been going around since the dawn of Screeps I think originating on the website StackOverflow.

    for(var i in Memory.creeps) {
        if(!Game.creeps[i]) {
            delete Memory.creeps[i];
        }
    }
    

    It simply removes creeps from memory that are no longer living. I usually place this somewhere where it can run every tick.



  • Is this just to free up memory? Or?...



  • I'm using a bit more advanced version. The basics are the same, though it checks if the role of a creep (in my ai, all creeps must have a role) must respawn. If thats the case, it will push the creep to the spawn queue.

    The script is over here:
    https://github.com/avdg/screeps/blob/e98cf551ec3393c2c165d9a465851ee843694d2e/scripts/unit_deathChecker.js



  • Thanks for the tip! but shouldn't this be a game-engine-level thing? there's probably a very limited need to keep tally of dead units, and keeping them in mem shouldn't be the default IMO.



  • @vivri: it's still the best solution in my opinion (unless someone knows better?).

    Yeah, it's confusing, but imagine that the engine passed death creeps the next round only (and not the rounds after) while the bot failed to push orders, then having to conclude the bot never knew creeps died because of it.



  • Also, a creep can contain a lot of information even when its death. Information that could be vital



  • @avdg_ - i understand what you mean, but i think that both for clarity and runtime/memory there can be a separate toplevel collection of dead creeps. This way, you won't have to iterate over all of them to find out who's dead, and you'll be able to set some flag that keeps your dead creeps in mem for several ticks. e.g.:

    SomeConfig.deadCreepsRemainFor = 1 // ticks
    DeadCreeps.forEach (function(creep) { buryWithHonor (creep) })

    This way, you could extract whatever info you wanted about your creep and let the engine do the creep decomposition 🙂



  • It's a great idea I guess but it requires active monitoring from the ai part, though the engine should keep storing death creeps that way untill the ai has done at least 1 successful turn. AI's can time out easily.



  • I'm using something like the OP's snippet as part of a garbage collector, but wanted to share part of my learning curve. When using createCreep with a memory argument, that creeps memory created and available to code for the remainder of that tick. The creep itself isn't available until the start of the next tick.

    My first pass had an OP style GC at the end of my code, and I couldn't figure out why my creeps memory were being wipe before the creep was even being created. What was happening was:

    1. This tick:
      1. createCreep called with memory argument
      2. Memory.creeps.myNewCreep available
      3. GC runs seeing Memory.creep.myNewCreep but not Game.creep.myNewCreep so deletes Memory.creep.myNewCreep
    2. Next tick:
      1. Game.creep.myNewCreep has no memory.

    Moving the GC earlier in the tick solved the problem.

    1. This tick
      1. run GC, myNewCreep hasen't been created yet, so isn't deleted
      2. createCreep
      3. Memory.creep.myNewCreep available
    2. Next tick
      1. Now when GC runs both Memory and Game versions of myNewCreep are available, so it is not deleted from memory.

    TL;DR: If you use this snippet, use it very early in your code.