Search code examples
javascriptscreeps

Screeps: store reference to source in creeps memory?


Fascinating game!

Looking for an example of how to persist a reference to a particular energy source in a creep's memory. Seems that storing the actual source object won't work(?).


Solution

  • You can't store object instances, but you can store their IDs.

    if(!creep.memory.targetSourceId) {
        var source = creep.pos.findNearest(Game.SOURCES_ACTIVE);
        creep.memory.targetSourceId = source.id;
    }  
    

    And then you can use Game.getObjectById() to find this particular source.

    var source = Game.getObjectById(creep.memory.targetSourceId);
    creep.moveTo(source);