Can't store a function in Memory



  • When I try to store a function in Memory (no matter how deeply nested), I get an error:

    > Memory.roles.foo[0] = function(){}
    Error: Failed to execute 'postMessage' on 'WorkerGlobalScope': An object could not be cloned.
        at Error (native)
        at DedicatedWorkerGlobalScope.self.onmessage (blob:https%3A//screeps.com/998be971-c9a5-4b18-a970-0a3f39ccd455:4:13263)
    

    Is this intentional?



  • Memory is stored as JSON between turns. JSON doesn't support storing javascript functions, it only supports storing objects, array, number, string and nulls.



  • You may be able to do: Memory.roles.foo[0] = function(){}.toSource()

    Curious though, why do you want to do that? It takes more space and doesn't really convey any real info.