Thanks for the info Estecka. I will look into that as well.
Posts made by grandpa_sam
-
RE: Add functionality to memory objects
-
RE: Add functionality to memory objects
Ok great,
Thanks for having this discussion. I'm at work so I can't test this stuff but you have given me multiple paths to explore so I'm going mark this as resolved.
Thanks again for your help.
-
RE: Add functionality to memory objects
Ok I have a question on your edit. Are you saying that while technically Object.setPrototypeOf would fix my problem, I shouldn't use it and instead should try a different line of thinking?
I'm not at all trying to save anything but the data inside the objects but I would like to be able to keep my key functionality pertaining to the data in those objects in one place preferably nicely wrapped up in the object prototype itself.
I guess my goal here is when its time to save to memory I rip all the data out of the class. Then on the next tick when its time to load I put that data back into a new instance of the same class made that tick.
-
RE: Add functionality to memory objects
Ok cool. Thanks for your help. I must be doing something wrong with this. I will get back here with more information.
EDIT: also clever joke. When I re read it I laughed
-
RE: Add functionality to memory objects
As I said before.
setPrototypeOf
has the exact same problem with the exact error.Also from what I've read and tested, directly adding a function to an objects prototype would not be saved because
JSON.stringify
ignores functions all together -
Adding functions to memory object prototypes seems to cause memory loading error.
See this forum post for full details: https://screeps.com/forum/topic/2428/add-functionality-to-memory-objects
Basically when I try to use the following line of reasoning:
class basicClass{ function test(){ return 'test'; } } Game.rooms['sim'].memory.test = new basicClass();
I know I will have to re add the class method to the memory object prototype.
var obj = Game.rooms['sim'].memory.test; //As expected obj.test is undefined console.log(obj.test); //This works but for a couple ticks in a row i will get a memory error before my script begins. Object.getPrototypeOf(obj).test = basicClass.prototype.test;
The error I get is: "Error: "test" is not a valid memory segment ID at self.onmessage (blob:https://screeps.com/83fe0913-2d1b-43e0-83a7-d3490fc5868b:2:100470)"
-
RE: Add functionality to memory objects
Ok so I've been taking a look at this a little more and it seems like what I'm trying to is legal. I will put this in the bug section and see what happens there.
-
RE: Add functionality to memory objects
Hmmm never tried that.
Turns out the exact same thing happens.
-
Add functionality to memory objects
I have a class with some properties and methods that will eventually be dropped into a room memory.
On the next tick when I pull that memory object I have all the correct properties but non of the methods. I understand why that is and that isn't too big of a problem. I'll show my problem in the below code:
class testClass { constructor(){ this.prop = 'test'; } test(){ return this.prop; } }
Later I will save this object:
var room = Game.rooms['sim']; var classObj = new testClass(); room.memory.test = classObj;
On the next tick I will pull up the object:
var room = Game.rooms['sim']; var classObj = room.memory.test; //correctly sends the word test to the console. console.log(classObj.prop); //understandably sends undefined to the console. console.log(classObj.test);
I have attempted to remedy this problem by adding the function to the memory object prototype.
Object.getPrototypeOf(classObj).test = testClass.prototype.test;
This works some of the time except I constantly keep getting this error in the console every couple ticks before my whole script is even started.
"Error: "test" is not a valid memory segment ID at self.onmessage (blob:https://screeps.com/83fe0913-2d1b-43e0-83a7-d3490fc5868b:2:100470)"
Am I doing this completely wrong? What can I do to make this error go away?
-
RE: screep.carry.energy show 50 units while right panel for creep clearly show it is not carrying anything
Just fixed the problem. I was accidentally setting that value to 50 earlier and I guess the system resets back to the correct value in between loop calls.
-
screep.carry.energy show 50 units while right panel for creep clearly show it is not carrying anything
I have a harvester creep that is supposed to gather energy when it is not carrying any energy. This code was working before but the creep grew old and died while its inventory was pretty much full and a new one replaced it. The new creep's energy value (screep.carry.enery) is now returning 50 at all times. As such my harvester is constantly trying to tranfer energy it does not have to other creeps. I have tried the drop commands and would like an explanation as to why it is behaving as such.