Thank you both for your answers. That really cleared things up!
Posts made by Jupiter1
-
RE: Class instance in creep memory forgets his members
-
Class instance in creep memory forgets his members
As a test, I created the following code:
if (!MyClass) { console.log('define class'); var MyClass = class { run() { console.log('AHAH'); } } } module.exports.loop = function () { console.log('tick'); let spawn = Game.spawns['Spawn1']; if(!spawn.spawning) { console.log('spawn'); spawn.spawnCreep([MOVE], 'myCreep', { memory: { myObj: new MyClass() } }); } for (let creep in Game.creeps) { console.log(creep, JSON.stringify(Game.creeps[creep].memory)); Game.creeps[creep].memory.myObj.run(); } }
This is the output:
[00:44:03]define class [00:44:03]tick [00:44:03]spawn [00:44:03]myCreep {"myObj":{}} [00:44:03]AHAH [00:44:04]tick [00:44:04]myCreep {"myObj":{}} [00:44:04]TypeError: Game.creeps[creep].memory.myObj.run is not a function at Object.module.exports.loop:25:41 at __mainLoop:1:19079 at eval:2:4 at Object.r.run:2:143185 [00:44:05]tick [00:44:05]myCreep {"myObj":{}} [00:44:06]TypeError: Game.creeps[creep].memory.myObj.run is not a function at Object.module.exports.loop:25:41 at __mainLoop:1:19079 at eval:2:4 at Object.r.run:2:143185 [00:44:15]define class [00:44:15]tick [00:44:15]spawn [00:44:15]myCreep {"myObj":{}} [00:44:15]TypeError: Game.creeps[creep].memory.myObj.run is not a function at Object.module.exports.loop:25:41 at __mainLoop:1:19079 at eval:2:4 at Object.r.run:2:143185 [00:44:30]define class [00:44:30]tick [00:44:30]spawn [00:44:30]myCreep {"myObj":{}} [00:44:30]TypeError: Game.creeps[creep].memory.myObj.run is not a function at Object.module.exports.loop:25:41 at __mainLoop:1:19079 at eval:2:4 at Object.r.run:2:143185 [00:44:32]define class [00:44:32]tick [00:44:32]spawn [00:44:32]myCreep {"myObj":{}} [00:44:32]TypeError: Game.creeps[creep].memory.myObj.run is not a function at Object.module.exports.loop:25:41 at __mainLoop:1:19079 at eval:2:4 at Object.r.run:2:143185 [00:44:33]tick [00:44:33]spawn [00:44:33]myCreep {"myObj":{}} [00:44:33]TypeError: Game.creeps[creep].memory.myObj.run is not a function at Object.module.exports.loop:25:41 at __mainLoop:1:19079 at eval:2:4 at Object.r.run:2:143185 [00:44:42]define class [00:44:42]tick [00:44:42]spawn [00:44:42]myCreep {"myObj":{}} [00:44:42]TypeError: Game.creeps[creep].memory.myObj.run is not a function at Object.module.exports.loop:25:41 at __mainLoop:1:19079 at eval:2:4 at Object.r.run:2:143185 [00:44:50]tick [00:44:50]spawn [00:44:50]myCreep {"myObj":{}} [00:44:50]TypeError: Game.creeps[creep].memory.myObj.run is not a function at Object.module.exports.loop:25:41 at __mainLoop:1:19079 at eval:2:4 at Object.r.run:2:143185 [00:44:51]tick [00:44:51]spawn [00:44:51]myCreep {"myObj":{}} [00:44:51]TypeError: Game.creeps[creep].memory.myObj.run is not a function at Object.module.exports.loop:25:41 at __mainLoop:1:19079 at eval:2:4 at Object.r.run:2:143185
I have several questions about the behaviour of this code.
The first is that it seems that the object
myObj
forgets all his members the next tick after it started spawning. Why is that? Does this has something to do with the lifetime of theMyClass
class object or something (I'm a JS newbie)? How do I deal with this? Storing classes themselves in memory?Secondly, and this question propably ties in with the first, why is
MyClass
defined just sometimes? Why not either just once or always?