Preventing a player from taking an object until conditions are met

I’m trying to do something like the custom get/drop message in the documentation: Adventuron - Cookbook

I have a ‘meal’ the player can only pick up if they have a basket. I tried putting the following code in an on_command block, but when I type GET MEAL in the game it says “You take the meal.”

   : match "get meal;take meal;pick meal" {
      : if (is_beside "meal"){
         : if (is_carried "basket"){
            : get "meal" quiet = "true" ;
            : if (is_carried "meal") {
               : print "You lift the meal into the basket." ;
         }
         : else {
            : print "It slips through your hands. You need something to carry it."; 
         }
       }
      } 
   }

Any suggestions on how to keep the player from grabbing the meal without the basket?

It was because of brackets. With the correct indentation this is easier to understand.

I think you may have your editor indents set to 2 or 4, and Adventuron’s editor kind of likes 3, if you can set the editor indents to 3 for these types of files the intents will travel better between your text editor and the browser editor.

(By the way, get/take/pick up are system level synonyms, so you only need to match one of them.)

   : match "get meal" {
      : if (is_beside "meal") {
         : if (is_carried "basket") {
            : pocket "meal" ;
            : if (is_carried "meal") {
               : print "You lift the meal into the basket." ;
            }
         }
         : else {
            : print "It slips through your hands. You need something to carry it."; 
         }
      }
   }
1 Like

Thank you, and thank you for the tip on indents!

In addition to what @adventuron said, you might like to restructure your code a bit to minimise the nested if statements and the need for the else statement. I also find it a good habit to add a done statement when you’re finished processing, otherwise it can fall through and execute other unanticipated code within the same action handler. The check for !is_pocketable is not necessary if you have a huge inventory limit (the default in Adventuron is 10 items), but it’s just a precaution.

   : match "get meal" {
      : if (is_beside "meal") {
         : if (!is_carried "basket") {
            : print "It slips through your hands. You need something to carry it.";
            : done;
         }
         : if (!is_pocketable "meal") {
            : print "You can't carry any more.";
            : done;
         }
         : pocket "meal";
         : print "You lift the meal into the basket.";
         : done;
      }
   }

Yes, putting in the :done at the end is a good belt-and-braces way of stopping downstream code firing off unexpectedly (caused me many issues before I got into that habit).