ThePix / QuestJS

A major re-write of Quest that is written in JavaScript and will run in the browser.
MIT License
66 stars 13 forks source link

agenda.walkRandom error: TypeError: w[npc.loc][exit] is undefined #2

Closed Kln95130 closed 5 years ago

Kln95130 commented 5 years ago

Good evening.

I am trying to build an agenda for a "zombi" NPC. I have an error when trying to implement the walkRandom function, and I think it might be a code problem.

TypeError: w[npc.loc][exit] is undefined

Here are my items:

createItem("target",
NPC(),
//MONSTER(3, PRONOUNS.plural, 0, 50, 0), (custom template; does not affect the issue)
  {
        alias: "Target",
        loc:"lounge",
        regex:/^target$/,
        //agenda: ["possessedAI:player:The target moves!"]
        agenda: ["walkRandom: the target moves!"]
  }
);

createRoom("lounge", {
  desc:'A smelly room with an [old settee:couch:sofa] and a [tv:telly].',
    north:new Exit("devRoom"),
    east:new Exit("bin"),
});

createRoom("devRoom", {
  desc:'The mysterious dev room!',
    south:new Exit("lounge"),
});

createRoom("bin", {
  desc:'Where dashed hopes go to die.',
    west:new Exit("lounge"),
});

Here is the current version of walkRandom on my project:

  // Move to another room via a random, unlocked exit, then print the rest of the array as text
  walkRandom:function(npc, arr) {
    //debugmsg("Moving random...");
    const exit = w[npc.loc].getRandomExit(true);
    if (exit === null) {
      this.text(npc, arr);
      return true;
    }
    const dest = w[npc.loc][exit].name; //THIS IS WHAT CAUSES THE ERROR
    //debugmsg("dest:" + dest);
    if (!w[dest]) errormsg("Location '" + dest + "' not recognised in the agenda of " + npc.name);
    npc.moveWithDescription(dest);
    return false;
  },

Thanks for all your work so far, by the way.

Kln95130 commented 5 years ago

Replacing const dest = w[npc.loc][exit].name; by const dest = exit.name; does the trick.

ThePix commented 5 years ago

Thanks. I did test the patrol worked, but must have later updated it and not then tested. I have added the correction to the code.

Kln95130 commented 5 years ago

Hello again. Following your commit, there is another error message. I think you need to replace if (!w[dest]) errormsg("Location '" + exit.name + "' not recognised in the agenda of " + npc.name); by if (!w[exit.name]) errormsg("Location '" + exit.name + "' not recognised in the agenda of " + npc.name);