code caching?



  • Hi,

    I'm a newbie with basic JavaScript skills and had a weird issue in my latest room/spawn:

    I tried to be 'clever' and write a module which stores all possible targets for creep actions in an object once, instead of repeatedly finding targets for each creep. So far so good. Requiring the targets module in the main module and passing the targets object as an argument to the run functions of the creep roles seemed to work at first.

    But then I noticed my creeps stopping in their tracks and restarting randomly. Sometimes they stayed idle until they expired.

    E.g. my 'worker' creeps would normally build construction sites if there are any and upgrade the controller the rest of the time. But the room's construction sites in the targets object/module didn't seem to update properly.

    I could get the creeps to 'restart' by manually saving my script.

    What causes this? The targets object should be updated with each tick. No state needs to be maintained which could be lost...

    Could this be because of code caching at the back-end?

    I don't see this as a bug and will rewrite my code to work around this. But I'm curious what caused this issue.

    Thanks!

    mar


  • Culture

    Ye it's due to how the server manage to scripts. What you have to do is store the object id of every game object you want to store for longer than a tick. And when you want to access the object use Game.getObjectById(id);



  • http://support.screeps.com/hc/en-us/articles/204825672-New-main-loop-architecture

     

    This basically means the only code that runs every tick is the code inside (or called by) your main.loop function. The reason this works (or resets) every time you save code, is because committing code is one of the things that causes a new global to be created (because things could have changed).