<Solved> Object not recognized in inventory

What should happen (short version): The player looks under the mat for the first time and finds a key. Return message "The key is there, just like Dad said. You take it." The key is added to the inventory. If the player looks under the same mat again, the message will return "You already took the key." and the key will not be re-added. The door is set to open with the key.

What is happening (short version): When testing, I look under the mat. I get the message "The key is there, just like Dad said. You take it." The key is not added to my inventory. I look under the mat again, and I receive the same message: "The key is there, just like Dad said. You take it." The key is not added to my inventory. This loops if I continue looking under the mat. I cannot open the door to progress because the game says I do not have the key.

The coding for 'object': I have the office key listed as an object that is not visible in the room description. Its alternatively accepted labels are officekey, door key, doorkey, key. Its parent is the doormat (whose alternatively accepted labels are mat and door mat). The doormat is visible in the room description. It has a few red herring verbs (such as look at, eat, step on, etc.) and it has the main verb "look under" (alt. "look beneath" "move" or "pick up".)

If/else coding: The coding I have for the "if no key, get key; if key, no get key" is as follows in the code editor:
if (Got(office key)) {
msg ("You already have the key.")
}
else {
msg ("The key is there, just as Dad said. You pick it up.")
AddToInventory (office key)
}

Other Notes: The player will not return to this location, so I am not worried about the fact that with the current code, the player can technically re-find the key after using it. The player moves locations after using the key and does not return to this location.

What I'm asking: What am I doing wrong, and how do I correct this error? Thank you in advance for your time and help. <3


My brain doesn’t work like the good coders here but...

What I would do is set a flag on the room the doormat and key are in. Call it something like ‘keytaken’. Then check for that flag once the doormat is looked under, set the flag. On the doormat verb ‘look under’, simply put your “if/Then” script. If ‘room’ has ‘keytaken’ print msg; else if ‘room’ does not have ‘keytaken’ run your add key to inventory script.

I know this doesn’t really answer your question regarding what you’re not doing correctly but I use my method all the time and works perfectly.

My guess: the flag is set in a different location than where you are checking. That’ll happen, for example, if you set the if/then flag on the doormat but check it on the ‘room’. Let us know!

Happy gaming.

XM


That's such a good idea, thank you! I will try that.


You said "I have the office key listed as an object that is not visible in the room description"

Does this mean that the key isn't visible?
If so, this will add it to the inventory but it will still be invisible. So you would need something like:

if (Got(office key)) {
  msg ("You already have the key.")
}
else {
  msg ("The key is there, just as Dad said. You pick it up.")
  AddToInventory (office key)
  MakeObjectVisible (office key)
}

I notice you considered the possibility of the player "finding" the key again after leaving it somewhere else. While this may not happen in this particular case, it's good to have a way to do things that doesn't lead to this issue. There are ways to check if you've found an object other than looking in the inventory, and it's a good habit to get into :)

For this reason, I would suggest changing the line:

if (Got(office key)) {

to one of:

  • if (not Contains (this, office key)) {
    (tests if the key is not in the mat)
  • if (GetBoolean (office key, "hasbeenmoved")) {
    (tests if the key has been moved from its initial location)
  • if (office key.visible) {
    (tests if the key is visible)

YES!! That's the problem I had. Thank you, and thank you for the future notice about re-finding the item.


This topic is now closed. Topics are closed after 60 days of inactivity.

Support

Forums