Memory slot for Structures



  • Hi,

    I'm relatively new (I don't know if that requested has already been made), I've been in trouble when I tried to add behaviour to my link.

    Right now only spawns/flags have memory slot to store data, and that pretty fine but other structures like tower or link don't have that privilege, so we have to make some workarround with look up table ['id', [data1, data2]] and store it somewhere in the memory I think it could be nice to allow data storage on those structures to ease theire data access.

     

    Thanks,

    Shadow_bird


  • Culture

    You can use Memory.anythingHereReally to store data. You could technically create your own Memory.structures using this technique (same as Memory.spawns)



  • I do know that, this what I was saying with the look up table.

    An Example :

    var links = room.find(STRUCTURE_LINK);

    var state = Memory.objects[ links[0].id ][ 'state' ];

     

    this is one way to do it right now but it could be easier that way : 

    var links = room.find(STRUCTURE_LINK);

    var state = links[0].memory.state;

     

    Actualy it's already possible with spawns ...

     


  • Dev Team

    Built-in memory properties exist only for objects with names. If you want to use your own memory schema that doesn't involve names as keys, it's up to you. We don't have any plans to add ID-based memory schema.



  • Ok, let me know if there is a change about it one day. 😉



  • You can extend the Structure prototype and thus add that property yourself, you don't need the devs for that.. : )



  • Here is an example of how to add it yourself. you have to do this every tick before you use the .memory of the structure. Idk if this is done well or not but it works for me. I got the code from http://stackoverflow.com/questions/30147800/extend-source-prototype-to-have-a-memory-object and just modified it slightly so it would be for structures instead of sources:

    Object.defineProperty(Structure.prototype, 'memory', {
    configurable: true,
    get: function() {
    if(_.isUndefined(Memory.myStructuresMemory)) {
    Memory.myStructuresMemory = {};
    }
    if(!_.isObject(Memory.myStructuresMemory)) {
    return undefined;
    }
    return Memory.myStructuresMemory[this.id] = Memory.myStructuresMemory[this.id] || {};
    },
    set: function(value) {
    if(_.isUndefined(Memory.myStructuresMemory)) {
    Memory.myStructuresMemory = {};
    }
    if(!_.isObject(Memory.myStructuresMemory)) {
    throw new Error('Could not set source memory');
    }
    Memory.myStructuresMemory[this.id] = value;
    }
    });