Allow us to access .type of objects



  • When automating some of my logging I've stumbled into an odd problem - how to quickly determine what type of object is `this`. After quickly looking at some objects .toString I saw that they indeed call `this.type` but we cannot access that property (returns undefined).

    I am not sure what is the reason to not expose `this.type` for our scripts, but could we pretty please get access to it? It sure is easier than having to parse it out of strings, or running ifs to determine type of an object.



  • Have you tried typeof command?



  • Yep, with the same undefined result.

     

    Update, actually this is all you get when you use it right:

     

    typeof(Game.getObjectById('579db57467f3738857a0844f'))
     
    object
     
    So not much help!


  • (Game.getObjectById('57a025bdcfa3ffae361105ba')) instanceof StructureTower

    👍


  •     report(thing) {
            var report = "";
            report += "Report For "+  thing + "\n";
            if (thing.structureType != null) {
                report += "Type: STRUCTURE\n";
            } else if ( thing instanceof Creep ) {
                report += "Type: CREEP\n";
            }
            report += "ID: " + thing.id + "\n";
            report += "---------------\n";
            report += "STRINGIFIED:\n" + JSON.stringify(thing) + "\n";
            report += "---------------\n";
            
            console.log(report);
        }

    ---------------
    Report For [creep Kaelyn]
    Type: CREEP
    ID: 58841ace5f886cd81fe8c6a6
    ~~~~~~~~~~
    STRINGIFIED:
    {"room":{"name":"E75N2","mode":"world","energyAvailable":500,"energyCapacityAvailable":550},"pos":{"x":24,"y":14,"roomName":"E75N2"},"id":"58841ace5f886cd81fe8c6a6","name":"Kaelyn","body":[{"type":"work","hits":100},{"type":"carry","hits":100},{"type":"carry","hits":100},{"type":"move","hits":100},{"type":"move","hits":100}],"my":true,"owner":{"username":"MrC0de"},"spawning":false,"ticksToLive":1440,"carryCapacity":100,"carry":{"energy":2},"fatigue":0,"hits":500,"hitsMax":500}
    ~~~~~~~~~~

    Thanks your answers all helped me along!
    - Man from Future