<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[How does Memory work?]]></title><description><![CDATA[<p>I've read the <a href="https://docs.screeps.com/global-objects.html" rel="nofollow">https://docs.screeps.com/global-objects.html</a>, but I didn't find the answer...</p>
<p>I want to save an instance of a class to the Memory.
My code is:</p>
<pre><code>class MyClass {
    constructor(name) {
        this.name = name;
    }
    run() {
        console.log(&quot;Run in class&quot;)
    }
}
let instance = new MyClass('name');
Memory.classes = {};
Memory.classes['name'] = instance;
console.log(&quot;'run' method for 'Memory.scripts['name']' (Not in main loop) is:  &quot; + Memory.classes['name'].run)
module.exports.loop = function () {
    console.log(&quot;'run' method for 'Memory.scripts['name']' (In main loop) is:  &quot; + Memory.classes['name'].run)
}
</code></pre>
<p>The console output is:</p>
<pre><code>[22:40:22]'run' method for 'Memory.scripts['name']' (Not in main loop) is:  run() {
        console.log(&quot;Run in class&quot;)
    }
[22:40:22]'run' method for 'Memory.scripts['name']' (In main loop) is:  run() {
        console.log(&quot;Run in class&quot;)
    }
[22:40:23]'run' method for 'Memory.scripts['name']' (In main loop) is:  undefined
[22:40:24]'run' method for 'Memory.scripts['name']' (In main loop) is:  undefined
[22:40:25]'run' method for 'Memory.scripts['name']' (In main loop) is:  undefined
[22:40:26]'run' method for 'Memory.scripts['name']' (In main loop) is:  undefined
</code></pre>
<p>In the first game tick, the <strong>Memory.classes['name'].run</strong> is defined. It's a function <strong>run()</strong>.</p>
<p>But in the following ticks the <strong>Memory.classes['name'].run</strong> is <strong>undefined</strong>.</p>
<p>I would be grateful if someone could help me figure it out.</p>
]]></description><link>http://screeps.com/forum/topic/3026/how-does-memory-work</link><generator>RSS for Node</generator><lastBuildDate>Fri, 13 Nov 2026 08:01:02 GMT</lastBuildDate><atom:link href="http://screeps.com/forum/topic/3026.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 04 Jul 2020 19:49:38 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to How does Memory work? on Invalid Date]]></title><description><![CDATA[<p>I've read the <a href="https://docs.screeps.com/global-objects.html" rel="nofollow">https://docs.screeps.com/global-objects.html</a>, but I didn't find the answer...</p>
<p>I want to save an instance of a class to the Memory.
My code is:</p>
<pre><code>class MyClass {
    constructor(name) {
        this.name = name;
    }
    run() {
        console.log(&quot;Run in class&quot;)
    }
}
let instance = new MyClass('name');
Memory.classes = {};
Memory.classes['name'] = instance;
console.log(&quot;'run' method for 'Memory.scripts['name']' (Not in main loop) is:  &quot; + Memory.classes['name'].run)
module.exports.loop = function () {
    console.log(&quot;'run' method for 'Memory.scripts['name']' (In main loop) is:  &quot; + Memory.classes['name'].run)
}
</code></pre>
<p>The console output is:</p>
<pre><code>[22:40:22]'run' method for 'Memory.scripts['name']' (Not in main loop) is:  run() {
        console.log(&quot;Run in class&quot;)
    }
[22:40:22]'run' method for 'Memory.scripts['name']' (In main loop) is:  run() {
        console.log(&quot;Run in class&quot;)
    }
