Store references to game objects on global



  • With the new isolated virtual machine we now have a global environment which can survive for many ticks without being reset. What I am wondering is, what happens if we hold onto a reference to a game object across multiple ticks?

    The documentation specifically states that we cannot store game objects in Memory, but doesn't say anything about caching references to game objects on global. I did a brief test with a reference to a Room and it appeared to work, but I am not sure how safe this is.

    Has anyone else tried this?

    As a 10 CPU player with 9 rooms my largest CPU expense is loading all the room objects each tick. If would be great if I could cache lists of references instead of having to use getObjectById.



  • The objects you'd be holding onto will go stale, the next tick they will not contain current information anymore. Additionally holding onto game objects will put a very large pressure on your heap. Perhaps on 10 CPU this is not immediately noticable but the game object can be quite bulky, having references to multiple generations of them will cause out of memory resets after awhile. Most objects internally hold references to other related objects, so a reference to one can keep a whole graph of objects from becoming eligable for garbage collection.

    There was talk of Screeps at some points getting reusuable game objects, but we're it's not there yet.