Screeps Wiki

The Game.getObjectById() method has many great uses, and costs very little CPU-wise to convert the string representation of a object id into the corresponding in-game object. Any object descendant of RoomObject (except Flags) have an object id, which is the same id representing the object in the game's back-end database. It will only provide you the object if you have visibility in the room, otherwise, it will return null.

Use with Memory[]

Storing actual game objects is a bad idea, because of the way the Memory stringify/parse process works. It effectively makes it unusable after the end of the current tick. A functional way to do it, is to store the object's ID into Memory, and use the saved ID in conjunction with Game.getObjectById() to retrieve the object in the next tick.

Because Game.getObjectById() is a database lookup using a string it is extremely fast, and is often used by players to store live game objects that will be used repeatedly, such as Sources, hostile Creeps, structures such as walls, etc.

Example Usage[]

creep.memory.sourceId = creep.pos.findClosestByRange(FIND_SOURCES).id;
var source = Game.getObjectById(creep.memory.sourceId);