[22:40:23]'run' method for 'Memory.scripts['name']' (In main loop) is:  undefined
[22:40:24]'run' method for 'Memory.scripts['name']' (In main loop) is:  undefined
[22:40:25]'run' method for 'Memory.scripts['name']' (In main loop) is:  undefined
[22:40:26]'run' method for 'Memory.scripts['name']' (In main loop) is:  undefined
</code></pre>
<p>In the first game tick, the <strong>Memory.classes['name'].run</strong> is defined. It's a function <strong>run()</strong>.</p>
<p>But in the following ticks the <strong>Memory.classes['name'].run</strong> is <strong>undefined</strong>.</p>
<p>I would be grateful if someone could help me figure it out.</p>
]]></description><link>http://screeps.com/forum/post/15444</link><guid isPermaLink="true">http://screeps.com/forum/post/15444</guid><dc:creator><![CDATA[GBLightning]]></dc:creator><pubDate>Invalid Date</pubDate></item><item><title><![CDATA[Reply to How does Memory work? on Invalid Date]]></title><description><![CDATA[<p>Short answer, you can't.</p>
<p>The reason for this is your code only executes in tiny spurts once per tick, between ticks you could lose the global memory and it would have to be recreated.</p>
<p>Store data you need to be persistent in the Memory object, &quot;Memory&quot; that's part of your global memory set up for you at the beginning of each tick. Game structures have a shortcut to the memory object that is handy for storing the data pertinent to that game structure. For example, the Creep object has a property, &quot;memory&quot; that points to the place in the memory object associated with that creep, keyed off it's name. So the Memory object for creep &quot;myCreep&quot; would be &quot;Memory.creeps.myCreep&quot; and can be accessed through the creep as &quot;creep.memory&quot;</p>
<p>Data that you can just recreate can be stored in global memory, but know that it could simply &quot;go away&quot; and you will have to rebuild it.</p>
]]></description><link>http://screeps.com/forum/post/15445</link><guid isPermaLink="true">http://screeps.com/forum/post/15445</guid><dc:creator><![CDATA[Smokeman]]></dc:creator><pubDate>Invalid Date</pubDate></item><item><title><![CDATA[Reply to How does Memory work? on Invalid Date]]></title><description><![CDATA[<p>Short answer: Indeed, you can't.</p>
<p>Better longer answer is that you can ONLY save primitive objects to <code>Memory</code>. As stated in the documentation, this is because at the end of each Tick, <code>Memory</code> is serialized by <code>JSON.stringify</code> and the resulting string is saved to the backend.</p>
<p>On the next tick, the backend gives back a plain string which is passed to <code>JSON.parse</code> to produce the <code>Memory</code> object available to your script.</p>
<p>For this reason, you cannot save any class instances, functions, or Game Object references to <code>Memory</code> as <code>JSON.parse</code> will not know how to reinstantiate the class/function on the next tick.</p>
<p>The usual solutions are to save Game Object <code>Id</code>s (e.g. <code>Memory.mainSpawnId = Game.spawns.Spawn1.id</code>). <code>Id</code>s are strings, and will be returned from memory on the next tick, and you can look up the Game Object instance on that tick with: <code>const spawn = Game.getObjectById(Memory.mainSpawnId)</code></p>
<p>You should save to <code>Memory</code> just enough primitive information to reinstantiate your needed classes on later ticks.</p>
<hr />
<p>Unrelated to the <code>Memory</code> object, there is another caching technique where you cache real object instances on the Global scope. (e.g. <code>global.MainClass = new MyClass()</code>)</p>
<p>This object actually will survive the Tick, as it exists in your virtual javascript instance on the server. (However, I believe you cannot store actual Game Objects (e.g. Creeps, Structures, etc) this way, as they are regenerated every tick by the runtime).</p>
<p>However, while <code>Memory</code> has a system guarantee to ALWAYS survive till the next tick, Global scope has no such guarantee. And anything saved on Global Scope can be cleared without warning by the Runtime if it restarts your isolated VM. (see: <a href="https://wiki.screepspl.us/index.php/Global_reset" rel="nofollow">https://wiki.screepspl.us/index.php/Global_reset</a>). This is what Smokeman is referring to as you should only store data on Global Scope that you can regenerate/recalculate from the information you have in <code>Memory</code>.</p>
<p>Combining these two techniques efficiently is a large part of optimizing Screeps <img
      src="http://screeps.com/forum/plugins/nodebb-plugin-emoji/emoji/android/1f642.png?v=a1k070tfs06"
      class="not-responsive emoji emoji-android emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
]]></description><link>http://screeps.com/forum/post/15446</link><guid isPermaLink="true">http://screeps.com/forum/post/15446</guid><dc:creator><![CDATA[Keidence]]></dc:creator><pubDate>Invalid Date</pubDate></item></channel></rss>