I am having a hard time trying to figure out how to extend the creep class to add my own functions in the new javascript mmo game, Screeps -> www.screeps.com
2 Answers
A bit of an old thread, and I am not sure if Screeps has changed since the initial query was posted, but here are my thoughts...
Why have a wrapper class?? why not extend the original / game provided Creep class ?
e.g.
Creep.prototype.myFunction = function(target){
// my logic
}
Make sure to check out the screeps inheritance structure.. (Google the screeps API and check out the 'Prototypes' section on the landing page)
This can save a lot of duplicate code, for example one declaration of an inherited function in the 'Structure' prototype may save a seperate declaration for each individual Structure sub class protocols.

- 3,731
- 22
- 33
- 46

- 71
- 1
- 1
-
You can also add properties using Object.defineProperty – jfren484 Apr 20 '17 at 19:31
I dunno how to do that, but I created a wrapper class like this one:
You created a function for calling memory, and try to use it s property. See below: var _ = require("lodash");
function MyCreep(creep){
this.creep = creep;
this.memoryProp = creep.memory;
}
MyCreep.prototype.memoryFunc = function(){
return this.creep.memory;
};
MyCreep.prototype.moveTo = function(target){
this.creep.moveTo(target);
}
MyCreep.prototype.myFunction = function(target){
//TODO something
}
So when I need to deal with creep, I do:
var myCreeps = [];
for (var creep in Game.creeps){
creep.memory.role = "hello memory";
var myCreep = new MyCreep(Game.creeps[creep]);
myCreeps.push(myCreep); ;
console.log("original creep memory: "+creep.memory.role);
console.log("my creep memory func: "+myCreep.memoryFunc().role);
console.log("my creep memory prop: "+myCreep.memoryProp.role);
}
or
var myCreeps = [];
_.forEach(Game.creeps, function(creep){
var myCreep = new MyCreep(creep);
myCreeps.push(myCreep);
});
and then deal with myCreeps, locally stored.

- 82
- 5
-
1Pretty sure you should be doing `new MyCreep(Game.creeps[creep]);` – Andrew Leap Nov 22 '14 at 21:03
-
Oh yes, you.re right. Originally, I use lodash _.forEach(), which returns not the creep name, but th creep object itself. I updated my answer. – kamiLL Nov 23 '14 at 05:05
-
How do I handle basic creep functions after this? For example mycreep.memory.role is now invalid. I even tried to create a My Creep.prototype.memory = function() { return this.creep.memory } but it was never called – Ryan Nov 23 '14 at 21:10