Make game object properties configurable
-
Hi,
I'd like to encourage to flag properties on game objects as configurable.
Like that:Object.defineProperty(Creep.prototype, 'memory', {
configurable:true,
get:function() {This would make it possible to override existing properties.Best example is the code snippet above. We could implement raw memory access or usage of segments for creeps, using the common memory property. Without having to rewrite every creep memory access in the whole code.Currently we are getting an exception when trying to do that, due to the missing configurable flag:TypeError: Cannot redefine property: memory
-
Hmm, maybe I found a dirty workaround.
// only for testing purposes. Stores all creep memory in Memory.test instead of Memory.creeps
Memory.test = Memory.test || {};
Creep.prototype = Object.create(Creep.prototype, {
memory: {
get: function () {
return Memory.test[this.name] = Memory.test[this.name] || {};
},
set: function (value) {
Memory.test[this.name] = value;
}
}
});But I'd still prefer to override properties the classical way using Object.defineProperty...
Edit: This approach doesn't seem to work for ticks when node recreates objects. Object instances get created before the new prototype can be defined
Or to get even more dirty, we need to recreate such objects in those ticks...
_.forEach(Game.creeps, creep => {
Game.creeps[creep.name] = new Creep(creep.id);
});
-
I've made the same observations and opened a pull request a while ago.
As you might expect I'd also like to see this being changed, but the pull request is still pending.
-
Oh yeah, please merge!