ERR_INVALID_TARGET in very basic harvester code

  • I have very basic harvester code that does nothing but store a path and target in memory, moves to the node if it can't harvest yet, and does nothing otherwise.

    I'm storing the result of "findClosestByPath(FIND_SOURCES_ACTIVE)" into memory, then passing that to "findPathTo". Both of these functions work just fine. They both return an object and array respectively. 

    However, whenever I tried to get the creep to harvest() the target, it gives me ERR_INVALID_TARGET. I'm not sure what else can cause this so I'm posting here.

    I'm no expert in Javascript. It could possibly be something wrong with my code that I'm simply overseeing. Any help in indicating the problem would be appreciated. 

    EDIT: For what it's worth, I've verified in the memory viewer that the location for the target is a valid energy source. If you'd like me to give a screenshot, I can... although, I don't see why the search function would give me an object that is apparently invalid. 

  • Actually, it seems the object is invalidated every tick. But I can store the unique ID of the source and fetch by that each tick (although it uses more CPU). I guess that'll have to do for now. 

  • Storing the ID is the correct way to do it.

  • Get you use Game.getObjectById(), you are pulling a live reference to the object that is connected to the world.

    When you retrieve an object that you saved into memory you are looking at a serialized snapshot of that object as it looked when you last saved it, perhaps many ticks ago.

    That's why you need to store the ID and pull the live object with Game.getObjectById()