General Problems with Roles and Memory



  • I'm not sure if this has been addressed elsewhere, but I'm unable to find it if it is.

    I'm having a problem where my screeps aren't getting roles assigned when they are spawned from code. Not sure how well the code will format here, but here are some snippets...

    if(!as_myspawn.spawning && as_myspawn.canCreateCreep([WORK, CARRY, MOVE], ls_creepname, {role: as_creeptype}) == OK)
    {
         var lb_success = as_myspawn.createCreep([WORK, CARRY, MOVE],            ls_creepname, {role: 'harvester'});
         console.log('Creating...')
    }

    I'm not sure if the role is not getting assigned because, well, I'm just not sure. This is, more or less, the same code in the tutorials. So, I thought would attempt to look at the role of the creep to see if it was undefined. If so, I'll assign it. However, I'm getting strange errors that seem to prevent me from assigning roles after the fact.

     

    // Check for unassigned roles
    for(var name in lobj_creeps)
    {
         var creepname = name;
         console.log(creepname);

         // I've tried various forms of this, with no luck
         if(lobj_creeps[name].memory.role === undefined)
        {
              console.log(creepname);
              Game.creeps.creepname.memory.role = 'harvester';
         }
    }

    This gives me an error:

         main:46
         Game.creeps.creepname.memory.role = 'harvester';
         ^
         TypeError: Cannot read property 'memory' of undefined
         at Object.module.exports.loop (main:46:34)
         at __mainLoop:1:52

    It doesn't appear to matter whether I use just the name in a variable or the whole object, I still get this error, or one very similar.

    However, if I use this...

         Game.creeps.HarvesterA6.memory.role = 'harvester';

    A role is assigned. 

    What am I missing here?

    Thanks,

    vt


  • Culture

    I'm assuming you're doing memory cleanup for older creeps, please make sure you do memory cleaning before spawning new creeps. Else your creeps memory will get deleted the same tick again.



  • Thanks Dissi. I saw that issue in another post and made sure that I was deleting creeps from memory before creating/assigning roles. This doesn't seem to be related to that. I assume this is a PEBKAC issue, but I can't figure it out if it is.

    vt



  •  

    i dont think you can do Game.creeps.creepname, and have to do Game.creeps[creepname]

     



  • I think I've run into something similar at one point, I don't know when you are trying to access creep object but if you are trying to access memory of a creep that is being spawned, it might not exist in the Game.creeps hash yet.  Give Memory.creeps[creepname].role a try and see if it is populated.  The new PTR notes have a note about inserting a dummy Creep object into the Game.creeps hash during the createCreep method so it might be fixed in the next version.



  • they syntax for an object in javascript is . . . 

    var myObj = { apple: 'red', banana: 'yellow', cherry: 'red' , gameTime: Game.time, index: 0, isSilly: true};

    you can use this when creating a new creep to define the role and any other necessary variables at creep creation . . . 

    Game.spawns.spawn1.createCreep( [WORK,CARRY,MOVE], '', { role: 'myFirstCreep', load: false, goHere: { x:20, y:20, roomName: 'sim' }});

    As to your implied larger question of performing some tests to make sure your memory isn't borked up somehow, I'm going to leave that nest of snakes alone for the time being since that is a much larger question which I am still struggling with a bit myself yet. 

     



  • @Primeoz56

    Game.creeps.creepname can work, but only if your creep is literally named 'creepname'. 

     

    however if creepname is a string containing your creeps name you need to use this syntax instead.

    let creepname = 'george';
    Game.creeps[creepname].move(TOP);

     

    Note. you can go much farther with this syntax and replace all but the original object.

    Game['spawns']['spawn1'].createCreep([MOVE],'george');