Creep Death and Respawn



  • Hi all, first post here just got into the game recently. Not used Javascript really so I went through the tutotial to get some base syntax to take apart and have been slowly learning the engine of the game and how to define things and such.

    My first big issue is finding a way to check if a creep has lived its last tick and automatically recreate it. From there I can plan things out much further. I am not really interested in copy/pasting code since that doesn't help me learn or understand.

    Curious if there are good resources for examples or some ideas to get me started into detecting if a creep has died etc. (I found tickstolive but it doesn't seem to want to spawn the creep if I use that property in a condition. There is a chance my code is just incorrect but just curious, hadn't found other posts about this and maybe Im just thinking along the wrong path for this particular game.

     

    Thanks!



  • Tickstolive is only relevant as long as the creep lives - once it dies, the creep entry is gone.

    Easiest way to go about it is if you name your creeps yourself - then, basically, what you wanna do is check every tick if the creep is still in the array Game.creeps, and if not, you spawn a new one. so, if you named it "Charlie" on spawning it, you can check 

    if(!Game.creeps["Charlie"]) {
    // Spawn Charlie
    }

  • Culture

    I generally iterate through my Memory.creeps and check if that creep exists in Game.creeps. If it doesn't then that means the creep is dead and proper cleanup procedures can be done.



  • There are a lot of different ways to do this.  What I do is have a "master controller" that knows how many and what type of creeps it needs for a specific task (e.g. each source in my room has a harvester controller that knows how many harvesters/couriers it should have).  Each controller keeps a list of IDs for the creeps it owns, and every tick it checks to see if the creep is still alive (`Game.getObjectById()` returns null if the creep is dead).  If one or more creeps are dead, the controller asks for a new creep of the same type to be spawned.

    If instead you would rather have creeps respawn themselves, you can do that by storing a creep's information in memory (mostly the body, since everything else should already be in memory).  When a creep dies, the memory entry still exists.  You can iterate through `Memory.creeps` to find any creep that no longer exists in `Game.creeps`.  Once you've found one or more creeps that are dead, you can respawn them using the data in memory.  Note that you need to do this before you spawn any new creeps in a tick, or it will try to "respawn" those newly-created creeps and likely error out.



  • Easiest solution imo:

    for (creep in Memory.creeps) {
    if (Game.creeps[creep]) {
       // run creep;
       } else {
    Game.spawns.Spawn1.createCreep([MOVE, WORK, CARRY], creep);
    }
    }

    This recreates your creeps and leaves the existing memory settings untouched.