Storing a variable callable from several different creeps.



  • How can I store a variable so that multiple creeps (even of different roles in different modules) can all see it? So far, the only things I can do, I have to re-calc every single tick, and that's incredibly in-efficient. 





  • This is not an easy question to answer because it highly depends on your code architecture, and I have no idea how your code is structured.

    Global variables are bad, 'mmkay. But you can always have a module that all the other modules load, and call it Global or something.

    Global = require("global");

    Global.variable = 3;  // visible everywhere "global" is required, assuming you exported a property of that name.



  • @ayrtep, I got the impression from his question that he meant a variable holding a function, since he asked for one that was 'Callable'. You can't store a function pointer into Memory, as functions are not serialized.



  • MyrddinE, the problem I have with that is it seems that the variables stored in the modules, don't save from frame to frame. They're initialized each and every tick with their default value.

    //dispatcher.constructionSites

    var currConstSite = "invalid";
    var dispatcherConstructionSites = {
    GetConstructionSite: function(room){

    currConstSite = //code to evaluate construction site here.

    }

    }
    module.exports = dispatcherConstructionSites;

     

    //role.builder
    var dispatcherConstructionSites = require('dispatcher.constructionSites');
    var roleBuilder = {
    run: function(creep) {

    var constSite = dispatcherConstructionSites.GetConstructionSite();

    }

    }

     

    This function gives back "invalid" each and every single time I call it. It's as if the variable is never even being set at all.

    The problem is, I'm fluent in C++, but for some reason, I'm lost in Java a lot.



  • Remember (or think) that every tick your script is destroyed and recreated. Don't think of it as persisting between ticks. 

     

    Instead store the information needed to recreate your state between ticks in Memory. 

     

    http://support.screeps.com/hc/en-us/articles/203016642-Working-with-memory

     

    As you can see you could work with your own memory getter and setter, but its better (IMO) to use the standard one and just modify what you store so that next tick you can recover your state from memory. 

     

    for example:

     

    var getConstructionSites = function(room) {

    if(!room.memory.construction_sites) {

    // eval some constructions sites

    room.memory.construction_sites = whatEverYouEvaled

    }

    return room.memory.construction_sites

    }



  • All RAM is reset, every tick. All code outside of exports is only called randomly when the code cache regenerates (which happens after you commit new code, but also randomly at other times).

    The only persistent state is what you store in the Memory object, which is automatically serialized between ticks. You get 2MB of serialized storage; that is all the state you can store.

    You cannot serialize game objects or functions. Because of this the best practice when you want to store a game object (like you are describing) is to store the id. For example:

    -----------------

    let currentConstructionSite = dispatcherConstructionSites.GetConstructionSite();
    creep.memory.myCS = currentConstructionSite.id

    // later

    let myConstructionSite = Game.GetObjectById(creep.memory.myCS)

    -----------------

    The takeaway is that code persists, RAM does not, unless you serialize it into the Memory object (linked by ayrtep above).



  • Oooooh, there's a Memory object outside of the creeps.memory. Didn't know that. TY guys! 🙂