How to create a vending machine? (Solved, thank you)

Hi, I'm kinda new at making games here. So...

The character will pick up a wallet that has a specific number of money (its gold in my game) and uses it to buy things from the vending machine until the wallet runs out.
How do you do that?


Well you have to think about what Objects you need to create, then the individual custom attributes you give them.
So you have a Wallet and a Vending Machine and some 3rd object tied to the Vending Machine purchase.

For your Wallet you have Money as an attribute and for the Vending Machine you'll have an ID attribute you can associate to a single item to purchase (repeat for multiple items).

Objects have a Use script so you can Use the Wallet on the Vending Machine and from that you can give the player another object in to their inventory based on the player selection.

I won't go into more specifics as you should try to do this by yourself, but any problems I'm sure anyone here would be happy to help.


Do you want/have a specified amount of gold... like 37gp, or is it simpler, like you have gold and...

put gold in machine. get candy.
put gold in machine. get candy.
put gold in machine. get candy. (gold disappears from inventory)

put gold in machine. You have no gold left.


It should not be too different to a shop...

http://docs.textadventures.co.uk/quest/shop.html


It depends on how complex you want it. The Pixie's way is a bit too complicated for me (sorry).
I'll use my buy command as an example.

Buy #object#
(You have to use capital or something because "buy" is already a command in quest.)

msg ("See something that catches your eye?")
options = Split("Potion (100);Hyper Potion (200)", ";")
ShowMenu ("Shop", options, true) {
  switch (result) {
    case ("Potion (100)") {
      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.")
      }
    }
    case ("Hyper Potion (200)") {
      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.")
      }
    }
  }
}

Basically it's a few intergers/attributes, another attribute for options, and a show menu. It just looks complicated because the show menu thing has so many "buttons and dials."

I recommend moving the command to a single room. If you use the online version, there is a button for it. For some reason, that works, for keeping the command only in one place.


Of course, Pixie's code is good if you want multiple cloned objects.

(See the post below to show attributes/values in the status pane.)

P.S., This is code I use to buy an object, I call it Buy 2.
Blah

msg ("See something that catches your eye?")
options = Split("Potion (100);Hyper Potion (200);Pistol Fish (360);Ammo (40)", ";")
ShowMenu ("Shop", options, true) {
  switch (result) {
    case ("Potion (100)") {
      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.")
      }
    }
    case ("Hyper Potion (200)") {
      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.")
      }
    }
    case ("Pistol Fish (360)") {
      if (player.gold >= 360) {
        player.gold = player.gold - 360
        AddToInventory (pistol)
      }
      else {
        msg ("Either you don't have enough gold, or you already have it.")
      }
    }
    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.")
      }
    }
  }
}

This requires you to first make the item and store it somewhere (like the store/shop/vending machine).


Not to be unpleasant or anything, but does everyone know how to post code so the formatting is retained?

```
msg ("This is my code.")
if (player.aware) {
  msg ("I'm sorry to bring it up, if so.")
}
```

msg ("This is my code.")
if (player.aware) {
  msg ("I'm sorry to bring it up, if so.")
}

http://docs.textadventures.co.uk/forum/#codeblocks


Again:

This is not an attempt to be unpleasant. It's just much easier to read, copy, and paste codes this way.


Ah, okay.

When I first came here I didn't know how to, then I just didn't bother with it.


@jmn

You're cool!

It took me a while to figure it out. Then it took a little longer to remember to do it. Then I didn't bother with it. Then 'the man' asked nicely, so I started posting appropriately and...

Dang...

Now I'm 'the man'!


That does look much better, though; doesn't it?


Anyway, how do you see the potion in your inventory? Is there an object or a room that you place in scope if (player.hyper_potion > 1)?

Or is there an object carried by the player that has invisible potions in them?

(I'm seriously asking, by the way. Not being a smart-aleck.)


Actually The Pixie taught me... sort of. I used his combat tutorial.

I adapted an already existing dictionary. And I made new intergers.

I use the online version.

Here's what the code looks like. This is in the start script.

player.alias = "you"
player.hitpoints = 110
player.max = 110
player.damage = 3
player.attack = 1
player.defence = 0
player.armour = 0
player.exp = 0
player.level = 0
player.attackers = NewObjectList()
player.ammo = 40
player.potion = 1
player.hyper_potion = 0
player.gold = 0
player.statusattributes = NewStringDictionary()
dictionary add (player.statusattributes, "hitpoints", "Hit points: !")
dictionary add (player.statusattributes, "ammo", "Spare ammo: !")
dictionary add (player.statusattributes, "equippedname", "Weapon: !")
dictionary add (player.statusattributes, "ammonote", "Ammo: !")
dictionary add (player.statusattributes, "level", "level: !")
dictionary add (player.statusattributes, "exp", "exp: !")
dictionary add (player.statusattributes, "potion", "Potions: !")
dictionary add (player.statusattributes, "hyper_potion", "Hyper Potions: !")
spade.damage = "2d5"
spade.attack = 3
pistol.damage = 0
pistol.attack = 0
pistol.firearmdamage = "3d7"
pistol.firearmattack = 3
pistol.ammo = 7
pistol.ammomax = 6
game.notarealturn = false
dictionary add (player.statusattributes, "gold", "Gold: !")

At the end it shows the stats in the status pane. Just look at my so-far only published game, The Legend of The Secret of the Smelly, Stinky Fish.

Blah
Also I made an object that the player uses to heal themselves, a "potion bag." Not that you technically need it, but I programmed the game so that the player needed it to progress (locked the first room). Otherwise, you would type in "Heal potion potion bag", or " Heal Hyper potion potion bag." I tried using it without the "#object#", but for some reason I couldn't get it to work. Maybe I'm just bad at programming, maybe I should have followed The Pixie's example of "if player is carrying potion bag"...
Blah


Oh... So many different ways...

Meh okay whatever I'll try everything to see which one is easiest for me.

THANK YOU PEOPLE


Yes! Finally! I made a vending machine too! Thanks to all who posted on this forum- big help!


for anyone's use/amusement, you can take a look at this 'skeeball' game that I made for someone:

http://textadventures.co.uk/forum/quest/topic/yttvba_5o0katld5xx8zua/help-with-tickets-attributes#683a2c46-9b02-4be7-ab9b-d5992eea760c

it's not a top-down design and/or nor Pixie's design ability level (I'm not even close to being at these coding levels yet), but it's a way of thinking of design that's a bit more advance than how someone new to coding would try to do it.


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

Support

Forums