Creep memory object inconsistent?



  • I am new to screeps and have trouble finding explanations on why the memory object on some spawned creeps seems inconistent. The memory gets assigned a task and target variable at creation:

    spawn.spawnCreep( [WORK, WORK, CARRY, MOVE], 'newCreep', {memory: { target: 'source', task: 'harvest'}});
    

    However, when the creep is selected to view its memory, the entry differes to an addition that is later mayde by another script, like changing the task for example:

    if(creep.memory.target == 'spawn') { creep.memory.task = 'transport'; }
    

    Why does the memory seem to differ? One memory object holds the initial JSON data and two other lines hold the current values, which means, it is entered twice. Is this the difference beween a creep 'memory' object and the global memory object, meaning, one entry stores a string whereas the other one is a JSON object? Please clarify, thank you 🙂



  • Hard to say without seeing the whole code. If you have parts of your code that are modifying the creep's memory (EX: Their task) then likely part of your code is changing it before you are able to view it.

    For example, I have made a mistake when trying to check a creep's role and execute the proper script, but instead of putting == I put =. This resulted in all of my creeps (at and after the check) suddenly having the same role (in your case task?), and my spawns then went nuts trying to re-spawn the missing creeps only to result in getting their changed again and flooding my rooms with creeps of the same role.

    I would try console.log(); along your code to see the memory state throughout different steps to see how its behaving. I've always found memory to be very consistent tick-to-tick unless its my own issue.



  • @donatzor Thanks for your reply. Here is the code that will later modify the memory entries:

    if (Game.spawns['SP_001'].energy < Game.spawns['SP_001'].energyCapacity) {
                creep.memory.task = 'transport';
                creep.memory.target = 'spawn';
                creep.memory.path = creep.room.findPath(creep.pos, Game.spawns['SP_001'].pos);
            }
    

    Somehow this particular code will not access the same Json memory object that was passed as a argument when the creep is spawned (see initial post). So my only explanation is that the spawnCreep function will add a whole json object named "memory" to the memory, whereas this code here will set two different strings named "task" and "target"... The strange thing is, that that's exactly the way it is displayed when I look into the memory:

    Memory[5]
       Memory[2]
          task: harvest
          target: spawn
       task: upgrade
       target: controller
       path[8]
    

    Basically there should only be only two strings. I don't know much of javascript, but maybe it is just a very unfortunate way of naming the Json object "memory" in the documentations example:

    https://docs.screeps.com/api/#StructureSpawn.spawnCreep

    Game.spawns['Spawn1'].spawnCreep([WORK, CARRY, MOVE], 'Worker1', {
    memory: {role: 'harvester'}
    });


  • @4nytime You probably also have some logic using those memory values. Double check them for the mistake mentioned by Donatzor.


  • Dev Team

    In your shoes, I'd double-check that you call .spawnCreep() only one time per tick (for each spawner, of course). When you call .spawnCreep() several times per tick for the same spawner, only the last creep will start spawning, but memory entries will be created for each .spawnCreep() call… which can be very confusing.



  • @4nytime That's weird, it looks like its calling creep.memory.memory... Did you check what @o4kapuk said?


  • Dev Team

    creep.memory.memory?

    Make sure you never use .createCreep() (the third parameter of .createCreep() is creep memory object, not an options object)



  • @o4kapuk I know what you mean. So far as a beginner I am only using one spawn in one room. Also .spawnCreep() gets called only once per tick if a certain condition is true.

    @donatzor said:

    @4nytime That's weird, it looks like its calling creep.memory.memory...

    exactly this^

    My only explanation is, that creep.memory.task is another variable then creep.memory.memory.task, as this is a Json object, correct? Drawing the conclusion, setting a string value with = would mean to actually create a variable if it has no been established before, rather than accessing the custom memory.memory object that has been introduced at spawning. The "logical" problem only arises if a varibale has the same name as a member.. xD



  • @o4kapuk said in Creep memory object inconsistent?:

    creep.memory.memory?

    Make sure you never use .createCreep() (the third parameter of .createCreep() is creep memory object, not an options object)

    I think now I understand. So if the creep memory is assigned through .createCreep, it cannot be changed later on?



  • @4nytime no, it can be. Just .createCreep() is an obsoleted method, you should use .spawnCreep() instead.



  • @orlet Yes, I am aware that there is a .spawnCreep() function. It is not yet updated in the tutorial, which is still using .createCreep()