How to check if the player has money

My game is a thing where you are trying to find an item, and along the way you run into a vending machine

The vending machine has a verb called 'buy something'

If you do it subtracts 2 dollars from your money supply

But it will even subtract into the negative?

So can anyone tell me how to make it so that the computer will tell me 'You cannot buy this' if I don't have 2 dollars?

Thank you all

I am using the windows edition


Check your money first and only allow buying if there is sufficient.

psuedo code -
if player.money >= item.price then buy item else say"You don't have enough to buy that"


It's different, but sufficient.

msg ("See something that catches your eye?")
options = Split("Potion (100);Hyper Potion (200);Ammo (40);Ammo (80)", ";")
ShowMenu ("Shop", options, true) {
  switch (result) {
    case ("Potion (100)") {
      if (game.hard = true) {
        if (player.gold >= 100) {
          player.gold = player.gold - 100
          player.potion = player.potion + 1
          msg ("You bought a Potion.")
        }
        else {
          msg ("You don't have enough gold.")
        }
      }
      else {
        msg ("The person shakes his head. \"Sorry, I can't do that. We're all out.\"")
      }
    }
    case ("Hyper Potion (200)") {
      if (game.hard = true) {
        if (player.gold >= 200) {
          player.gold = player.gold - 200
          player.hyper_potion = player.hyper_potion + 1
          msg ("You bought a Hyper Potion.")
        }
        else {
          msg ("You don't have enough gold.")
        }
      }
      else {
        msg ("The person shakes his head. \"Sorry, I can't do that. We're all out.\"")
      }
    }
    case ("Ammo (40)") {
      if (player.gold >= 40) {
        player.gold = player.gold - 40
        player.ammo = player.ammo + 20
      }
      else {
        msg ("You don't have enough gold.")
      }
    }
    case ("Ammo (80)") {
      if (player.gold >= 80) {
        player.gold = player.gold - 80
        player.ammo = player.ammo + 40
      }
      else {
        msg ("You don't have enough gold.")
      }
    }
  }
}

here's a quick simple example:

( Here's a link on a detailed guide on quest structure and its code structure, and also on, using Attributes and the 'if' Script, including arithmetic, addition ~ subtraction ~ multiplication ~ division, operations, and transactional, buying ~ selling, operations: http://textadventures.co.uk/forum/samples/topic/5559/attributes-and-if-script-guide-by-hk )

(ask if you need help with anything)

<object name="room">

  <inherit name="editor_room" />

</object>

<object name="player">

  <inherit name="editor_object" />
  <inherit name="editor_player" />

  <attr name="parent" type="object">room</attr>

  <attr name="currency" type="int">0</attr>

</object>

<object name="vending_machine">

  <inherit name="editor_object" />

  <attr name="parent" type="object">room</attr>

  <attr name="item_list" type="stringlist">
    <value>candy</value>
    <value>chocolate</value>
    <value>soup</value>
    <value>hamburger</value>
  </attr>

  <attr name="buy" type="script">

    <![CDATA[

      show menu ("Which item do you want to buy?", this.item_list, false) {

        item_string_variable = result
        item_object_variable = GetObject (result)

        if (player.currency < item_object_variable.price) {

          msg ("Sorry, but you don't have enough currency for the " + item_string_variable + " item")

        } else {

          player.currency = player.currency - item_object_variable.price

          cloned_object_variable = CloneObject (item_object_variable)
          set (cloned_object_variable, "original_object", item_object_variable.name)

          MoveObject (cloned_object_variable, player)

          msg ("You've bought the " + item_string_variable + " item")

        }

      }

    ]]>

  </attr>

</object>

<object name="vending_machine_items">

  <inherit name="editor_room" />

  <object name="candy">

    <inherit name="editor_object" />

    <attr name="price" type="int">5</attr>

  </object>

  <object name="chocolate">

    <inherit name="editor_object" />

    <attr name="price" type="int">10</attr>

  </object>

  <object name="soup">

    <inherit name="editor_object" />

    <attr name="price" type="int">15</attr>

  </object>

  <object name="hamburger">

    <inherit name="editor_object" />

    <attr name="price" type="int">20</attr>

  </object>

</object>

<verb>

  <property>buy</property>
  <pattern>buy</pattern>
  <defaultexpression>You can't buy that!</defaultexpression>

</verb>

Here is a very basic example (in which you can't actually drink the soda, and the machine has no description):

<!--Saved by Quest 5.7.6404.15496-->
<asl version="550">
  <include ref="English.aslx" />
  <include ref="Core.aslx" />
  <game name="Simple Vending">
    <gameid>3a99a835-1723-423d-9bae-22b14f7d9d8c</gameid>
    <version>1.0</version>
    <firstpublished>2017</firstpublished>
    <showmoney />
  </game>
  <object name="break room">
    <inherit name="editor_room" />
    <object name="player">
      <inherit name="editor_object" />
      <inherit name="editor_player" />
      <money type="int">2</money>
    </object>
    <object name="vending machine">
      <inherit name="editor_object" />
      <feature_usegive />
      <use type="script"><![CDATA[
        // Drinks cost 1 dollar in this example
        if (game.pov.money>0) {
          if (soda.parent = vending machine) {
            MoveObjectHere (soda)
            game.pov.money = game.pov.money - 1
            msg ("You put a dollar into the machine, and a can of {object:soda} appears.")
          }
          else {
            msg ("The machine is sold out.")
          }
        }
        else {
          msg ("You have no money.")
        }
      ]]></use>
      <object name="soda">
        <inherit name="editor_object" />
        <alias>soda</alias>
      </object>
    </object>
    <command name="buy_drink_command">
      <pattern>buy something</pattern>
      <script><![CDATA[
        // Drinks cost 1 dollar in this example
        if (game.pov.money>0) {
          if (soda.parent = vending machine) {
            MoveObjectHere (soda)
            game.pov.money = game.pov.money - 1
            msg ("You put a dollar into the machine, and a can of {object:soda} appears.")
          }
          else {
            msg ("The machine is sold out.")
          }
        }
        else {
          msg ("You have no money.")
        }
      ]]></script>
    </command>
  </object>
</asl>

When I first saw this post (and before anyone else responded), I started working on an example that became a bit complicated.

The machine has a slot, and a tray, buttons, etc., and you can drink the drinks.

http://textadventures.co.uk/forum/quest/topic/s9h31_bc-02rdqogqr7q_g/vending-machine-sodas-only-at-the-moment-work-in-progress


NOTES

  • The money feature needs to be enabled under the Game=>Features tab for my example to work.
  • R2T1's and HK's examples handle different items with different prices. You'd probably need to change player.currency to player.money in HK's example, though, unless you set up the currency attribute.

  • JMNE's example depends on many attributes (such as player.gold, player.ammo, and player.hyper_potion), and errors will pop up left and right if those aren't set up in your game.


ALSO

I use game.pov instead of player because this will handle the scripts if you can play as more than one character.

If you had an object named "Bob", and you could play as Bob, changing (or checking) any attributes on player while playing as Bob would be pointless. Bob and player are two totally separate objects.

So, I use game.pov.money, and Quest knows it should change (or check) the attributes of whomever I am playing as. (This works fine if you only play as the player object, as well.)


This help page may also be useful:
http://docs.textadventures.co.uk/quest/shop.html


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

Support

Forums