Checking wether a part of Memory is undefined



  • Hello,

    i try to store a path only once in the global memory.

    if (Memory.Path == null) {
                var Path = Game.rooms['W1N1'].findPath(fromsomewhere, tosomewhere, { serialize: true });
                Memory.Path = Path;
            }
    console.log("Path:" + Memory.Path);

    The console logs: Path:        , thats logic because the if statment is false.

    Ofcourse Memory.Path is undefined, null, "" or something like this but i have tried everything  i can think about but the if check is never true, so what do i put in instead of null to get that working?

    When i look into the Memory i can type in a value for Path but as long as i am not doing it theres nothing as value declared.


  • CoPS

    In javascript, `undefined`, `null`, and negative numbers are "falsey" - which is to say they are evaluated as false in a boolean context.

    In an object (i.e. Memory), a non-existing property is undefined.

    So, a simple boolean test should do it

    if (!Memory.path) {
    // do stuff
    }
    console.log(`Path: ${Memory.Path}`); // should succeed

    (edits because I'm bad at formatting)