My creep spawning code doesn't work

  • My code is

    create:function(spawn,name,energy,data) { var partNum; var body = []; partNum = Math.floor(energy/250); for(var i = partNum;i > 0;i--) { body.push(WORK); body.push(CARRY); body.push(MOVE); body.push(MOVE); } body = body.sort().reverse() //console.log(body,name,JSON.stringify(data)) var ret = spawn.spawnCreep(body,name,{memory:data}); console.log(ret) return ret; }

    It doesn't work and it returns ERR_INVALID_ARGS.
    Also, in training room, it totally works!....

    • spawn is the structureSpawn object, object type
    • name is name of creep, string type ex) "W0 Spawn1"
    • data is memory of creep, object type ex) {role:"harvester"}
    • energy is the usable energy to spawn creep, int type
    • body is the creep's body array ex) [WORK,WORK,CARRY,CARRY,MOVE,MOVE,MOVE,MOVE]

  • Your code looks good. It could be something to do with the arguments you're passing to create, since the description for ERR_INVALID_ARGS reads:

    • "Body is not properly described or name was not provided."

    Are you sure your name is a string? And if you console.log your BODY, it's an array of parts (numbers)? The only other thing I can think of is that maybe you're passing in too much energy and it's trying to create too many parts, but I haven't personally tested whether you get that error code when trying to create a creep with parts that exceed your energy.

    Long story short, I don't see any problems, it must have to do with the way you're calling it.