CreateCreep getting error with passed array

  • Working on a function to build a creep based on a given main body then add repeat body parts until energy is used up or repeated x number of times. When I use the bodyMain: [WORK, CARRY, MOVE] passed to the function it work great. When I create a new array and push() the bodyMain to the array, i get an error ERR_INVALID_ARGS -10 Body is not properly described or name was not provided. Not sure what I am doing wrong with my code below.

    var roleProperty = {
      harvester:{ maxNum: 2,
                  bodyMain: [WORK, CARRY, MOVE],
                  bodyRepeat: [MOVE, CARRY],
                  repeatMax: 2,},
    StructureSpawn.prototype.checkCreepNumbers =
    function () {
            this.createDefaultCreep('harvester', roleProperty['harvester'].bodyMain, roleProperty['harvester'].bodyRepeat);
                  // spawns a creep based on passed parameters
                  // roleName -> role assigned to Memory
                  // mainParts -> base body parts to used
                  // repeatParts -> what parts to repeat
                  StructureSpawn.prototype.createDefaultCreep =
                  function (roleName, mainParts , repeatParts ) {
                    var creepName = roleName  + Game.time;
                    var bodyBuild = [];
                    var creepMaker = 
                              this.createCreep(mainParts, creepName, { role: roleName, working: false });  //WORKS
                    var creepMaker = 
                              this.createCreep(bodyBuild, creepName, { role: roleName, working: false }); // Return -10
                    return creepMaker;

  • push() adds one element to the array for each argument it's given. If that argument happens to be an array, it doesn't add each element; it adds the array itself.

    In this case, you probably want to use the spread operator - try bodyBuild.push(...mainParts) instead. That breaks apart the elements of mainParts and adds each one.

    As an example, take a look at these two pieces code. The first one is what your code is doing right now. The second is what you're trying to do.

    // Option 1
    var foo1 = [1, 2, 3];
    var bar1 = [4, 5, 6];
    // foo1 is now [1, 2, 3, [4, 5, 6]]. It has 4 elements - three numbers and an array.
    // Option 2
    var foo2 = [1, 2, 3];
    var bar2 = [4, 5, 6];
    // foo2 is now [1, 2, 3, 4, 5, 6]. It has 6 elements, all numbers.

  • Thanks for the help. That is what was going wrong.

  • Also, as a side note, while it is not too late, consider switching to Spawn.spawnCreep instead. Spawn.createCreep is deprecated and can be removed at any update.

    All you really need to change the parameter you pass as memory into an object where you attach the new creep's memory to the appropriately named memory key.

    So the creep spawning bit would look like this:

                var creepMaker = 
                          this.spawnCreep(mainParts, creepName, { memory: { role: roleName, working: false } });

    That's all. Just remember, creepName is required for spawnCreep, although you seem to already be providing one already, so should not be a problem.

  • @orlet Thanks.