I haven't found any way to assign hooks to anything, and if I could it would be very useful. Also, how can I check whether a game object still exists? (i.e. hasn't died of age, and wasn't destroyed by an enemy.)
4 Answers
If you mean via API - not possible yet. But you can change your current state and compare it to memorized previous state of the objects. For example, if some creep name in Memory
still presents, but it is gone in Game.creeps
, then something happened.
for(var i in Game.creeps) {
var creep = Game.creeps[i];
creep.memory.lastSeenAt = {x: creep.pos.x, y: creep.pos.y};
}
for(var i in Memory.creeps) {
if(!Game.creeps[i]) {
console.log("Oops! Something happened with a creep "+creep.name+" at "+
Memory.creeps[i].lastSeenAt.x+","+Memory.creeps[i].lastSeenAt.y);
}
}

- 4,487
- 2
- 28
- 35
-
Thanks! Is there currently any way to check for death without worst-case iterating through all the creeps in the game? – btd Nov 21 '14 at 17:12
-
Not yet, but we are thinking on some kind of events storage. We may add it later I hope. Iterating through creeps is not really CPU-consuming operation though. – artch Nov 21 '14 at 17:21
Here is my use case :
- Count the number of hostiles (N) in the current room
- Count the number of alive guards (C)
- If C < N then build another guard
After a while, when using the Room.find(Game.MY_CREEPS)
, I'll get dead guards aswell. Having to filter them all the time is really painfull, and the global Memory
continues to list them. Is there a way to remove dead creeps from the global Memory
object ?
[EDIT] Found this, hope it will help.
for(var i in Memory.creeps) {
if(!Game.creeps[i]) {
delete Memory.creeps[i];
}
}
I run it at the beginning of each tick

- 79
- 5
I forked the script-samples repository and made my own events handling code - https://github.com/pineapplemachine/script-samples/tree/master/hooks
Using that script you can assign initialization, update, and destruction methods to events rather than having to handle things more obtusely.

- 684
- 6
- 15
I don't think so. The API seems to be focused on developing code for the main game loop, like writing code inside a while(true)
. But you can make your creeps do something before they die, for example.
I've created a guard module (just like the harvester module you create on the tutorial). The code below should work:
module.exports = function (creep) {
if(creep.hits<=100) { Game.spawns.Spawn1.createCreep([Game.ATTACK, Game.ATTACK, Game.TOUGH, Game.TOUGH, Game.MOVE], "guard2", {role:"guard"}); }
var targets = creep.room.find(Game.HOSTILE_CREEPS);
if(targets.length) {
creep.moveTo(targets[0]);
creep.attack(targets[0]);
} else{
creep.moveTo(Game.spawns.Spawn1);
}
}

- 1,097
- 15
- 36
-
1In this situation, would the conditional "if(creep.hits<=0)" be a valid way to check if the creep has died? – btd Nov 21 '14 at 16:23
-
-
I've created a guard module (updated the answer) and for some reason "if(creep.hits<=hit_threshold)" doesn't work. – Lucas Azevedo Nov 21 '14 at 16:44
-
2`creep.hits <= 0` is not possible, dead creeps are removed from the game immediately. – artch Nov 21 '14 at 16:54
-
Allright, but it works with a higher threshold. Turns out I ran out of energy on my first try so the Game wasn't generating more creeps. – Lucas Azevedo Nov 21 '14 at 16:58
-
What about if(!creep.hits), or try{var i=creep.hits;}catch(e){is_dead=true;} ? – btd Nov 21 '14 at 17:11