Creeps spawning without memory



  • Hi guys,

    Recently my spawning code has been starting to act wierd where sometimes my creeps spawn without ANY memory, and it keeps doing that until my whole colony starves itself. The code I use:

    Game.spawns.Spawn1.createCreep([CARRY,CARRY,CARRY,CARRY,MOVE,MOVE], undefined, {role: "filler", bkprole: "filler", SourceRoom: spawn.room.name})

    Where it was never crashing until I added , SourceRoom: spawn.room.name

    spawn variable is being passed in a hardcoded Game.spawns.Spawn1 and I don't lose control of it so it shouldn't be that.

    I do run out of CPU sometimes, though it keeps happening even after my CPU levels are stable because everything is dead.

    I'm wondering if anyone knows what could be the problem and why it loses the entire memory for a creep?

    Thanks,

    Primoz



  • ok i have found this is related to the cleanup code (as taken from tutorial) for(var name in Memory.creeps) { if(!Game.creeps[name]) { delete Memory.creeps[name]; console.log(name + ' has died.'); } } This code is deleting their memory as soon as they are getting spawned... Anyone know what is wrong with it? :s



  • I've had the same issue, it seems that creep memory happens 1 tick after that the creep has been 'created', testing needed.


  • Culture

    You have to do your memory cleanup before anything else. Else your unspawned creeps memory will be deleted.



  • To clarify, this happens because createCreep does not start spawning (and thus creating the creep object) until the next tick, but does already create the memory entry. Your cleanup code then sees a memory entry without a creep and deletes it.

    Thus, if you put the cleanup code before createCreep, it'll only be executed a tick later when the creep object already exists, fixing your problem.



  • I am pretty sure this is because it deletes all the creep memory that isn't attributed to any alive creep. The creep you're spawning is not alive yet, so it deletes the memory. But that wouldn't really explain why it worked correctly in the first place, without SourceRoom.

    An easy but a messy fix would be adding the role, source room and such only when it doesn't have one, i.e. role is undefined or if you want creeps with no role, then add an another tag, like thisCreepGotSpawnedAndGotTagsAppliedCorrectlyOrWhatever and, again, check if it's undefined and define it if it isn't.



  • From what i could observe in my rooms, Amadox got it right. I expanded to a second room, thus, i had a second spawn doing the spawn routine. This included the cleanup. So the first room gave order to spawn a creep, Memory was created, but the creep was not yet spawning. Then within the same tick Spawn2 would do the same, deleting the Memory Spawn1 set up.

     

    My fix was to take the cleanup and put it in a seperate function which is called seperately, only once when required, before any spawns do their magic.



  • yes thats exactly what i done and exactly the reason i came across the issue, but it feels a bit silly that you can access a creeps memory that has not yet been created while the tick after it does exist even though it still hasnt been spawned. just feels like flawed logic



  • I think the logic is in the way the game loop works. The Memory is yours, and you can act on it. You can manipulate the same Memory object several times in a row, and the data is written instantly always. Spawning and World-affecting stuff (i guess everything they declared with the const cost of 0.2CPU) could be seen as being acted between gameticks. Your code is preparing it, telling everyone what to do, but they don't do it until your code is complete. So the spawner does not start spawning until the end of the tick (although it already got the command earlier). Until then, it leaves us with the state of the creep's Memory existing before the creep itself (kinda philosophic, isn't it?)

     

    Edit: Another thing i stumbled upon is the cancelOrder(methodName) method of the creep. This might be a reason why things work as they work. It seems like you can gain more control by being able to cancel the Orders you gave in your code before they are executed.