Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to assign function hooks to game objects, e.g. run function on init, death, etc?

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.)

like image 750
btd Avatar asked Feb 12 '23 12:02

btd


2 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);
    }
}
like image 127
artch Avatar answered Feb 14 '23 00:02

artch


Here is my use case :

  1. Count the number of hostiles (N) in the current room
  2. Count the number of alive guards (C)
  3. 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

like image 33
P. M. Avatar answered Feb 14 '23 02:02

P. M.