Setting roles
-
I've had a lot of trouble using the memory object to set roles. The thing that's worked has been setting it twice, as below. Any idea why? None of the methods suggested in the documentation work reliably, such as including { role : "guard" } as a parameter in createCreep.
var newCreep = spawn.createCreep(spawn.memory.creepSpecs[myRole]); if(_.isString(newCreep)) { Game.creeps[newCreep]= { memory: { role: myRole } }; Memory.creeps[newCreep] = {role : myRole}; console.log('The name is: ' + newCreep); }
-
Your first attempt isn't valid because you try to overwrite the entire creep object.
This should work:
Game.creeps[newCreep].memory = { role: myRole };However I recommend assigning the role already when you trigger the spawning, as shown in the docs:
var newCreep = spawn.createCreep(spawn.memory.creepSpecs[myRole], null, {role: myRole});
-
Qzar, I've noticed that there were some bugs in the memory assignments using the createCreep method. See my past topic here: http://support.screeps.com/hc/communities/public/questions/201373251-Console-Command-to-spawn-creep-does-not-apply-memory-options
For some reason, using createCreep in the console will erratically apply or skip memory options.
-
Thanks for the suggestion. However, those changes don't work when I try them in the simulator.
This syntax,
'Game.creeps[newCreep].memory = { role: myRole };'
returns an error:
TypeError: Cannot set property 'memory' of undefined
and when I try to set the role as a parameter using the documented pattern:
'var newCreep = spawn.createCreep(spawn.memory.creepSpecs[myRole], null, {role: myRole});'
then the role is undefined when I check it later. I assume there's some delay in initializing the memory object when a creep spawns.Whatever the reason, attempting to set it twice as above works.
-
In the tick, where you call
spawn.createCreep(...)
, Game.creeps[newCreep] does not yet exist (because your creep is not spawned yet!), Memory.creeps.[newCreep] however does (because it already has been initialized)! In the next game tick, both the reference inGame.creeps
andMemory.creeps
exist.
-
Yet if I comment out
//Game.creeps[newCreep]= { memory: { role: myRole } };
and just run it with
Memory.creeps[newCreep] = {role : myRole};
it still returns the role as undefined when I check any creep created thus. I had hoped putting the role assignment in the check to see if the object has been created would delay the assignment until the role parameter is ready. But evidently not. Is there a less redundant way to make this work, like a delayed role assignment? I avoided that so I wouldn't have to iterate through the creeps again and count body parts to determine roles. The way suggested in the documentation has been failing for me in the simulator.
-
You should never tamper with the object itself, but rather set the property:
Memory.creeps[newCreep] = {role : myRole}; // should be if (Memory.creeps[newCreep]){ Memory.creeps[newCreep].role = myRole; }
There is a huge difference between these two statements! The first one basically destroys the existing object and overwrites it with an object which contains one property (your role), while the second one "extends" your existing object with a new property
role
!