Morgan
Morgan

Reputation: 196

Object.keys not working?

With the following in memory: (this can be copy/pasted directly into the Memory tab and committed)

    {
    "creeps": {},
    "spawns": {},
    "DEBUG": true,
    "q": {
        "[spawn Spawn1]": [
            [
                [
                    "work",
                    "carry",
                    "carry",
                    "move",
                    "move"
                ],
                "harvester_1",
                {
                    "type": "harvester"
                }
            ],
            [
                [
                    "tough",
                    "attack",
                    "move",
                    "move"
                ],
                "guard2",
                {
                    "type": "guard"
                }
            ],
            [
                [
                    "work",
                    "carry",
                    "carry",
                    "move",
                    "move"
                ],
                "harvester_3",
                {
                    "type": "harvester"
                }
            ],
            [
                [
                    "ranged_attack",
                    "move",
                    "move",
                    "move",
                    "move"
                ],
                "fighter4",
                {
                    "type": "ranged_fighter"
                }
            ],
            [
                [
                    "heal",
                    "heal",
                    "move",
                    "move",
                    "move"
                ],
                "healer5",
                {
                    "type": "healer"
                }
            ]
        ]
    }
}

The following code returns 0:

console.log(Object.keys(Memory.q).length);

When it's obvious that Memory.q in fact holds a reference to a spawning location.

logging just (Memory.q) outputs the [Object object] reference, so I know it exists somewhere.

What am I missing? Or is this just bugged?

Upvotes: 1

Views: 623

Answers (1)

Morgan
Morgan

Reputation: 196

Woops.

Remember that every tick the entire script gets run over again.. Which I forgot.

At the beginning of my script I was initializing Memory.q = {}; every tick, so it was emptying the object and then filling it back up again, which is why the check was failing. I changed the initial code to:

if (typeof Memory.q == "undefined") {
    Memory.q = {};
}

Cheers!

Upvotes: 1

Related Questions