[Solved] Property pos empty on creep in remote room
-
Ok I used to have creeps in remote rooms but I don't remember if I already used the pos property on them while outside a controlled room.
I assumed that you have no access to Room objects when you have no structures or creeps in the room, but once you get a creep there you should have access to the correspondand Room object.
Now I have a creep in an uncontrolled room, and when I try to access its pos property I get an error stating that the creep itself is not defined.
Here's the snippet:
try {
var source = this.pos.findClosestByPath(sources);
} catch(err) {
console.log(err.message);
console.log(JSON.stringify(this));
this.say("Exception!");
return false;
}Note that "this" is a Creep object, since it's working in a prototype extension, and the rest of the code is treating "this" as a Creep correctly.
The console log prints:
Cannot read property 'pos' of undefined
{"room":{"name":"E38N57","mode":"world","energyAvailable":0,"energyCapacityAvailable":0},"pos":{"x":42,"y":20,"roomName":"E38N57"},"id":"57bf311759ad49491ec636ac","name":"Caden","body":[{"type":"work","hits":100},{"type":"carry","hits":100},{"type":"move","hits":100}],"my":true,"owner":{"username":"Frhay"},"spawning":false,"ticksToLive":1047,"carryCapacity":50,"carry":{"energy":0},"fatigue":0,"hits":300,"hitsMax":300,"saying":"Exception!"}So it means that it consider "this" as undefined, since it cannot access pos property, and at the same time it prints me the whole creep structure, meaning that it's actually a Creep object.
What am I missing?
Thanks in advantage for the useful answers!
-
It might not be this.pos that it's failing to read, I'm guessing findClosestByRange() also needs to look at the pos value of every entity in the array it's passed, and then it's dying inside the findClosestByRange function. How are you populating "sources"?
You could probably use the FIND_SOURCES constant instead, and it looks like that's probably what you wanted to do.
-
Well the exception error message is "Cannot read property 'pos' of undefined" meaning that undefined is the element on which I'm calling the pos property.
But your point could be valid, if the js stack trace is not accurate enough, and by experience I know they are sometimes not really accurate actually.
That said I'll run a further investigation about it, checking if my "sources" variable is loaded or not (by the way it's a cache of FIND_SOURCES done by room).
-
Ok, my bad, turns out you were right Spite, the sources variable was actually empty and the script was dying inside the finder function.
I really didn't realize my new caching system was failing so hard for new rooms, good to know.
Many thanks to you my friend. May your storage be always full of useful stuff!
-
It may be because you cached it before you had vision of the room? That would result in an array of valid room objects ... just with a bunch of those fields pointing to "undefined", which would cause exactly the result you saw.
And no problem, glad I was able to help.
-
The actual problem was that I use a list of "sources" dependent to the creep role, and long story short, when the storage was missing (like in a new room) the correspondant "source" element wasn't null, but an array with null as element, so the find function was trying to find the pos property of that null element.