screepers/Screeps-Typescript-Declarations

Problem with the Game arrays

Strategic-Link-Consulting opened this issue · 5 comments

Thanks for creating and keeping this updated.

I noticed issues compiling and I think I see the problem but I'm pretty new at this.
It's the declarations for Game.rooms/flags/creeps etc. Instead of just an array, shouldn't they be like this?

  creeps: {[creepName: string]: Creep};
  rooms: {[roomName: string]: Room};
  spawns: {[spawnName: string]: Spawn};
  structures: {[structureId: string]: Structure};

What kind of compile time error you're getting and what line calls it out?
The array solution works fine for me. Both for IDE checking and while compiling.

error TS7015: Element implicitly has an 'any' type because index expression is not of type 'number'

applyBehavior(name: string, role: CreepRole) {
  var p0 = performance.now();
  var creep = Game.creeps[name];
  var roleCreeps = Memory.minions[creep.memory.role];
  this.minions[role].applyBehavior(name);
  if (roleCreeps != null) roleCreeps.creeps.splice(roleCreeps.creeps.indexOf(name));
  Memory.minions[role].creeps.push(name);
}

I was testing around a bit and I think you're right. If we only have array, then it should work in most cases, but your solution is better. I'll change the API.

Thanks for your help.

Although, I think you have a bug in your code. With using creeps.indexOf(). If I understand correctly then creeps is an object and not an array. So you can't use splice or indexOf() with it.

Updated the API. Run npm install to get the updates.

Thanks, I'm just learning Typescript. Chose to learn it by applying it to Screeps.
RR